Last active
September 21, 2023 09:23
-
-
Save thinkbigthings/6b199effd24594d95a086f1ad5f8237f to your computer and use it in GitHub Desktop.
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
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.time.LocalDateTime; | |
import java.util.Base64; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
import static java.net.http.HttpResponse.BodyHandlers.ofInputStream; | |
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | |
import static java.time.ZoneId.systemDefault; | |
import static java.time.format.DateTimeFormatter.ofPattern; | |
public class Downloader { | |
public static void main(String[] args) { | |
if(args.length != 4) { | |
System.out.println(); | |
System.out.println("Usage: java Downloader.java [username] [password] [endpoint] [destFolder]"); | |
System.out.println("Note: enclose argument in single quotes if using special characters on zsh"); | |
System.out.println(); | |
return; | |
} | |
var username = args[0]; | |
var password = args[1]; | |
var endpoint = args[2]; | |
var destFolder = args[3]; | |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | |
scheduler.scheduleAtFixedRate(() -> downloadFile(username, password, endpoint, destFolder), 0, 6, TimeUnit.HOURS); | |
} | |
public static void downloadFile(String username, String password, String endpoint, String destFolder) { | |
String date = LocalDateTime.now(systemDefault()).format(ofPattern("yyyy-MM-dd-HH-mm-ss")); | |
String filename = String.format("download-"+username+"-%s.csv", date); | |
Path destinationFile = Paths.get(destFolder, filename); | |
String auth = username + ":" + password; | |
String authHeader = "Basic " + new String(Base64.getEncoder().encode(auth.getBytes())); | |
HttpClient client = HttpClient.newBuilder() | |
.version(HttpClient.Version.HTTP_1_1) | |
.followRedirects(HttpClient.Redirect.NORMAL) | |
.build(); | |
try { | |
System.out.println(LocalDateTime.now(systemDefault()) + " Calling to " + endpoint); | |
HttpRequest request = HttpRequest.newBuilder() | |
.GET() | |
.uri(new URI(endpoint)) | |
.header("Authorization", authHeader) | |
.build(); | |
var response = client.send(request, ofInputStream()); | |
if (response.statusCode() == 200) { | |
System.out.println(LocalDateTime.now(systemDefault()) + " Writing to " + destinationFile.toFile().getAbsolutePath()); | |
Files.copy(response.body(), destinationFile, REPLACE_EXISTING); | |
System.out.println(LocalDateTime.now(systemDefault()) + " Done."); | |
} | |
else { | |
throw new IOException("Received status code " + response.statusCode()); | |
} | |
} | |
catch (URISyntaxException | IOException | InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment