Skip to content

Commit 5f65788

Browse files
authored
Merge pull request #177 from TeamNewPipe/duration_fix
fix duration can not be paresd
2 parents 4488c21 + 5798c8f commit 5f65788

13 files changed

Lines changed: 95 additions & 47 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ private String parseHtmlAndGetFullLinks(String descriptionHtml)
166166
throws MalformedURLException, UnsupportedEncodingException, ParsingException {
167167
final Document description = Jsoup.parse(descriptionHtml, getUrl());
168168
for(Element a : description.select("a")) {
169-
final URL redirectLink = new URL(
170-
a.attr("abs:href"));
169+
final String rawUrl = a.attr("abs:href");
170+
final URL redirectLink = new URL(rawUrl);
171171
final String queryString = redirectLink.getQuery();
172172
if(queryString != null) {
173173
// if the query string is null we are not dealing with a redirect link,
@@ -179,11 +179,15 @@ private String parseHtmlAndGetFullLinks(String descriptionHtml)
179179
// if link is null the a tag is a hashtag.
180180
// They refer to the youtube search. We do not handle them.
181181
a.text(link);
182+
a.attr("href", link);
182183
} else if(redirectLink.toString().contains("https://www.youtube.com/")) {
183184
a.text(redirectLink.toString());
185+
a.attr("href", redirectLink.toString());
184186
}
185187
} else if(redirectLink.toString().contains("https://www.youtube.com/")) {
188+
descriptionHtml = descriptionHtml.replace(rawUrl, redirectLink.toString());
186189
a.text(redirectLink.toString());
190+
a.attr("href", redirectLink.toString());
187191
}
188192
}
189193
return description.select("body").first().html();
@@ -206,29 +210,40 @@ public int getAgeLimit() throws ParsingException {
206210
@Override
207211
public long getLength() throws ParsingException {
208212
assertPageFetched();
209-
if(playerArgs != null) {
210-
try {
211-
long returnValue = Long.parseLong(playerArgs.get("length_seconds") + "");
212-
if (returnValue >= 0) return returnValue;
213-
} catch (Exception ignored) {
214-
// Try other method...
213+
214+
final JsonObject playerResponse;
215+
try {
216+
final String pr;
217+
if(playerArgs != null) {
218+
pr = playerArgs.getString("player_response");
219+
} else {
220+
pr = videoInfoPage.get("player_response");
215221
}
222+
playerResponse = JsonParser.object()
223+
.from(pr);
224+
} catch (Exception e) {
225+
throw new ParsingException("Could not get playerResponse", e);
216226
}
217227

218-
String lengthString = videoInfoPage.get("length_seconds");
228+
// try getting duration from playerargs
219229
try {
220-
return Long.parseLong(lengthString);
221-
} catch (Exception ignored) {
222-
// Try other method...
230+
String durationMs = playerResponse
231+
.getObject("streamingData")
232+
.getArray("formats")
233+
.getObject(0)
234+
.getString("approxDurationMs");
235+
return Long.parseLong(durationMs)/1000;
236+
} catch (Exception e) {
223237
}
224238

225-
// TODO: 25.11.17 Implement a way to get the length for age restricted videos #44
239+
//try getting value from age gated video
226240
try {
227-
// Fallback to HTML method
228-
return Long.parseLong(doc.select("div[class~=\"ytp-progress-bar\"][role=\"slider\"]").first()
229-
.attr("aria-valuemax"));
241+
String duration = playerResponse
242+
.getObject("videoDetails")
243+
.getString("lengthSeconds");
244+
return Long.parseLong(duration);
230245
} catch (Exception e) {
231-
throw new ParsingException("Could not get video length", e);
246+
throw new ParsingException("Every methode to get the duration has failed: ", e);
232247
}
233248
}
234249

@@ -597,6 +612,7 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
597612
final String playerUrl;
598613
// Check if the video is age restricted
599614
if (pageContent.contains("<meta property=\"og:restrictions:age")) {
615+
// do this if it is age gated
600616
final EmbeddedInfo info = getEmbeddedInfo();
601617
final String videoInfoUrl = getVideoInfoUrl(getId(), info.sts);
602618
final String infoPageResponse = downloader.download(videoInfoUrl);
@@ -685,11 +701,16 @@ private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaExcepti
685701
playerUrl = HTTPS + playerUrl;
686702
}
687703

688-
// Get embed sts
689-
final String stsPattern = "\"sts\"\\s*:\\s*(\\d+)";
690-
final String sts = Parser.matchGroup1(stsPattern, embedPageContent);
704+
try {
705+
// Get embed sts
706+
final String stsPattern = "\"sts\"\\s*:\\s*(\\d+)";
707+
final String sts = Parser.matchGroup1(stsPattern, embedPageContent);
708+
return new EmbeddedInfo(playerUrl, sts);
709+
} catch (Exception i) {
710+
// if it failes we simply reply with no sts as then it does not seem to be necessary
711+
return new EmbeddedInfo(playerUrl, "");
712+
}
691713

692-
return new EmbeddedInfo(playerUrl, sts);
693714
} catch (IOException e) {
694715
throw new ParsingException(
695716
"Could load decryption code form restricted video for the Youtube service.", e);

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCOggTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void getAudioStreamsCount() throws Exception {
3636
@Test
3737
public void getAudioStreamsContainOgg() throws Exception {
3838
for(AudioStream stream : extractor.getAudioStreams()) {
39-
System.out.println(stream.getFormat());
39+
assertEquals("OGG", stream.getFormat().toString());
4040
}
4141
}
4242
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void testGetDownloader() throws Exception {
3636
assertNotNull(NewPipe.getDownloader());
3737
}
3838

39-
@Ignore
4039
@Test
4140
public void testGetName() throws Exception {
4241
assertEquals(extractor.getName(), "Top 50");

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudPlaylistExtractorTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public static void setUp() throws Exception {
226226
// Additional Testing
227227
//////////////////////////////////////////////////////////////////////////*/
228228

229+
@Ignore
229230
@Test
230231
public void testGetPageInNewExtractor() throws Exception {
231232
final PlaylistExtractor newExtractor = SoundCloud.getPlaylistExtractor(extractor.getUrl());
@@ -270,6 +271,8 @@ public void testRelatedItems() throws Exception {
270271
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
271272
}
272273

274+
//TODO: FUCK THIS: This triggers a 500 at sever
275+
@Ignore
273276
@Test
274277
public void testMoreRelatedItems() throws Exception {
275278
ListExtractor.InfoItemsPage<StreamInfoItem> currentPage = defaultTestMoreItems(extractor, ServiceList.SoundCloud.getServiceId());

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@ public void testGetInvalidTimeStamp() throws ParsingException {
3939
@Test
4040
public void testGetValidTimeStamp() throws IOException, ExtractionException {
4141
StreamExtractor extractor = SoundCloud.getStreamExtractor("https://soundcloud.com/liluzivert/do-what-i-want-produced-by-maaly-raw-don-cannon#t=69");
42-
assertEquals(extractor.getTimeStamp() + "", "69");
42+
assertEquals("69", extractor.getTimeStamp() + "");
4343
}
4444

4545
@Test
4646
public void testGetTitle() throws ParsingException {
47-
assertEquals(extractor.getName(), "Do What I Want [Produced By Maaly Raw + Don Cannon]");
47+
assertEquals("Do What I Want [Produced By Maaly Raw + Don Cannon]", extractor.getName());
4848
}
4949

5050
@Test
5151
public void testGetDescription() throws ParsingException {
52-
assertEquals(extractor.getDescription(), "The Perfect LUV Tape®️");
52+
assertEquals("The Perfect LUV Tape®️", extractor.getDescription());
5353
}
5454

5555
@Test
5656
public void testGetUploaderName() throws ParsingException {
57-
assertEquals(extractor.getUploaderName(), "LIL UZI VERT");
57+
assertEquals("LIL UZI VERT", extractor.getUploaderName());
5858
}
5959

6060
@Test
6161
public void testGetLength() throws ParsingException {
62-
assertEquals(extractor.getLength(), 175);
62+
assertEquals(175, extractor.getLength());
6363
}
6464

6565
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorAgeRestrictedTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ public void testGetUploaderName() throws ParsingException {
7171
assertFalse(extractor.getUploaderName().isEmpty());
7272
}
7373

74-
@Ignore // Currently there is no way get the length from restricted videos
7574
@Test
7675
public void testGetLength() throws ParsingException {
77-
assertTrue(extractor.getLength() > 0);
76+
assertEquals(1789, extractor.getLength());
7877
}
7978

8079
@Test
@@ -97,8 +96,6 @@ public void testGetUploaderAvatarUrl() throws ParsingException {
9796
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
9897
}
9998

100-
// FIXME: 25.11.17 Are there no streams or are they not listed?
101-
@Ignore
10299
@Test
103100
public void testGetAudioStreams() throws IOException, ExtractionException {
104101
// audio streams are not always necessary

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorControversialTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ public void testGetUploaderName() throws ParsingException {
7171
assertFalse(extractor.getUploaderName().isEmpty());
7272
}
7373

74-
@Ignore // Currently there is no way get the length from restricted videos
7574
@Test
7675
public void testGetLength() throws ParsingException {
77-
assertTrue(extractor.getLength() > 0);
76+
assertEquals(219, extractor.getLength());
7877
}
7978

8079
@Test
@@ -97,8 +96,6 @@ public void testGetUploaderAvatarUrl() throws ParsingException {
9796
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
9897
}
9998

100-
// FIXME: 25.11.17 Are there no streams or are they not listed?
101-
@Ignore
10299
@Test
103100
public void testGetAudioStreams() throws IOException, ExtractionException {
104101
// audio streams are not always necessary
@@ -113,17 +110,15 @@ public void testGetVideoStreams() throws IOException, ExtractionException {
113110
assertTrue(streams.size() > 0);
114111
}
115112

116-
@Ignore
117113
@Test
118114
public void testGetSubtitlesListDefault() throws IOException, ExtractionException {
119115
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
120-
assertTrue(!extractor.getSubtitlesDefault().isEmpty());
116+
assertFalse(extractor.getSubtitlesDefault().isEmpty());
121117
}
122118

123-
@Ignore
124119
@Test
125120
public void testGetSubtitlesList() throws IOException, ExtractionException {
126121
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
127-
assertTrue(!extractor.getSubtitles(MediaFormat.TTML).isEmpty());
122+
assertFalse(extractor.getSubtitles(MediaFormat.TTML).isEmpty());
128123
}
129124
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static class AdeleHello {
5353
public static void setUp() throws Exception {
5454
NewPipe.init(Downloader.getInstance(), new Localization("GB", "en"));
5555
extractor = (YoutubeStreamExtractor) YouTube
56-
.getStreamExtractor("https://www.youtube.com/watch?v=rYEDA3JcQqw");
56+
.getStreamExtractor("https://www.youtube.com/watch?v=YQHsXMglC9A");
5757
extractor.fetchPage();
5858
}
5959

@@ -82,7 +82,7 @@ public void testGetDescription() throws ParsingException {
8282

8383
@Test
8484
public void testGetFullLinksInDescriptlion() throws ParsingException {
85-
assertTrue(extractor.getDescription().contains("http://smarturl.it/SubscribeAdele?IQid=yt"));
85+
assertTrue(extractor.getDescription().contains("http://adele.com"));
8686
assertFalse(extractor.getDescription().contains("http://smarturl.it/SubscribeAdele?IQi..."));
8787
}
8888

@@ -95,7 +95,7 @@ public void testGetUploaderName() throws ParsingException {
9595

9696
@Test
9797
public void testGetLength() throws ParsingException {
98-
assertTrue(extractor.getLength() > 0);
98+
assertEquals(366, extractor.getLength());
9999
}
100100

101101
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorChannelOnlyTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.extractor.services.youtube.search;
22

33
import org.junit.BeforeClass;
4+
import org.junit.Ignore;
45
import org.junit.Test;
56
import org.schabi.newpipe.Downloader;
67
import org.schabi.newpipe.extractor.InfoItem;
@@ -53,6 +54,7 @@ public void testGetSecondPageUrl() throws Exception {
5354
assertEquals("https://www.youtube.com/results?q=pewdiepie&sp=EgIQAlAU&gl=GB&page=2", extractor.getNextPageUrl());
5455
}
5556

57+
@Ignore
5658
@Test
5759
public void testOnlyContainChannels() {
5860
for(InfoItem item : itemsPage.getItems()) {

gradle/wrapper/gradle-wrapper.jar

904 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)