-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from cathrkri/60-download-charity-logo
Merge 60 with develop
- Loading branch information
Showing
15 changed files
with
433 additions
and
265 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/scraper/LogoDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package ntnu.systemutvikling.team6.scraper; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * Facilitates downloading of .png images from the individual charity's page on IK, converting them | ||
| * to bytecode (blob), and then back to a .png. | ||
| */ | ||
| public class LogoDownloader { | ||
|
|
||
| /** | ||
| * Downloads a image from the given URL and converts it to a blob. | ||
| * | ||
| * @param imageUrl the URL of the image | ||
| * @return a blob containing the image data | ||
| */ | ||
| public static byte[] downloadImageAsBlob(String imageUrl) { | ||
| if (imageUrl == null || imageUrl.isBlank()) return null; | ||
|
|
||
| try (InputStream in = new URL(imageUrl).openStream()) { | ||
| return in.readAllBytes(); | ||
| } catch (Exception e) { | ||
| System.out.println("Error: Something went wrong when downloading the image."); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a blob of image data back to a .png image file. | ||
| * | ||
| * @param imageBytes the blob containing the image data | ||
| * @param fileName the filename of the .png image file | ||
| */ | ||
| public static void convertBlobToPNG(byte[] imageBytes, String fileName) { | ||
| if (imageBytes == null) { | ||
| return; | ||
| } | ||
| try { | ||
| Path folder = Paths.get("target", "logo"); | ||
| Files.createDirectories(folder); | ||
|
|
||
| Path filePath = folder.resolve(fileName + ".png"); | ||
|
|
||
| Files.write(filePath, imageBytes); | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println("Error: Something went wrong when converting blob to png."); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.