Skip to content

Commit 7d289a6

Browse files
committed
more comments, unit testing, some corrections
1 parent e175403 commit 7d289a6

4 files changed

Lines changed: 286 additions & 67 deletions

File tree

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

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.sapher.youtubedl;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.sapher.youtubedl.mapper.VideoFormat;
8+
import com.sapher.youtubedl.mapper.VideoInfo;
9+
import com.sapher.youtubedl.mapper.VideoThumbnail;
10+
11+
/**
12+
* @author memento
13+
* This class is child class from YoutubeDL that allows to use youtube-dl behind a Tor "Proxychains" software to avoid BAN for example.
14+
*
15+
* Test class: YoutubeDLProxychainsTest
16+
*
17+
* Before using this class, be sure you have installed proxychains on your machine
18+
* If you're using debian/ubuntu, install this powerful tool by typing this in your terminal :
19+
* > sudo apt-get install proxychains tor obfsproxy
20+
*
21+
* (Other linux distributions : https://www.linuxsecrets.com/3372-install-setup-proxychains-on-linux )
22+
*
23+
* Normally, to use youtube-dl behind proxychains, you'd execute (example) :
24+
* > proxychains youtube-dl https://www.youtube.com/watch?v=nMfPqeZjc2c
25+
*
26+
* pros : you stay anonymous and you avoid being ban
27+
* cons : the queries take longer
28+
*
29+
*/
30+
public class YoutubeDLProxychains extends YoutubeDL{
31+
32+
/**
33+
* Execute youtube-dl request
34+
* @param request request object
35+
* @return response object
36+
* @throws YoutubeDLException
37+
*/
38+
public static YoutubeDLResponse execute(YoutubeDLRequest request) throws YoutubeDLException {
39+
//We use proxychains, here
40+
setExecutablePath("proxychains youtube-dl");
41+
return execute(request, null);
42+
}
43+
44+
/**
45+
* Retrieve all information available on a video
46+
* @param url Video url
47+
* @return Video info
48+
* @throws YoutubeDLException
49+
*/
50+
public static VideoInfo getVideoInfo(String url) throws YoutubeDLException {
51+
//We use proxychains, here
52+
setExecutablePath("proxychains youtube-dl");
53+
54+
// Build request
55+
YoutubeDLRequest request = new YoutubeDLRequest(url);
56+
request.setOption("dump-json");
57+
request.setOption("no-playlist");
58+
YoutubeDLResponse response = execute(request);
59+
60+
// Parse result
61+
ObjectMapper objectMapper = new ObjectMapper();
62+
VideoInfo videoInfo;
63+
64+
//Proxychains : remove proxychains header ex: "ProxyChains-3.1 (http://proxychains.sf.net)\n"
65+
String output = response.getOut();
66+
output = output.substring(output.indexOf("{"));
67+
68+
try {
69+
videoInfo = objectMapper.readValue(output, VideoInfo.class);
70+
} catch (IOException e) {
71+
throw new YoutubeDLException("Unable to parse video information: " + e.getMessage());
72+
}
73+
74+
return videoInfo;
75+
}
76+
77+
/**
78+
* List formats
79+
* @param url Video url
80+
* @return list of formats
81+
* @throws YoutubeDLException
82+
*/
83+
public static List<VideoFormat> getFormats(String url) throws YoutubeDLException {
84+
//We use proxychains, here
85+
setExecutablePath("proxychains youtube-dl");
86+
87+
VideoInfo info = getVideoInfo(url);
88+
return info.formats;
89+
}
90+
91+
/**
92+
* List thumbnails
93+
* @param url Video url
94+
* @return list of thumbnail
95+
* @throws YoutubeDLException
96+
*/
97+
public static List<VideoThumbnail> getThumbnails(String url) throws YoutubeDLException {
98+
//We use proxychains, here
99+
setExecutablePath("proxychains youtube-dl");
100+
101+
VideoInfo info = getVideoInfo(url);
102+
return info.thumbnails;
103+
}
104+
105+
/**
106+
* List categories
107+
* @param url Video url
108+
* @return list of category
109+
* @throws YoutubeDLException
110+
*/
111+
public static List<String> getCategories(String url) throws YoutubeDLException {
112+
//We use proxychains, here
113+
setExecutablePath("proxychains youtube-dl");
114+
115+
VideoInfo info = getVideoInfo(url);
116+
return info.categories;
117+
}
118+
119+
/**
120+
* List tags
121+
* @param url Video url
122+
* @return list of tag
123+
* @throws YoutubeDLException
124+
*/
125+
public static List<String> getTags(String url) throws YoutubeDLException {
126+
//We use proxychains, here
127+
setExecutablePath("proxychains youtube-dl");
128+
129+
VideoInfo info = getVideoInfo(url);
130+
return info.tags;
131+
}
132+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.sapher.youtubedl;
2+
3+
import com.sapher.youtubedl.mapper.VideoFormat;
4+
import com.sapher.youtubedl.mapper.VideoInfo;
5+
import com.sapher.youtubedl.mapper.VideoThumbnail;
6+
import com.sapher.youtubedl.YoutubeDLProxychains;
7+
import org.junit.Test;
8+
import org.junit.Assert;
9+
10+
import java.util.List;
11+
12+
public class YoutubeDLProxychainsTest{
13+
14+
private final static String DIRECTORY = System.getProperty("java.io.tmpdir");
15+
private final static String VIDEO_URL = "https://www.youtube.com/watch?v=nMfPqeZjc2c";
16+
private final static String NONE_EXISTENT_VIDEO_URL = "https://www.youtube.com/watch?v=dQw4w9WgXcZ";
17+
18+
/**@Test
19+
public void testUsingOwnExecutablePath() throws YoutubeDLException {
20+
YoutubeDLProxychains.setExecutablePath("/usr/bin/youtube-dl");
21+
Assert.assertNotNull(YoutubeDLProxychains.getVersion());
22+
}**/
23+
24+
@Test
25+
public void testGetVersion() throws YoutubeDLException {
26+
Assert.assertNotNull(YoutubeDLProxychains.getVersion());
27+
}
28+
29+
@Test
30+
public void testElapsedTime() throws YoutubeDLException {
31+
32+
long startTime = System.nanoTime();
33+
34+
YoutubeDLRequest request = new YoutubeDLRequest();
35+
request.setOption("version");
36+
YoutubeDLResponse response = YoutubeDLProxychains.execute(request);
37+
38+
int elapsedTime = (int) (System.nanoTime() - startTime);
39+
40+
Assert.assertTrue(elapsedTime > response.getElapsedTime());
41+
}
42+
43+
44+
@Test
45+
public void testSimulateDownload() throws YoutubeDLException {
46+
47+
YoutubeDLRequest request = new YoutubeDLRequest();
48+
request.setUrl(VIDEO_URL);
49+
request.setOption("simulate");
50+
51+
YoutubeDLResponse response = YoutubeDLProxychains.execute(request);
52+
System.out.println(response.getCommand());
53+
Assert.assertEquals("proxychains youtube-dl " + VIDEO_URL + " --simulate", response.getCommand());
54+
}
55+
56+
@Test
57+
public void testDirectory() throws YoutubeDLException {
58+
59+
YoutubeDLRequest request = new YoutubeDLRequest(VIDEO_URL, DIRECTORY);
60+
request.setOption("simulate");
61+
62+
YoutubeDLResponse response = YoutubeDLProxychains.execute(request);
63+
64+
Assert.assertEquals(DIRECTORY, response.getDirectory());
65+
}
66+
67+
@Test
68+
public void testGetVideoInfo() throws YoutubeDLException {
69+
VideoInfo videoInfo = YoutubeDLProxychains.getVideoInfo(VIDEO_URL);
70+
Assert.assertNotNull(videoInfo);
71+
}
72+
73+
@Test
74+
public void testGetVideoInfoInDetails() throws YoutubeDLException {
75+
VideoInfo videoInfo = YoutubeDLProxychains.getVideoInfo(VIDEO_URL);
76+
Assert.assertNotNull(videoInfo);
77+
//Let's check we can access key elements from video info
78+
Assert.assertNotNull(videoInfo.getId());
79+
Assert.assertNotNull(videoInfo.getTitle());
80+
Assert.assertNotNull(videoInfo.getFulltitle());
81+
Assert.assertNotNull(videoInfo.getDescription());
82+
Assert.assertNotNull(videoInfo.getThumbnail());
83+
Assert.assertNotNull(videoInfo.getUploaderId());
84+
Assert.assertNotNull(videoInfo.getUploader());
85+
Assert.assertNotNull(videoInfo.getUploadDate());
86+
Assert.assertNotNull(videoInfo.getDuration());
87+
Assert.assertNotNull(videoInfo.getViewCount());
88+
Assert.assertNotNull(videoInfo.getLikeCount());
89+
Assert.assertNotNull(videoInfo.getDislikeCount());
90+
Assert.assertNotNull(videoInfo.getAverageRating());
91+
//Let's print the whole object
92+
System.out.println("videoInfo:"+videoInfo.toString());
93+
94+
95+
}
96+
97+
@Test
98+
public void testGetVideoInfoInDetailsWithProxychains() throws YoutubeDLException {
99+
VideoInfo videoInfo = YoutubeDLProxychains.getVideoInfo(VIDEO_URL);
100+
Assert.assertNotNull(videoInfo);
101+
//Let's check we can access key elements from video info
102+
Assert.assertNotNull(videoInfo.getId());
103+
Assert.assertNotNull(videoInfo.getTitle());
104+
Assert.assertNotNull(videoInfo.getFulltitle());
105+
Assert.assertNotNull(videoInfo.getDescription());
106+
Assert.assertNotNull(videoInfo.getThumbnail());
107+
Assert.assertNotNull(videoInfo.getUploaderId());
108+
Assert.assertNotNull(videoInfo.getUploader());
109+
Assert.assertNotNull(videoInfo.getUploadDate());
110+
Assert.assertNotNull(videoInfo.getDuration());
111+
Assert.assertNotNull(videoInfo.getViewCount());
112+
Assert.assertNotNull(videoInfo.getLikeCount());
113+
Assert.assertNotNull(videoInfo.getDislikeCount());
114+
Assert.assertNotNull(videoInfo.getAverageRating());
115+
//Let's print the whole object
116+
System.out.println("videoInfo:"+videoInfo.toString());
117+
118+
119+
}
120+
121+
@Test
122+
public void testGetFormats() throws YoutubeDLException {
123+
List<VideoFormat> formats = YoutubeDLProxychains.getFormats(VIDEO_URL);
124+
System.out.println("formats : "+formats);
125+
Assert.assertNotNull(formats);
126+
Assert.assertTrue(formats.size() > 0);
127+
}
128+
129+
@Test
130+
public void testGetThumbnails() throws YoutubeDLException {
131+
List<VideoThumbnail> thumbnails = YoutubeDLProxychains.getThumbnails(VIDEO_URL);
132+
Assert.assertNotNull(thumbnails);
133+
Assert.assertTrue(thumbnails.size() > 0);
134+
}
135+
136+
@Test
137+
public void testGetTags() throws YoutubeDLException {
138+
List<String> tags = YoutubeDLProxychains.getTags(VIDEO_URL);
139+
Assert.assertNotNull(tags);
140+
Assert.assertTrue(tags.size() > 0);
141+
}
142+
143+
@Test
144+
public void testGetCategories() throws YoutubeDLException {
145+
List<String> categories = YoutubeDLProxychains.getCategories(VIDEO_URL);
146+
Assert.assertNotNull(categories);
147+
Assert.assertTrue(categories.size() > 0);
148+
}
149+
150+
@Test(expected = YoutubeDLException.class)
151+
public void testFailGetNonExistentVideoInfo() throws YoutubeDLException {
152+
YoutubeDLProxychains.getVideoInfo(NONE_EXISTENT_VIDEO_URL);
153+
}
154+
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,6 @@ public void testGetVideoInfoInDetails() throws YoutubeDLException {
9191
System.out.println("videoInfo:"+videoInfo.toString());
9292

9393

94-
}
95-
96-
@Test
97-
public void testGetVideoInfoInDetailsWithProxychains() throws YoutubeDLException {
98-
VideoInfo videoInfo = YoutubeDLProchains.getVideoInfo(VIDEO_URL);
99-
Assert.assertNotNull(videoInfo);
100-
//Let's check we can access key elements from video info
101-
Assert.assertNotNull(videoInfo.getId());
102-
Assert.assertNotNull(videoInfo.getTitle());
103-
Assert.assertNotNull(videoInfo.getFulltitle());
104-
Assert.assertNotNull(videoInfo.getDescription());
105-
Assert.assertNotNull(videoInfo.getThumbnail());
106-
Assert.assertNotNull(videoInfo.getUploaderId());
107-
Assert.assertNotNull(videoInfo.getUploader());
108-
Assert.assertNotNull(videoInfo.getUploadDate());
109-
Assert.assertNotNull(videoInfo.getDuration());
110-
Assert.assertNotNull(videoInfo.getViewCount());
111-
Assert.assertNotNull(videoInfo.getLikeCount());
112-
Assert.assertNotNull(videoInfo.getDislikeCount());
113-
Assert.assertNotNull(videoInfo.getAverageRating());
114-
//Let's print the whole object
115-
System.out.println("videoInfo:"+videoInfo.toString());
116-
117-
11894
}
11995

12096
@Test

0 commit comments

Comments
 (0)