我正在开发一个 Java 项目,该项目获取 Twitch 上当天最流行的剪辑的 URL。为此,我使用以下代码向 twitch API 发送请求:
private List<TwitchClip> getVideoList() {
try {
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(ZoneId.of("Europe/Berlin"));
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
String formattedStartTime;
String formattedEndTime;
if(String.valueOf(todayMidnight.getMonthValue()).length() != 2) {
formattedStartTime = todayMidnight.getYear() + "-" + 0 + todayMidnight.getMonthValue() + "-" + (todayMidnight.getDayOfMonth() - 1) + "T00:00:00Z";
formattedEndTime = todayMidnight.getYear() + "-" + 0 + todayMidnight.getMonthValue() + "-" + todayMidnight.getDayOfMonth() + "T00:00:00Z";
}else {
formattedStartTime = todayMidnight.getYear() + "-" + todayMidnight.getMonthValue() + "-" + (todayMidnight.getDayOfMonth() - 1) + "T00:00:00Z";
formattedEndTime = todayMidnight.getYear() + "-" + todayMidnight.getMonthValue() + "-" + todayMidnight.getDayOfMonth() + "T00:00:00Z";
}
URL url = new URL("https://api.twitch.tv/helix/clips?game_id=" + Game.FORTNITE.getId() + "&first=25&started_at=" + formattedStartTime + "&ended_at=" + formattedEndTime);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("Client-ID", "");
File out = File.createTempFile(UUID.randomUUID().toString(), ".json");
System.out.println("Downloaded clips data at " + out.getPath());
writeFile(out, connection.getInputStream());
Gson gson = new Gson();
return gson.fromJson(new FileReader(out), new TypeToken<List<TwitchClip>>() {
}.getType());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
慕无忌1623718
相关分类