From 5c8bcf15ba8d14f7be2620ab7a85e1d61789ca7b Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sun, 12 Feb 2017 13:33:03 +0100 Subject: [PATCH] make colector hirarchicall remove broken commit() --- .../detail/VideoItemDetailFragment.java | 3 +- .../schabi/newpipe/extractor/InfoItem.java | 10 ++- .../newpipe/extractor/InfoItemCollector.java | 66 +++++++++++++++++++ .../extractor/channel/ChannelInfo.java | 2 +- .../extractor/channel/ChannelInfoItem.java | 34 +++++++++- ...ctor.java => InfoItemSearchCollector.java} | 13 ++-- .../extractor/search/SearchEngine.java | 16 +++-- .../extractor/search/SearchResult.java | 3 +- .../services/youtube/YoutubeSearchEngine.java | 15 +++-- .../extractor/stream_info/StreamInfo.java | 5 +- .../extractor/stream_info/StreamInfoItem.java | 15 ++++- .../stream_info/StreamInfoItemCollector.java | 32 +++------ .../newpipe/info_list/InfoItemBuilder.java | 10 ++- .../newpipe/info_list/InfoListAdapter.java | 5 +- 14 files changed, 170 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/extractor/InfoItemCollector.java rename app/src/main/java/org/schabi/newpipe/extractor/search/{StreamInfoSearchItemCollector.java => InfoItemSearchCollector.java} (74%) diff --git a/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailFragment.java b/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailFragment.java index 8f2496740..c3ef28bb4 100644 --- a/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/detail/VideoItemDetailFragment.java @@ -46,6 +46,7 @@ import org.schabi.newpipe.Localization; import org.schabi.newpipe.R; import org.schabi.newpipe.ReCaptchaActivity; import org.schabi.newpipe.download.DownloadDialog; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.stream_info.AudioStream; @@ -536,7 +537,7 @@ public class VideoItemDetailFragment extends Fragment { private void initSimilarVideos(final StreamInfo info) { LinearLayout similarLayout = (LinearLayout) activity.findViewById(R.id.similar_streams_view); - for (final StreamInfoItem item : info.related_streams) { + for (final InfoItem item : info.related_streams) { similarLayout.addView(infoItemBuilder.buildView(similarLayout, item)); } infoItemBuilder.setOnItemSelectedListener(new InfoItemBuilder.OnItemSelectedListener() { diff --git a/app/src/main/java/org/schabi/newpipe/extractor/InfoItem.java b/app/src/main/java/org/schabi/newpipe/extractor/InfoItem.java index 1b95a3868..a3395e8e4 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/InfoItem.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/InfoItem.java @@ -1,9 +1,5 @@ package org.schabi.newpipe.extractor; -import android.icu.text.IDNA; - -import static org.schabi.newpipe.extractor.InfoItem.InfoType.STREAM; - /** * Created by the-scrabi on 11.02.17. * @@ -30,6 +26,8 @@ public interface InfoItem { PLAYLIST, CHANNEL } - void setInfoType(InfoType iT); - InfoType getInfoType(); + + InfoType infoType(); + String getTitle(); + String getLink(); } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/InfoItemCollector.java b/app/src/main/java/org/schabi/newpipe/extractor/InfoItemCollector.java new file mode 100644 index 000000000..1bc27171c --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/extractor/InfoItemCollector.java @@ -0,0 +1,66 @@ +package org.schabi.newpipe.extractor; + +import org.schabi.newpipe.extractor.exceptions.ExtractionException; + +import java.util.List; +import java.util.Vector; + +/** + * Created by Christian Schabesberger on 12.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * InfoItemCollector.java is part of NewPipe. + * + * NewPipe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPipe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPipe. If not, see . + */ + +public class InfoItemCollector { + private List itemList = new Vector<>(); + private List errors = new Vector<>(); + private UrlIdHandler urlIdHandler; + private int serviceId = -1; + + public InfoItemCollector(UrlIdHandler handler, int serviceId) { + urlIdHandler = handler; + this.serviceId = serviceId; + } + + public List getItemList() { + return itemList; + } + public List getErrors() { + return errors; + } + public void addFromCollector(InfoItemCollector otherC) throws ExtractionException { + if(serviceId != otherC.serviceId) { + throw new ExtractionException("Service Id does not equal: " + + NewPipe.getNameOfService(serviceId) + + " and " + NewPipe.getNameOfService(otherC.serviceId)); + } + errors.addAll(otherC.errors); + itemList.addAll(otherC.itemList); + } + protected void addError(Exception e) { + errors.add(e); + } + protected void addItem(InfoItem item) { + itemList.add(item); + } + protected int getServiceId() { + return serviceId; + } + protected UrlIdHandler getUrlIdHandler() { + return urlIdHandler; + } +} diff --git a/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java b/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java index 2cf0733d9..68862ec32 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java @@ -75,7 +75,7 @@ public class ChannelInfo { public String avatar_url = ""; public String banner_url = ""; public String feed_url = ""; - public List related_streams = null; + public List related_streams = null; public boolean hasNextPage = false; public List errors = new Vector<>(); diff --git a/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java b/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java index 21f4b5eaa..e598c659f 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfoItem.java @@ -1,8 +1,38 @@ package org.schabi.newpipe.extractor.channel; +import org.schabi.newpipe.extractor.InfoItem; + /** - * Created by the-scrabi on 11.02.17. + * Created by Christian Schabesberger on 11.02.17. + * + * Copyright (C) Christian Schabesberger 2017 + * ChannelInfoItem.java is part of NewPipe. + * + * NewPipe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPipe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPipe. If not, see . */ -public class ChannelInfoItem { +public class ChannelInfoItem implements InfoItem { + + public InfoType infoType() { + return InfoType.CHANNEL; + } + + public String getTitle() { + return ""; + } + + public String getLink() { + return ""; + } } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/search/StreamInfoSearchItemCollector.java b/app/src/main/java/org/schabi/newpipe/extractor/search/InfoItemSearchCollector.java similarity index 74% rename from app/src/main/java/org/schabi/newpipe/extractor/search/StreamInfoSearchItemCollector.java rename to app/src/main/java/org/schabi/newpipe/extractor/search/InfoItemSearchCollector.java index 17be8e17e..3040452ed 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/search/StreamInfoSearchItemCollector.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/search/InfoItemSearchCollector.java @@ -1,13 +1,13 @@ package org.schabi.newpipe.extractor.search; +import org.schabi.newpipe.extractor.InfoItemCollector; import org.schabi.newpipe.extractor.UrlIdHandler; -import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; /** - * Created by Christian Schabesberger on 11.05.16. + * Created by Christian Schabesberger on 12.02.17. * - * Copyright (C) Christian Schabesberger 2016 - * StreamInfoSearchItemCollector.java is part of NewPipe. + * Copyright (C) Christian Schabesberger 2017 + * InfoItemSearchCollector.java is part of NewPipe. * * NewPipe is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,11 +23,10 @@ import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; * along with NewPipe. If not, see . */ -public class StreamInfoSearchItemCollector extends StreamInfoItemCollector { - +public class InfoItemSearchCollector extends InfoItemCollector { private String suggestion = ""; - public StreamInfoSearchItemCollector(UrlIdHandler handler, int serviceId) { + InfoItemSearchCollector(UrlIdHandler handler, int serviceId) { super(handler, serviceId); } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/search/SearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/search/SearchEngine.java index 669ad266b..03474d80c 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/search/SearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/search/SearchEngine.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.search; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; import java.io.IOException; @@ -32,18 +33,23 @@ public abstract class SearchEngine { } } - private StreamInfoSearchItemCollector collector; + private StreamInfoItemCollector streamCollector; + private InfoItemSearchCollector collector; public SearchEngine(UrlIdHandler urlIdHandler, int serviceId) { - collector = new StreamInfoSearchItemCollector(urlIdHandler, serviceId); + streamCollector = new StreamInfoItemCollector(urlIdHandler, serviceId); + collector = new InfoItemSearchCollector(urlIdHandler, serviceId); } - protected StreamInfoSearchItemCollector getStreamPreviewInfoSearchCollector() { + protected StreamInfoItemCollector getStreamPreviewInfoCollector() { + return streamCollector; + } + + protected InfoItemSearchCollector getInfoItemSearchCollector() { return collector; } - //Result search(String query, int page); - public abstract StreamInfoSearchItemCollector search( + public abstract InfoItemSearchCollector search( String query, int page, String contentCountry) throws ExtractionException, IOException; } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/search/SearchResult.java b/app/src/main/java/org/schabi/newpipe/extractor/search/SearchResult.java index 7fa56a3e9..a3ef1fbcd 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/search/SearchResult.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/search/SearchResult.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.extractor.search; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; @@ -45,6 +46,6 @@ public class SearchResult { } public String suggestion = ""; - public List resultList = new Vector<>(); + public List resultList = new Vector<>(); public List errors = new Vector<>(); } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java index e4314f716..db5dbd32f 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngine.java @@ -7,8 +7,9 @@ import org.schabi.newpipe.extractor.Downloader; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.search.InfoItemSearchCollector; import org.schabi.newpipe.extractor.search.SearchEngine; -import org.schabi.newpipe.extractor.search.StreamInfoSearchItemCollector; +import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector; import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; import java.net.URLEncoder; @@ -45,9 +46,11 @@ public class YoutubeSearchEngine extends SearchEngine { } @Override - public StreamInfoSearchItemCollector search(String query, int page, String languageCode) + public InfoItemSearchCollector search(String query, int page, String languageCode) throws IOException, ExtractionException { - StreamInfoSearchItemCollector collector = getStreamPreviewInfoSearchCollector(); + StreamInfoItemCollector streamCollector = getStreamPreviewInfoCollector(); + InfoItemSearchCollector collector = getInfoItemSearchCollector(); + Downloader downloader = NewPipe.getDownloader(); @@ -97,13 +100,13 @@ public class YoutubeSearchEngine extends SearchEngine { // video item type } else if ((el = item.select("div[class*=\"yt-lockup-video\"").first()) != null) { - collector.commit(extractPreviewInfo(el)); + streamCollector.commit(extractPreviewInfo(el)); } else { //noinspection ConstantConditions - collector.addError(new Exception("unexpected element found:\"" + el + "\"")); + throw new ExtractionException("unexpected element found:\"" + el + "\""); } } - + collector.addFromCollector(streamCollector); return collector; } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfo.java b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfo.java index 2d1abc522..cf3df49d8 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfo.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfo.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.stream_info; import org.schabi.newpipe.extractor.AbstractStreamInfo; import org.schabi.newpipe.extractor.DashMpdParser; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; @@ -246,7 +247,7 @@ public class StreamInfo extends AbstractStreamInfo { StreamInfoItemExtractor nextVideo = extractor.getNextVideo(); c.commit(nextVideo); if(c.getItemList().size() != 0) { - streamInfo.next_video = c.getItemList().get(0); + streamInfo.next_video = (StreamInfoItem) c.getItemList().get(0); } streamInfo.errors.addAll(c.getErrors()); } @@ -285,7 +286,7 @@ public class StreamInfo extends AbstractStreamInfo { public int dislike_count = -1; public String average_rating = ""; public StreamInfoItem next_video = null; - public List related_streams = null; + public List related_streams = null; //in seconds. some metadata is not passed using a StreamInfo object! public int start_position = 0; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItem.java b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItem.java index f9c4e2860..8a6db5bc0 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItem.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItem.java @@ -21,8 +21,21 @@ package org.schabi.newpipe.extractor.stream_info; */ import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.InfoItem; /**Info object for previews of unopened videos, eg search results, related videos*/ -public class StreamInfoItem extends AbstractStreamInfo { +public class StreamInfoItem extends AbstractStreamInfo implements InfoItem { public int duration; + + public InfoType infoType() { + return InfoType.STREAM; + } + + public String getTitle() { + return title; + } + + public String getLink() { + return webpage_url; + } } \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java index cefef9fa7..67db27e8a 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.extractor.stream_info; +import org.schabi.newpipe.extractor.InfoItemCollector; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.FoundAdException; @@ -28,39 +29,24 @@ import java.util.Vector; * along with NewPipe. If not, see . */ -public class StreamInfoItemCollector { - private List itemList = new Vector<>(); - private List errors = new Vector<>(); - private UrlIdHandler urlIdHandler; - private int serviceId = -1; +public class StreamInfoItemCollector extends InfoItemCollector { public StreamInfoItemCollector(UrlIdHandler handler, int serviceId) { - urlIdHandler = handler; - this.serviceId = serviceId; - } - - public List getItemList() { - return itemList; - } - - public List getErrors() { - return errors; - } - - public void addError(Exception e) { - errors.add(e); + super(handler, serviceId); } public void commit(StreamInfoItemExtractor extractor) throws ParsingException { try { StreamInfoItem resultItem = new StreamInfoItem(); // importand information - resultItem.service_id = serviceId; + resultItem.service_id = getServiceId(); resultItem.webpage_url = extractor.getWebPageUrl(); - if (urlIdHandler == null) { + if (getUrlIdHandler() == null) { throw new ParsingException("Error: UrlIdHandler not set"); } else if(!resultItem.webpage_url.isEmpty()) { - resultItem.id = NewPipe.getService(serviceId).getUrlIdHandlerInstance().getId(resultItem.webpage_url); + resultItem.id = NewPipe.getService(getServiceId()) + .getUrlIdHandlerInstance() + .getId(resultItem.webpage_url); } resultItem.title = extractor.getTitle(); resultItem.stream_type = extractor.getStreamType(); @@ -91,7 +77,7 @@ public class StreamInfoItemCollector { } catch (Exception e) { addError(e); } - itemList.add(resultItem); + addItem(resultItem); } catch(FoundAdException ae) { System.out.println("AD_WARNING: " + ae.getMessage()); } catch (Exception e) { diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java index a0a7e2c87..dd33d4caa 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoItemBuilder.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.info_list; import android.app.Activity; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -11,6 +12,7 @@ import com.nostra13.universalimageloader.core.ImageLoader; import org.schabi.newpipe.ImageErrorLoadingListener; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.AbstractStreamInfo; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; /** @@ -55,7 +57,11 @@ public class InfoItemBuilder { this.onItemSelectedListener = onItemSelectedListener; } - public void buildByHolder(InfoItemHolder holder, final StreamInfoItem info) { + public void buildByHolder(InfoItemHolder holder, final InfoItem i) { + final StreamInfoItem info = (StreamInfoItem) i; + if(info.infoType() != InfoItem.InfoType.STREAM) { + Log.e("InfoItemBuilder", "Info type not yet supported"); + } // fill holder with information holder.itemVideoTitleView.setText(info.title); if(info.uploader != null && !info.uploader.isEmpty()) { @@ -97,7 +103,7 @@ public class InfoItemBuilder { }); } - public View buildView(ViewGroup parent, final StreamInfoItem info) { + public View buildView(ViewGroup parent, final InfoItem info) { View streamPreviewView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.video_item, parent, false); InfoItemHolder holder = new InfoItemHolder(streamPreviewView); diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java index 843010865..0daf211e5 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java @@ -7,6 +7,7 @@ import android.view.View; import android.view.ViewGroup; import org.schabi.newpipe.R; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.stream_info.StreamInfoItem; import java.util.List; @@ -35,7 +36,7 @@ import java.util.Vector; public class InfoListAdapter extends RecyclerView.Adapter { private final InfoItemBuilder infoItemBuilder; - private final List streamList; + private final List streamList; public InfoListAdapter(Activity a, View rootView) { infoItemBuilder = new InfoItemBuilder(a, rootView); @@ -47,7 +48,7 @@ public class InfoListAdapter extends RecyclerView.Adapter { infoItemBuilder.setOnItemSelectedListener(onItemSelectedListener); } - public void addStreamItemList(List videos) { + public void addStreamItemList(List videos) { if(videos!= null) { streamList.addAll(videos); notifyDataSetChanged();