Skip to content

Commit f498d68

Browse files
committed
Add youtube-dl out-of-date error message
1 parent 2aed559 commit f498d68

File tree

1 file changed

+80
-66
lines changed

1 file changed

+80
-66
lines changed

src/com/joelchristophel/sourceradio/Song.java

Lines changed: 80 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -116,63 +116,72 @@ class Song {
116116
* - indicates whether or not to use cached data in constructing the song
117117
* @return the newly created <code>Song</code>
118118
*/
119-
static Song createSong(String query, Player requester, boolean useCachedData) throws IOException {
119+
static Song createSong(String query, Player requester, boolean useCachedData) {
120120
Song song = null;
121-
if (query == null || query.isEmpty()) {
122-
song = new Song(null, null, null, -1, query, requester, false);
123-
} else {
124-
String[] songData = useCachedData ? database.getSongDataFromQuery(query) : null;
125-
String cachedAudioPath = null;
126-
String youtubeId = null;
127-
if (songData != null) {
128-
youtubeId = songData[1];
129-
cachedAudioPath = getCachedPath(youtubeId);
130-
}
131-
if (cachedAudioPath == null) {
132-
boolean usedCachedQuery = false;
133-
if (youtubeId == null) {
134-
youtubeId = getYoutubeVideo(query);
135-
} else {
136-
usedCachedQuery = true;
121+
try {
122+
if (query == null || query.isEmpty()) {
123+
song = new Song(null, null, null, -1, query, requester, false);
124+
} else {
125+
String[] songData = useCachedData ? database.getSongDataFromQuery(query) : null;
126+
String cachedAudioPath = null;
127+
String youtubeId = null;
128+
if (songData != null) {
129+
youtubeId = songData[1];
130+
cachedAudioPath = getCachedPath(youtubeId);
137131
}
138-
if (youtubeId == null) {
139-
song = new Song(null, null, null, -1, query, requester, false);
140-
} else {
141-
String youtubeUrl = " http://www.youtube.com/watch?v=" + youtubeId;
142-
String format = " -f bestaudio[ext!=webm] ";
143-
String command = "\"" + YOUTUBEDL_PATH + "\"" + " -e -g --get-duration -x" + format + youtubeUrl;
144-
Process process = null;
145-
try {
146-
process = Runtime.getRuntime().exec(command);
147-
} catch (Exception e) {
148-
e.printStackTrace();
132+
if (cachedAudioPath == null) {
133+
boolean usedCachedQuery = false;
134+
if (youtubeId == null) {
135+
youtubeId = getYoutubeVideo(query);
136+
} else {
137+
usedCachedQuery = true;
149138
}
139+
if (youtubeId == null) {
140+
song = new Song(null, null, null, -1, query, requester, false);
141+
} else {
142+
String youtubeUrl = " http://www.youtube.com/watch?v=" + youtubeId;
143+
String format = " -f bestaudio[ext!=webm] ";
144+
String command = "\"" + YOUTUBEDL_PATH + "\"" + " -e -g --get-duration -x" + format
145+
+ youtubeUrl;
146+
Process process = null;
147+
try {
148+
process = Runtime.getRuntime().exec(command);
149+
} catch (Exception e) {
150+
e.printStackTrace();
151+
}
150152

151-
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
152-
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
153-
154-
String title = stdInput.readLine();
155-
String streamUrl = stdInput.readLine();
153+
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
154+
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
156155

157-
if (title == null || streamUrl == null) {
158-
song = null;
159-
} else {
160-
int duration = stringToSeconds(stdInput.readLine());
161-
song = new Song(title, streamUrl, youtubeId, duration, query, requester, usedCachedQuery);
162-
}
156+
String title = stdInput.readLine();
157+
String streamUrl = stdInput.readLine();
163158

164-
String error = null;
165-
while ((error = stdError.readLine()) != null) {
166-
if (error.contains("Unable to download webpage")) {
167-
throw new UnknownHostException(error);
159+
if (title == null || streamUrl == null) {
160+
song = null;
168161
} else {
169-
throw new RuntimeException(error);
162+
int duration = stringToSeconds(stdInput.readLine());
163+
song = new Song(title, streamUrl, youtubeId, duration, query, requester, usedCachedQuery);
164+
}
165+
166+
String error = null;
167+
while ((error = stdError.readLine()) != null) {
168+
if (error.contains("Unable to download webpage")) {
169+
new UnknownHostException(error).printStackTrace();
170+
} else if (error.contains("extraction")) {
171+
String message = "youtube-dl is out of date. Try again shortly.";
172+
new IOException(message).printStackTrace();
173+
} else {
174+
new IOException(error).printStackTrace();
175+
}
170176
}
171177
}
178+
} else {
179+
song = new Song(songData[0], null, songData[1], Integer.parseInt(songData[2]), query, requester,
180+
true);
172181
}
173-
} else {
174-
song = new Song(songData[0], null, songData[1], Integer.parseInt(songData[2]), query, requester, true);
175182
}
183+
} catch (IOException e) {
184+
e.printStackTrace();
176185
}
177186
return song;
178187
}
@@ -282,30 +291,35 @@ void addSongListener(SongListener listener) {
282291
* @param query
283292
* - the argument of a song request
284293
* @return the YouTube ID of the video best matching the query
294+
* @throws IOException
285295
*/
286296
private static String getYoutubeVideo(String query) throws IOException {
287-
String videoId = getYoutubeIdFromUrl(query);
288-
if (videoId == null) {
289-
YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(),
290-
new HttpRequestInitializer() {
291-
public void initialize(HttpRequest request) throws IOException {
292-
}
293-
}).setApplicationName("sourceradio").build();
294-
YouTube.Search.List search = youtube.search().list("id");
295-
search.setKey(properties.get("youtube key"));
296-
search.setQ(query);
297-
search.setType("video");
298-
search.setFields("items(id)");
299-
search.setMaxResults(1L);
300-
301-
SearchListResponse searchResponse = search.execute();
302-
List<SearchResult> searchResultList = searchResponse.getItems();
303-
304-
if (searchResultList != null && searchResultList.size() > 0) {
305-
videoId = searchResultList.get(0).getId().getVideoId();
297+
try {
298+
String videoId = getYoutubeIdFromUrl(query);
299+
if (videoId == null) {
300+
YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(),
301+
new HttpRequestInitializer() {
302+
public void initialize(HttpRequest request) throws IOException {
303+
}
304+
}).setApplicationName("sourceradio").build();
305+
YouTube.Search.List search = youtube.search().list("id");
306+
search.setKey(properties.get("youtube key"));
307+
search.setQ(query);
308+
search.setType("video");
309+
search.setFields("items(id)");
310+
search.setMaxResults(1L);
311+
312+
SearchListResponse searchResponse = search.execute();
313+
List<SearchResult> searchResultList = searchResponse.getItems();
314+
315+
if (searchResultList != null && searchResultList.size() > 0) {
316+
videoId = searchResultList.get(0).getId().getVideoId();
317+
}
306318
}
319+
return videoId;
320+
} catch (IOException e) {
321+
throw new IOException("Error: Failed to find YouTube video.", e);
307322
}
308-
return videoId;
309323
}
310324

311325
private static String getYoutubeIdFromUrl(String text) {

0 commit comments

Comments
 (0)