Skip to content

Instantly share code, notes, and snippets.

@florinbarbisch
Created April 10, 2020 18:07
Show Gist options
  • Save florinbarbisch/e0cb6c0ca81535f6239e69e8ed105558 to your computer and use it in GitHub Desktop.
Save florinbarbisch/e0cb6c0ca81535f6239e69e8ed105558 to your computer and use it in GitHub Desktop.
A java downloader for all AppleTV Wallpapers (according to https://aerial-screensavers.netlify.com/). With the ability to resume the download if it gets interupted/stopped (just rerun the program). Using java.nio and parallel streams (for the flex).
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class AerialScreensaverDownloader {
private static final File BASE_DIR = new File("C:/tmp/aerial-screensavers/");
public static void main(String[] args) throws Exception {
String htmlContent;
try (var urlStream = new URL("https://aerial-screensavers.netlify.com/").openStream()) {
htmlContent = new String(urlStream.readAllBytes());
}
Pattern.compile("(http[\\w\\d:/.-]+?\\.mov)")
.matcher(htmlContent)
.results()
.map(MatchResult::group)
.collect(Collectors.toList()) // otherwise the stream does not appear to be parallel
.parallelStream()
.forEach(AerialScreensaverDownloader::saveResumable);
}
public static void saveResumable(String urlString) {
try {
var url = new URL(urlString.replaceFirst("https", "http")); // the certificate is not trusted by java
var outputFile = new File(BASE_DIR, urlString.substring(urlString.lastIndexOf("/")));
var httpConnectionForContentLength = (HttpURLConnection) url.openConnection();
httpConnectionForContentLength.setRequestMethod("HEAD");
var contentLength = httpConnectionForContentLength.getContentLengthLong();
var fileLength = outputFile.length();
var append = fileLength < contentLength;
var httpConnection = (HttpURLConnection) url.openConnection();
if(append) {
httpConnection.setRequestProperty("Range", "bytes=" + fileLength + "-" + contentLength); // only download missing bytes
} else if (fileLength == contentLength) return;
try(var urlStream = httpConnection.getInputStream();
var readableByteChannel = Channels.newChannel(urlStream);
var fileOutputStream = new FileOutputStream(outputFile, append);
var fileChannel = fileOutputStream.getChannel();) {
fileChannel.transferFrom(readableByteChannel, fileLength, Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment