Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
# Created by .ignore support plugin (hsz.mobi)
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions org/geekhub/ConnectionUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.geekhub;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;


/**
* Utils class that contains useful method to interact with URLConnection
*/
Expand All @@ -11,12 +14,24 @@ public class ConnectionUtils {
/**
* Downloads content for specified URL and returns it as a byte array.
* Should be used for small files only. Don't use it to download big files it's dangerous.
*
* @param url
* @return
* @throws IOException
*/
public static byte[] getData(URL url) throws IOException {
//implement me
return null;
public static byte[] getData(URL url) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];

try (InputStream inStream = url.openConnection().getInputStream()) {
while ((nRead = inStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
} catch (IOException e) {
e.printStackTrace();
}

return buffer.toByteArray();
}
}
20 changes: 15 additions & 5 deletions org/geekhub/ImageCrawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
* To shutdown the service you should call stop() method
*/
public class ImageCrawler {

//number of threads to download images simultaneously
public static final int NUMBER_OF_THREADS = 10;

private ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
private String folder;

Expand All @@ -25,11 +23,18 @@ public ImageCrawler(String folder) throws MalformedURLException {

/**
* Call this method to start download images from specified URL.
*
* @param urlToPage
* @throws IOException
*/
public void downloadImages(String urlToPage) throws IOException {
//implement me
Page page = new Page(new URL(urlToPage));

for (URL url : page.getImageLinks())
if (isImageURL(url)) {
ImageTask imageTask = new ImageTask(url, folder);
executorService.execute(imageTask);
}
}

/**
Expand All @@ -41,10 +46,15 @@ public void stop() {

//detects is current url is an image. Checking for popular extensions should be enough
private boolean isImageURL(URL url) {
//implement me
String[] fileExtensions = {"jpg", "png"};
String fileName = url.toString();
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
for (String extension : fileExtensions) {
if (extension.equalsIgnoreCase(fileExtension))
return true;
}
return false;
}



}
15 changes: 14 additions & 1 deletion org/geekhub/ImageTask.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.geekhub;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Represents worker that downloads image from URL to specified folder.<br/>
Expand All @@ -20,11 +25,19 @@ public ImageTask(URL url, String folder) {
*/
@Override
public void run() {
//implement me
Path folderPath = Paths.get(folder + buildFileName(url));
try {
Files.write(folderPath,ConnectionUtils.getData(url));
} catch (IOException e) {
e.printStackTrace();
}
}

//converts URL to unique file name
private String buildFileName(URL url) {
return url.toString().replaceAll("[^a-zA-Z0-9-_\\.]", "_");
}



}
5 changes: 4 additions & 1 deletion org/geekhub/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class Main {

public static final String FOLDER_TO_DOWNLOAD = "d:/images/";
public static final String FOLDER_TO_DOWNLOAD = "/home/sergei/picture/";

public static void main(String[] args) throws IOException {
ImageCrawler imageCrawler = new ImageCrawler(FOLDER_TO_DOWNLOAD);
Expand All @@ -16,9 +16,12 @@ public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String command;
while(!"exit".equals(command = scanner.next())) {
System.out.println(command);
imageCrawler.downloadImages(command);

System.out.println("...and another url:");
}
System.out.println("exit go");
imageCrawler.stop();
}
}