Skip to content

Commit ac8d9f2

Browse files
committed
refactor: remove repetition
1 parent b25a6e6 commit ac8d9f2

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

lib/src/main/java/io/github/thoroldvix/internal/DefaultPlaylistsTranscriptApi.java

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* Default implementation of {@link PlaylistsTranscriptApi}
1717
*/
1818
class DefaultPlaylistsTranscriptApi implements PlaylistsTranscriptApi {
19-
private static final String MAX_RESULTS = "50";
2019
private final YoutubeClient client;
2120
private final YoutubeTranscriptApi youtubeTranscriptApi;
2221
private final ObjectMapper objectMapper;
@@ -34,22 +33,23 @@ public Map<String, TranscriptList> listTranscriptsForPlaylist(String playlistId,
3433

3534
for (String videoId : videoIds) {
3635
try {
37-
TranscriptList transcriptList;
38-
if (cookiesPath != null) {
39-
transcriptList = youtubeTranscriptApi.listTranscriptsWithCookies(videoId, cookiesPath);
40-
} else {
41-
transcriptList = youtubeTranscriptApi.listTranscripts(videoId);
42-
}
36+
TranscriptList transcriptList = getTranscriptList(videoId, cookiesPath);
4337
transcriptLists.put(videoId, transcriptList);
4438
} catch (TranscriptRetrievalException e) {
45-
if (continueOnError) continue;
46-
throw e;
39+
if (!continueOnError) throw e;
4740
}
4841
}
4942

5043
return transcriptLists;
5144
}
5245

46+
private TranscriptList getTranscriptList(String videoId, String cookiesPath) throws TranscriptRetrievalException {
47+
if (cookiesPath != null) {
48+
return youtubeTranscriptApi.listTranscriptsWithCookies(videoId, cookiesPath);
49+
}
50+
return youtubeTranscriptApi.listTranscripts(videoId);
51+
}
52+
5353
@Override
5454
public Map<String, TranscriptList> listTranscriptsForPlaylist(String playlistId, String apiKey, boolean continueOnError) throws TranscriptRetrievalException {
5555
return listTranscriptsForPlaylist(playlistId, apiKey, null, continueOnError);
@@ -69,39 +69,33 @@ public Map<String, TranscriptList> listTranscriptsForChannel(String channelName,
6969

7070

7171
private String getChannelPlaylistId(String channelId, String apiKey) throws TranscriptRetrievalException {
72-
HashMap<String, String> params = new HashMap<>(4);
73-
params.put("key", apiKey);
74-
params.put("part", "contentDetails");
75-
params.put("id", channelId);
76-
72+
Map<String, String> params = createParams(
73+
"key", apiKey,
74+
"part", "contentDetails",
75+
"id", channelId
76+
);
7777
String channelJson = client.get(CHANNELS, params);
7878

79-
JsonNode jsonNode;
80-
try {
81-
jsonNode = objectMapper.readTree(channelJson);
82-
} catch (JsonProcessingException e) {
83-
throw new TranscriptRetrievalException("Could not parse channel JSON for the channel with id: " + channelId, e);
84-
}
79+
JsonNode jsonNode = parseJson(channelJson,
80+
"Could not parse channel JSON for the channel with id: " + channelId);
81+
8582
JsonNode channel = jsonNode.get("items").get(0);
8683

8784
return channel.get("contentDetails").get("relatedPlaylists").get("uploads").asText();
8885
}
8986

9087
private String getChannelId(String channelName, String apiKey) throws TranscriptRetrievalException {
91-
Map<String, String> params = new HashMap<>(4);
92-
params.put("key", apiKey);
93-
params.put("q", channelName);
94-
params.put("part", "snippet");
95-
params.put("type", "channel");
88+
Map<String, String> params = createParams(
89+
"key", apiKey,
90+
"q", channelName,
91+
"part", "snippet",
92+
"type", "channel"
93+
);
9694

9795
String searchJson = client.get(SEARCH, params);
9896

99-
JsonNode jsonNode;
100-
try {
101-
jsonNode = objectMapper.readTree(searchJson);
102-
} catch (JsonProcessingException e) {
103-
throw new TranscriptRetrievalException("Could not parse search JSON for the channel: " + channelName, e);
104-
}
97+
JsonNode jsonNode = parseJson(searchJson,
98+
"Could not parse search JSON for the channel: " + channelName);
10599

106100
for (JsonNode item : jsonNode.get("items")) {
107101
JsonNode snippet = item.get("snippet");
@@ -115,39 +109,57 @@ private String getChannelId(String channelName, String apiKey) throws Transcript
115109

116110

117111
private List<String> getVideoIds(String playlistId, String apiKey) throws TranscriptRetrievalException {
118-
Map<String, String> params = new HashMap<>(5);
119-
params.put("key", apiKey);
120-
params.put("maxResults", MAX_RESULTS);
121-
params.put("playlistId", playlistId);
122-
params.put("part", "snippet");
112+
Map<String, String> params = createParams(
113+
"key", apiKey,
114+
"playlistId", playlistId,
115+
"part", "snippet",
116+
"maxResults", "50"
117+
);
123118

124119
List<String> videoIds = new ArrayList<>();
120+
125121
while (true) {
126122
String playlistJson = client.get(PLAYLIST_ITEMS, params);
127123

128-
JsonNode jsonNode;
129-
try {
130-
jsonNode = objectMapper.readTree(playlistJson);
131-
} catch (JsonProcessingException e) {
132-
throw new TranscriptRetrievalException("Could not parse playlist JSON for the playlist: " + playlistId, e);
133-
}
124+
JsonNode jsonNode = parseJson(playlistJson,
125+
"Could not parse playlist JSON for the playlist: " + playlistId);
134126

135-
jsonNode.get("items").forEach(item -> {
136-
String videoId = item.get("snippet")
137-
.get("resourceId")
138-
.get("videoId")
139-
.asText();
140-
videoIds.add(videoId);
141-
});
127+
extractVideoId(jsonNode, videoIds);
142128

143129
JsonNode nextPageToken = jsonNode.get("nextPageToken");
144130
if (nextPageToken == null) {
145131
break;
146132
}
133+
147134
params.put("pageToken", nextPageToken.asText());
148135
}
149136

150137
return videoIds;
151138
}
152139

140+
private void extractVideoId(JsonNode jsonNode, List<String> videoIds) {
141+
jsonNode.get("items").forEach(item -> {
142+
String videoId = item.get("snippet")
143+
.get("resourceId")
144+
.get("videoId")
145+
.asText();
146+
videoIds.add(videoId);
147+
});
148+
}
149+
150+
private Map<String, String> createParams(String... params) {
151+
Map<String, String> map = new HashMap<>(params.length / 2);
152+
for (int i = 0; i < params.length; i += 2) {
153+
map.put(params[i], params[i + 1]);
154+
}
155+
return map;
156+
}
157+
158+
private JsonNode parseJson(String json, String errorMessage) throws TranscriptRetrievalException {
159+
try {
160+
return objectMapper.readTree(json);
161+
} catch (JsonProcessingException e) {
162+
throw new TranscriptRetrievalException(errorMessage, e);
163+
}
164+
}
153165
}

0 commit comments

Comments
 (0)