Skip to content

Instantly share code, notes, and snippets.

@pinapelz
Created July 25, 2023 08:32
Show Gist options
  • Select an option

  • Save pinapelz/1b02334aa09267717877e0502b4cf579 to your computer and use it in GitHub Desktop.

Select an option

Save pinapelz/1b02334aa09267717877e0502b4cf579 to your computer and use it in GitHub Desktop.
import com.pinapelz.Holodex;
import com.pinapelz.HolodexException;
import com.pinapelz.datatypes.*;
import com.pinapelz.query.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main
{
static String API_BASE_URL = "";
static String QUEUE_URL = "";
static String QUEUE_TOKEN = "";
static String HOLODEX_API_KEY = "";
static int MAXIMUM = 2000;
public static boolean queueVideo(String url) {
try {
URL urlObject = new URL(QUEUE_URL);
HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
connection.setRequestMethod("POST");
Map<String, String> headers = new HashMap<>();
headers.put("X-AUTHENTICATION", QUEUE_TOKEN);
for (Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
String data = "url=" + url;
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean videoIsArchived(String videoId){
try {
URL url = new URL(API_BASE_URL+videoId);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
String errorJson = "{\"error\":\"Video ID does not exist\"}";
boolean hasError = response.toString().equals(errorJson);
if (hasError) {
return false;
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
int offset = 0;
if(args.length != 0){
try {
offset = Integer.parseInt(args[0]);
MAXIMUM = Integer.parseInt(args[1]);
}
catch (Exception e){
System.out.println("Invalid arguments");
System.exit(1);
}
}
try {
Holodex holodex = new Holodex(HOLODEX_API_KEY);
for (int i = 0; i < MAXIMUM; i += 50) {
System.out.println("Getting videos from " + offset + " to " + (offset + 50));
List<SimpleVideo> videos = (List<SimpleVideo>) holodex.searchVideo(new VideoSearchQueryBuilder().setSort("newest").setTopic(List.of("Music_Cover", "Original_Song")).
setPaginated(false).setLimit(50).setOffset(offset));
for (Object video : videos) {
SimpleVideo vid = (SimpleVideo) video;
Video detailedVid = holodex.getVideo(new VideoByVideoIdQueryBuilder().setVideoId(vid.id));
System.out.println("Checking " + vid.id + " " + vid.title);
if (vid.status.equals("past") && !videoIsArchived(vid.id) && detailedVid.duration > 60 && detailedVid.duration < 600){
if (queueVideo("https://www.youtube.com/watch?v=" + vid.id)) {
System.out.println("Queued " + vid.id);
} else {
System.out.println("Failed to queue " + vid.id);
}
}
}
offset += 50;
System.out.println("Sleeping for 2 minutes");
Thread.sleep(120000);
}
} catch (HolodexException ex) {
throw new RuntimeException(ex);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment