Skip to content

Instantly share code, notes, and snippets.

@AnthonyBloomer
Created September 30, 2018 18:32
Show Gist options
  • Save AnthonyBloomer/aab5c0c19b256fe3ae73f04fc8d19e41 to your computer and use it in GitHub Desktop.
Save AnthonyBloomer/aab5c0c19b256fe3ae73f04fc8d19e41 to your computer and use it in GitHub Desktop.
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
class Resume {
String fileName;
String fileUrl;
String getFileName() {
return fileName;
}
void setFileName(String fileName) {
this.fileName = fileName;
}
String getFileUrl() {
return fileUrl;
}
void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
}
public class ResumeDownloader {
private final static String ENDPOINT = "https://api.recruiterbox.com/v2/candidates/";
private final static String API_KEY = "YOUR_API_KEY";
private static ArrayList<Resume> getResumes(ArrayList<Integer> candidates) throws UnirestException {
ArrayList<Resume> resumes = new ArrayList<Resume>();
for (Integer candidate : candidates) {
HttpResponse<JsonNode> response = Unirest
.get(ENDPOINT + candidate)
.basicAuth(API_KEY, "")
.asJson();
JSONObject myObj = response.getBody().getObject();
JSONObject resume = myObj.getJSONObject("resume");
Resume myResume = new Resume();
myResume.setFileName(resume.getString("file_name"));
myResume.setFileUrl(resume.getString("file_url"));
resumes.add(myResume);
}
return resumes;
}
private static ArrayList<Integer> getCandidates() throws UnirestException {
int offset = 0;
ArrayList<Integer> theList = new ArrayList<Integer>();
while (true) {
HttpResponse<JsonNode> response = Unirest
.get(ENDPOINT)
.basicAuth(API_KEY, "")
.queryString("offset", offset)
.queryString("limit", 10)
.asJson();
JSONObject myObj = response.getBody().getObject();
if (myObj.getJSONArray("objects").length() == 0) {
break;
}
JSONArray jsonArray = myObj.getJSONArray("objects");
for (int i = 0; i < jsonArray.length(); i++) {
theList.add(jsonArray.getJSONObject(i).getInt("id"));
}
offset += 10;
}
return theList;
}
private static void downloadResume(String fileUrl, String fileName) throws IOException {
URL website = new URL(fileUrl);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("resumes/" + fileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
public static void main(String[] args) throws UnirestException, IOException {
new File("resumes").mkdir();
ArrayList<Integer> candidates = getCandidates();
ArrayList<Resume> resumes = getResumes(candidates);
int size = resumes.size();
int downloadCount = 0;
for (Resume resume : resumes) {
downloadResume(resume.getFileUrl(), resume.getFileName());
++downloadCount;
System.out.println("Downloaded " + downloadCount + " of " + size);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment