This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
ExtractorCase.java
139 lines (110 loc) · 5.4 KB
/
ExtractorCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package at.huber.youtubeExtractor;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.util.SparseArray;
public class ExtractorCase extends InstrumentationTestCase {
private static final String EXTRACTOR_TEST_TAG = "Extractor Test";
private String testUrl;
public void testUsualVideo() throws Throwable {
VideoMeta expMeta = new VideoMeta("YE7VzlLtp-4", "Big Buck Bunny", "Blender Foundation",
"UCSMOQeBJ2RAnuFungnQOxLg", 597, 0, false);
extractorTest("http://youtube.com/watch?v=YE7VzlLtp-4", expMeta);
extractorTestDashManifest("http://youtube.com/watch?v=YE7VzlLtp-4");
}
public void testEncipheredVideo() throws Throwable {
VideoMeta expMeta = new VideoMeta("e8X3ACToii0", "Rise Against - Savior", "RiseAgainstVEVO",
"UChMKB2AHNpeuWhalpRYhUaw", 244, 0, false);
extractorTest("https://www.youtube.com/watch?v=e8X3ACToii0", expMeta);
}
public void testAgeRestrictVideo() throws Throwable {
VideoMeta expMeta = new VideoMeta("61Ev-YvBw2c", "Test video for age-restriction",
"jpdemoA", "UC95NqtFsDZKlmzOJmZi_g6Q", 14, 0, false);
extractorTest("http://www.youtube.com/watch?v=61Ev-YvBw2c", expMeta);
extractorTestDashManifest("http://www.youtube.com/watch?v=61Ev-YvBw2c");
}
public void testLiveStream() throws Throwable {
VideoMeta expMeta = new VideoMeta("qzMQza8xZCc", "Nasa LIVE stream - Earth From Space LIVE Feed | Incredible ISS live stream of Earth from space",
"Space Videos", "UCakgsb0w7QB0VHdnCc-OVEA", 1800, 0, true);
extractorTest("http://www.youtube.com/watch?v=qzMQza8xZCc", expMeta);
}
private void extractorTestDashManifest(final String youtubeLink)
throws Throwable {
final CountDownLatch signal = new CountDownLatch(1);
YouTubeExtractor.LOGGING = true;
testUrl = null;
runTestOnUiThread(new Runnable() {
@Override
public void run() {
final YouTubeExtractor ytEx = new YouTubeExtractor(getInstrumentation()
.getTargetContext()) {
@Override
public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta videoMeta) {
assertNotNull(ytFiles);
int numNotDash = 0;
int itag;
for (int i = 0; i < ytFiles.size(); i++) {
itag = ytFiles.keyAt(i);
if (ytFiles.get(itag).getFormat().isDashContainer()) {
numNotDash = i;
break;
}
}
itag = ytFiles.keyAt(new Random().nextInt(ytFiles.size() - numNotDash) + numNotDash);
Log.d(EXTRACTOR_TEST_TAG, "Testing itag:" + itag);
testUrl = ytFiles.get(itag).getUrl();
signal.countDown();
}
};
ytEx.extract(youtubeLink, true, true);
}
});
signal.await(10, TimeUnit.SECONDS);
assertNotNull(testUrl);
final URL url = new URL(testUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int code = con.getResponseCode();
assertEquals(code, 200);
con.disconnect();
}
private void extractorTest(final String youtubeLink, final VideoMeta expMeta)
throws Throwable {
final CountDownLatch signal = new CountDownLatch(1);
YouTubeExtractor.LOGGING = true;
testUrl = null;
runTestOnUiThread(new Runnable() {
@Override
public void run() {
final YouTubeExtractor ytEx = new YouTubeExtractor(getInstrumentation()
.getTargetContext()) {
@Override
public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta videoMeta) {
assertEquals(expMeta.getVideoId(), videoMeta.getVideoId());
assertEquals(expMeta.getTitle(),videoMeta.getTitle());
assertEquals(expMeta.getAuthor(), videoMeta.getAuthor());
assertEquals(expMeta.getChannelId(), videoMeta.getChannelId());
assertEquals(expMeta.getVideoLength(), videoMeta.getVideoLength());
assertNotSame(0, videoMeta.getViewCount());
assertNotNull(ytFiles);
int itag = ytFiles.keyAt(new Random().nextInt(ytFiles.size()));
Log.d(EXTRACTOR_TEST_TAG, "Testing itag:" + itag);
testUrl = ytFiles.get(itag).getUrl();
signal.countDown();
}
};
ytEx.extract(youtubeLink, false, true);
}
});
signal.await(10, TimeUnit.SECONDS);
assertNotNull(testUrl);
final URL url = new URL(testUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int code = con.getResponseCode();
assertEquals(code, 200);
con.disconnect();
}
}