2020 * along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2121 */
2222
23- import org .schabi .newpipe .extractor .ListExtractor ;
24- import org .schabi .newpipe .extractor .StreamingService ;
25- import org .schabi .newpipe .extractor .UrlIdHandler ;
23+ import org .jsoup .Jsoup ;
24+ import org .jsoup .nodes .Document ;
25+ import org .jsoup .nodes .Element ;
26+ import org .schabi .newpipe .extractor .*;
2627import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
28+ import org .schabi .newpipe .extractor .exceptions .ParsingException ;
2729import org .schabi .newpipe .extractor .kiosk .KioskExtractor ;
2830import org .schabi .newpipe .extractor .stream .StreamInfoItemCollector ;
31+
2932import java .io .IOException ;
3033
3134public class YoutubeTrendingExtractor extends KioskExtractor {
3235
36+ private Document doc ;
37+
3338 public YoutubeTrendingExtractor (StreamingService service , String url , String nextStreamsUrl )
3439 throws IOException , ExtractionException {
3540 super (service , url , nextStreamsUrl );
3641 }
3742
3843 @ Override
39- public void fetchPage ()
40- throws IOException , ExtractionException {
44+ public void fetchPage () throws IOException , ExtractionException {
45+ Downloader downloader = NewPipe . getDownloader ();
4146
47+ String channelUrl = getCleanUrl ();
48+ String pageContent = downloader .download (channelUrl );
49+ doc = Jsoup .parse (pageContent , channelUrl );
4250 }
4351
4452 @ Override
4553 public String getType () {
46- return "Treinding " ;
54+ return "Trending " ;
4755 }
4856
4957 @ Override
@@ -57,7 +65,63 @@ public ListExtractor.NextItemsResult getNextStreams() {
5765 }
5866
5967 @ Override
60- public StreamInfoItemCollector getStreams () {
61- return null ;
68+ public StreamInfoItemCollector getStreams () throws ParsingException {
69+ StreamInfoItemCollector collector = new StreamInfoItemCollector (getServiceId ());
70+ Element ul = doc .select ("ul[class*=\" expanded-shelf-content-list\" ]" ).first ();
71+ for (final Element li : ul .children ()) {
72+ final Element el = li .select ("div[class*=\" yt-lockup-dismissable\" ]" ).first ();
73+ collector .commit (new YoutubeStreamInfoItemExtractor (li ) {
74+ @ Override
75+ public String getUrl () throws ParsingException {
76+ try {
77+ Element dl = el .select ("h3" ).first ().select ("a" ).first ();
78+ return dl .attr ("abs:href" );
79+ } catch (Exception e ) {
80+ throw new ParsingException ("Could not get web page url for the video" , e );
81+ }
82+ }
83+
84+ @ Override
85+ public String getName () throws ParsingException {
86+ try {
87+ Element dl = el .select ("h3" ).first ().select ("a" ).first ();
88+ return dl .text ();
89+ } catch (Exception e ) {
90+ throw new ParsingException ("Could not get web page url for the video" , e );
91+ }
92+ }
93+
94+ @ Override
95+ public String getUploaderName () throws ParsingException {
96+ try {
97+ Element uploaderEl = el .select ("div[class*=\" yt-lockup-byline \" ]" ).first ();
98+ return uploaderEl .select ("a" ).text ();
99+ } catch (Exception e ) {
100+ throw new ParsingException ("Could not get Uploader name" );
101+ }
102+ }
103+
104+ @ Override
105+ public String getThumbnailUrl () throws ParsingException {
106+ try {
107+ String url ;
108+ Element te = li .select ("span[class=\" yt-thumb-simple\" ]" ).first ()
109+ .select ("img" ).first ();
110+ url = te .attr ("abs:src" );
111+ // Sometimes youtube sends links to gif files which somehow seem to not exist
112+ // anymore. Items with such gif also offer a secondary image source. So we are going
113+ // to use that if we've caught such an item.
114+ if (url .contains (".gif" )) {
115+ url = te .attr ("abs:data-thumb" );
116+ }
117+ return url ;
118+ } catch (Exception e ) {
119+ throw new ParsingException ("Could not get thumbnail url" , e );
120+ }
121+ }
122+ });
123+ }
124+
125+ return collector ;
62126 }
63127}
0 commit comments