Skip to content

Commit e175403

Browse files
committed
Bless inheritance! Use YoutubeDL or YoutubeDLProchains, in a static way!
1 parent 7e39965 commit e175403

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

src/main/java/com/sapher/youtubedl/YoutubeDL.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,8 @@ public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
141141
ObjectMapper objectMapper = new ObjectMapper();
142142
VideoInfo videoInfo;
143143

144-
String output;
145-
if(executablePath.contains("proxychains")) {
146-
//Proxychains : remove proxychains header ex: "ProxyChains-3.1 (http://proxychains.sf.net)\n"
147-
output = response.getOut();
148-
output = output.substring(output.indexOf("{"));
149-
}else {
150-
output = response.getOut();
151-
}
152-
153-
154144
try {
155-
videoInfo = objectMapper.readValue(output, VideoInfo.class);
145+
videoInfo = objectMapper.readValue(response.getOut(), VideoInfo.class);
156146
} catch (IOException e) {
157147
throw new YoutubeDLException("Unable to parse video information: " + e.getMessage());
158148
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.sapher.youtubedl;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.sapher.youtubedl.mapper.VideoInfo;
7+
8+
public class YoutubeDLProchains extends YoutubeDL{
9+
10+
/**
11+
* Retrieve all information available on a video
12+
* @param url Video url
13+
* @return Video info
14+
* @throws YoutubeDLException
15+
*/
16+
public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
17+
18+
//We use proxychains, here
19+
setExecutablePath("proxychains youtube-dl");
20+
21+
// Build request
22+
YoutubeDLRequest request = new YoutubeDLRequest(url);
23+
request.setOption("dump-json");
24+
request.setOption("no-playlist");
25+
YoutubeDLResponse response = YoutubeDL.execute(request);
26+
27+
// Parse result
28+
ObjectMapper objectMapper = new ObjectMapper();
29+
VideoInfo videoInfo;
30+
31+
//Proxychains : remove proxychains header ex: "ProxyChains-3.1 (http://proxychains.sf.net)\n"
32+
String output = response.getOut();
33+
output = output.substring(output.indexOf("{"));
34+
35+
try {
36+
videoInfo = objectMapper.readValue(output, VideoInfo.class);
37+
} catch (IOException e) {
38+
throw new YoutubeDLException("Unable to parse video information: " + e.getMessage());
39+
}
40+
41+
return videoInfo;
42+
}
43+
}

src/test/java/com/sapher/youtubedl/YoutubeDLTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ public void testGetVideoInfoInDetails() throws YoutubeDLException {
9595

9696
@Test
9797
public void testGetVideoInfoInDetailsWithProxychains() throws YoutubeDLException {
98-
YoutubeDL youtubeDL = new YoutubeDL();
99-
youtubeDL.setExecutablePath("proxychains youtube-dl");
100-
101-
VideoInfo videoInfo = youtubeDL.getVideoInfo(VIDEO_URL);
98+
VideoInfo videoInfo = YoutubeDLProchains.getVideoInfo(VIDEO_URL);
10299
Assert.assertNotNull(videoInfo);
103100
//Let's check we can access key elements from video info
104101
Assert.assertNotNull(videoInfo.getId());

0 commit comments

Comments
 (0)