Skip to content

Commit 8838e2d

Browse files
authored
Merge pull request #261 from TeamNewPipe/yt_new
Update YouTube to material version
2 parents a63371a + 96285e0 commit 8838e2d

15 files changed

Lines changed: 849 additions & 1014 deletions

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

Lines changed: 112 additions & 103 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.schabi.newpipe.extractor.services.youtube.extractors;
22

3-
import org.jsoup.nodes.Element;
3+
import com.grack.nanojson.JsonObject;
4+
45
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
56
import org.schabi.newpipe.extractor.exceptions.ParsingException;
7+
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
68
import org.schabi.newpipe.extractor.utils.Utils;
79

8-
import java.util.regex.Matcher;
9-
import java.util.regex.Pattern;
10+
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
11+
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
1012

1113
/*
1214
* Created by Christian Schabesberger on 12.02.17.
@@ -29,87 +31,75 @@
2931
*/
3032

3133
public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor {
32-
private final Element el;
34+
private JsonObject channelInfoItem;
3335

34-
public YoutubeChannelInfoItemExtractor(Element el) {
35-
this.el = el;
36+
public YoutubeChannelInfoItemExtractor(JsonObject channelInfoItem) {
37+
this.channelInfoItem = channelInfoItem;
3638
}
3739

3840
@Override
3941
public String getThumbnailUrl() throws ParsingException {
40-
Element img = el.select("span[class*=\"yt-thumb-simple\"]").first()
41-
.select("img").first();
42-
43-
String url = img.attr("abs:src");
44-
45-
if (url.contains("gif")) {
46-
url = img.attr("abs:data-thumb");
42+
try {
43+
String url = channelInfoItem.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
44+
if (url.startsWith("//")) {
45+
url = url.substring(2);
46+
}
47+
if (url.startsWith(HTTP)) {
48+
url = Utils.replaceHttpWithHttps(url);
49+
} else if (!url.startsWith(HTTPS)) {
50+
url = HTTPS + url;
51+
}
52+
return url;
53+
} catch (Exception e) {
54+
throw new ParsingException("Could not get thumbnail url", e);
4755
}
48-
return url;
4956
}
5057

5158
@Override
5259
public String getName() throws ParsingException {
53-
return el.select("a[class*=\"yt-uix-tile-link\"]").first()
54-
.text();
60+
try {
61+
return channelInfoItem.getObject("title").getString("simpleText");
62+
} catch (Exception e) {
63+
throw new ParsingException("Could not get name", e);
64+
}
5565
}
5666

5767
@Override
5868
public String getUrl() throws ParsingException {
5969
try {
60-
String buttonTrackingUrl = el.select("button[class*=\"yt-uix-button\"]").first()
61-
.attr("abs:data-href");
62-
63-
Pattern channelIdPattern = Pattern.compile("(?:.*?)\\%252Fchannel\\%252F([A-Za-z0-9\\-\\_]+)(?:.*)");
64-
Matcher match = channelIdPattern.matcher(buttonTrackingUrl);
65-
66-
if (match.matches()) {
67-
return YoutubeChannelExtractor.CHANNEL_URL_BASE + match.group(1);
68-
}
69-
} catch(Exception ignored) {}
70-
71-
// fallback method for channels without "Subscribe" button (or just in case yt changes things)
72-
// provides an url with "/user/NAME", inconsistent with stream and channel extractor: tests will fail
73-
try {
74-
return el.select("a[class*=\"yt-uix-tile-link\"]").first()
75-
.attr("abs:href");
70+
String id = "channel/" + channelInfoItem.getString("channelId"); // Does prepending 'channel/' always work?
71+
return YoutubeChannelLinkHandlerFactory.getInstance().getUrl(id);
7672
} catch (Exception e) {
77-
throw new ParsingException("Could not get channel url", e);
73+
throw new ParsingException("Could not get url", e);
7874
}
7975
}
8076

8177
@Override
8278
public long getSubscriberCount() throws ParsingException {
83-
final Element subsEl = el.select("span[class*=\"yt-subscriber-count\"]").first();
84-
if (subsEl != null) {
85-
try {
86-
return Long.parseLong(Utils.removeNonDigitCharacters(subsEl.text()));
87-
} catch (NumberFormatException e) {
88-
throw new ParsingException("Could not get subscriber count", e);
89-
}
90-
} else {
91-
// If the element is null, the channel have the subscriber count disabled
92-
return -1;
79+
try {
80+
String subscribers = channelInfoItem.getObject("subscriberCountText").getString("simpleText").split(" ")[0];
81+
return Utils.mixedNumberWordToLong(subscribers);
82+
} catch (Exception e) {
83+
throw new ParsingException("Could not get subscriber count", e);
9384
}
9485
}
9586

9687
@Override
9788
public long getStreamCount() throws ParsingException {
98-
Element metaEl = el.select("ul[class*=\"yt-lockup-meta-info\"]").first();
99-
if (metaEl == null) {
100-
return 0;
101-
} else {
102-
return Long.parseLong(Utils.removeNonDigitCharacters(metaEl.text()));
89+
try {
90+
return Long.parseLong(Utils.removeNonDigitCharacters(channelInfoItem.getObject("videoCountText")
91+
.getArray("runs").getObject(0).getString("text")));
92+
} catch (Exception e) {
93+
throw new ParsingException("Could not get stream count", e);
10394
}
10495
}
10596

10697
@Override
10798
public String getDescription() throws ParsingException {
108-
Element desEl = el.select("div[class*=\"yt-lockup-description\"]").first();
109-
if (desEl == null) {
110-
return "";
111-
} else {
112-
return desEl.text();
99+
try {
100+
return channelInfoItem.getObject("descriptionSnippet").getArray("runs").getObject(0).getString("text");
101+
} catch (Exception e) {
102+
throw new ParsingException("Could not get description", e);
113103
}
114104
}
115105
}

0 commit comments

Comments
 (0)