Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created March 22, 2015 17:33
Show Gist options
  • Select an option

  • Save Zolomon/273c13fb9ee51b7dbde3 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/273c13fb9ee51b7dbde3 to your computer and use it in GitHub Desktop.
eda095 lab 1
import java.io.*;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by zol on 3/12/2015.
*/
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: PdfFetcher <url>");
}
try {
URL url = new URL(args[0]);
String contents = downloadHtml(url);
ArrayList<String> parsedLinks = parseHtml(contents);
ArrayList<URL> urls = stringsToUrls(url, parsedLinks);
//downloadPdfs(urls);
} catch (MalformedURLException e) {
e.printStackTrace();
}
/*
Matcher matcher = pattern.matcher("http://www.zolomon.com/game.pdf");
System.out.println(matcher.matches());*/
}
private static ArrayList<URL> stringsToUrls(URL url, ArrayList<String> parsedLinks) {
parsedLinks.parallelStream()
.map((String l) -> {
try {
return new URL(l);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
});
return null;
}
private static ArrayList<String> parseHtml(String contents) {
//Pattern pattern = Pattern.compile("(^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
Pattern pattern = Pattern.compile("\"((https?://)?.*\\.pdf)\"");
Matcher matcher = pattern.matcher(contents);
ArrayList<String> urls = new ArrayList<>();
while (matcher.find()) {
urls.add(matcher.group().replace("\"", ""));
}
return urls;
}
private static String downloadHtml(URL url) {
String encoding = "ISO-8859-1";
URLConnection uc = null;
try {
uc = url.openConnection();
String contentType = uc.getContentType();
int encodingStart = contentType.indexOf("charset=");
if (encodingStart != -1) {
encoding = contentType.substring(encodingStart + 8);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
StringBuilder stringBuilder = new StringBuilder();
try (Reader r = new InputStreamReader(in, encoding)) {
int c;
while ((c = r.read()) != -1) {
stringBuilder.append((char) c);
}
}
in.close();
return stringBuilder.toString();
} catch (MalformedURLException ex) {
System.err.println(url.toString() + " is not a parseable URL");
} catch (UnsupportedEncodingException ex) {
System.err.println("Server sent an encoding Java does not support: " + ex.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static void downloadPdfs(URL[] urls) throws IOException {
for (URL url : urls) downloadPdf(url);
}
private static String downloadPdf(URL url) throws IOException {
URLConnection con = url.openConnection();
String contentType = con.getContentType();
int contentLength = con.getContentLength();
if (contentType.startsWith("text/") || contentLength == -1) {
throw new IOException("This is not a binary file");
}
try (InputStream raw = con.getInputStream()) {
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int offset = 0;
while (offset < contentLength) {
int bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1) break;
offset += bytesRead;
System.out.println("Read: " + bytesRead);
}
if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes in total");
}
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf("/") + 1);
try (FileOutputStream fout = new FileOutputStream(filename)) {
fout.write(data);
fout.flush();
System.out.println("File saved: " + filename);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment