|
1 | 1 | package org.schabi.newpipe.extractor.services.youtube.extractors; |
2 | 2 |
|
3 | | -import org.jsoup.nodes.Element; |
| 3 | +import com.grack.nanojson.JsonObject; |
| 4 | + |
4 | 5 | import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor; |
5 | 6 | import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 7 | +import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory; |
6 | 8 | import org.schabi.newpipe.extractor.utils.Utils; |
7 | 9 |
|
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; |
10 | 12 |
|
11 | 13 | /* |
12 | 14 | * Created by Christian Schabesberger on 12.02.17. |
|
29 | 31 | */ |
30 | 32 |
|
31 | 33 | public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor { |
32 | | - private final Element el; |
| 34 | + private JsonObject channelInfoItem; |
33 | 35 |
|
34 | | - public YoutubeChannelInfoItemExtractor(Element el) { |
35 | | - this.el = el; |
| 36 | + public YoutubeChannelInfoItemExtractor(JsonObject channelInfoItem) { |
| 37 | + this.channelInfoItem = channelInfoItem; |
36 | 38 | } |
37 | 39 |
|
38 | 40 | @Override |
39 | 41 | 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); |
47 | 55 | } |
48 | | - return url; |
49 | 56 | } |
50 | 57 |
|
51 | 58 | @Override |
52 | 59 | 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 | + } |
55 | 65 | } |
56 | 66 |
|
57 | 67 | @Override |
58 | 68 | public String getUrl() throws ParsingException { |
59 | 69 | 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); |
76 | 72 | } catch (Exception e) { |
77 | | - throw new ParsingException("Could not get channel url", e); |
| 73 | + throw new ParsingException("Could not get url", e); |
78 | 74 | } |
79 | 75 | } |
80 | 76 |
|
81 | 77 | @Override |
82 | 78 | 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); |
93 | 84 | } |
94 | 85 | } |
95 | 86 |
|
96 | 87 | @Override |
97 | 88 | 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); |
103 | 94 | } |
104 | 95 | } |
105 | 96 |
|
106 | 97 | @Override |
107 | 98 | 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); |
113 | 103 | } |
114 | 104 | } |
115 | 105 | } |
0 commit comments