From b1d2e644500e27064f0a5118711f263d1027c2ab Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 4 Mar 2016 13:36:06 +0100 Subject: [PATCH 1/2] ad service_id field, and pulled video/audio stream out of streaminfo --- .../youtube/YoutubeSearchEngineTest.java | 5 +- .../YoutubeStreamExtractorDefaultTest.java | 16 +++-- .../YoutubeStreamExtractorGemaTest.java | 5 +- .../YoutubeStreamExtractorRestrictedTest.java | 17 ++++-- .../org/schabi/newpipe/ActionBarHandler.java | 9 +-- .../newpipe/VideoItemDetailActivity.java | 2 +- .../newpipe/VideoItemDetailFragment.java | 18 +++--- .../schabi/newpipe/VideoItemListActivity.java | 3 +- .../schabi/newpipe/VideoItemListFragment.java | 2 +- .../newpipe/extractor/AbstractVideoInfo.java | 1 + .../schabi/newpipe/extractor/AudioStream.java | 46 +++++++++++++++ .../newpipe/extractor/DashMpdParser.java | 6 +- .../newpipe/extractor/SearchEngine.java | 20 +++++-- .../schabi/newpipe/extractor/ServiceList.java | 2 +- .../newpipe/extractor/StreamExtractor.java | 17 ++++-- .../schabi/newpipe/extractor/StreamInfo.java | 59 ++----------------- .../extractor/StreamPreviewInfoCollector.java | 10 ++-- .../newpipe/extractor/StreamingService.java | 25 +++++--- .../schabi/newpipe/extractor/VideoStream.java | 44 ++++++++++++++ .../youtube/YoutubeParsingHelper.java | 2 +- .../services/youtube/YoutubeSearchEngine.java | 10 +++- .../services/youtube/YoutubeService.java | 15 +++-- .../youtube/YoutubeStreamExtractor.java | 22 ++++--- 23 files changed, 230 insertions(+), 126 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/extractor/AudioStream.java create mode 100644 app/src/main/java/org/schabi/newpipe/extractor/VideoStream.java diff --git a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java index 0a590ea51..07b1f27ad 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java @@ -4,10 +4,12 @@ import android.test.AndroidTestCase; import org.apache.commons.lang.exception.ExceptionUtils; import org.schabi.newpipe.extractor.SearchResult; +import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.StreamPreviewInfo; import org.schabi.newpipe.extractor.SearchEngine; import org.schabi.newpipe.extractor.services.youtube.YoutubeSearchEngine; import org.schabi.newpipe.Downloader; +import org.schabi.newpipe.extractor.services.youtube.YoutubeService; import java.util.ArrayList; @@ -38,7 +40,8 @@ public class YoutubeSearchEngineTest extends AndroidTestCase { @Override public void setUp() throws Exception{ super.setUp(); - SearchEngine engine = new YoutubeSearchEngine(); + SearchEngine engine = ServiceList.getService("Youtube") + .getSearchEngineInstance(new Downloader()); result = engine.search("bla", 0, "de", new Downloader()).getSearchResult(); diff --git a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorDefaultTest.java b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorDefaultTest.java index e35fd86d8..5217a5753 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorDefaultTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorDefaultTest.java @@ -5,6 +5,9 @@ import android.test.AndroidTestCase; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.ParsingException; +import org.schabi.newpipe.extractor.ServiceList; +import org.schabi.newpipe.extractor.StreamExtractor; +import org.schabi.newpipe.extractor.VideoStream; import org.schabi.newpipe.extractor.services.youtube.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.StreamInfo; @@ -31,15 +34,15 @@ import java.io.IOException; */ public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase { - private YoutubeStreamExtractor extractor; + private StreamExtractor extractor; public void setUp() throws IOException, ExtractionException { /* some anonymus video test extractor = new YoutubeStreamExtractor("https://www.youtube.com/watch?v=FmG385_uUys", new Downloader()); */ /* some vevo video (suggested to test against) */ - extractor = new YoutubeStreamExtractor("https://www.youtube.com/watch?v=YQHsXMglC9A", - new Downloader()); + extractor = ServiceList.getService("Youtube") + .getExtractorInstance("https://www.youtube.com/watch?v=YQHsXMglC9A", new Downloader()); } public void testGetInvalidTimeStamp() throws ParsingException { @@ -48,8 +51,9 @@ public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase { } public void testGetValidTimeStamp() throws ExtractionException, IOException { - YoutubeStreamExtractor extractor = - new YoutubeStreamExtractor("https://youtu.be/FmG385_uUys?t=174", new Downloader()); + StreamExtractor extractor = + ServiceList.getService("Youtube") + .getExtractorInstance("https://youtu.be/FmG385_uUys?t=174", new Downloader()); assertTrue(Integer.toString(extractor.getTimeStamp()), extractor.getTimeStamp() == 174); } @@ -94,7 +98,7 @@ public class YoutubeStreamExtractorDefaultTest extends AndroidTestCase { } public void testGetVideoStreams() throws ParsingException { - for(StreamInfo.VideoStream s : extractor.getVideoStreams()) { + for(VideoStream s : extractor.getVideoStreams()) { assertTrue(s.url, s.url.contains("https://")); assertTrue(s.resolution.length() > 0); diff --git a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorGemaTest.java b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorGemaTest.java index c6c8471a9..695544a01 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorGemaTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorGemaTest.java @@ -4,6 +4,7 @@ import android.test.AndroidTestCase; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.ExtractionException; +import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.services.youtube.YoutubeStreamExtractor; import java.io.IOException; @@ -38,9 +39,9 @@ public class YoutubeStreamExtractorGemaTest extends AndroidTestCase { public void testGemaError() throws IOException, ExtractionException { if(testActive) { try { - new YoutubeStreamExtractor("https://www.youtube.com/watch?v=3O1_3zBUKM8", + ServiceList.getService("Youtube") + .getExtractorInstance("https://www.youtube.com/watch?v=3O1_3zBUKM8", new Downloader()); - assertTrue("Gema exception not thrown", false); } catch(YoutubeStreamExtractor.GemaException ge) { assertTrue(true); } diff --git a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorRestrictedTest.java b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorRestrictedTest.java index 12d8a65c7..d848bb2ce 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorRestrictedTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeStreamExtractorRestrictedTest.java @@ -5,17 +5,21 @@ import android.test.AndroidTestCase; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.ParsingException; +import org.schabi.newpipe.extractor.ServiceList; +import org.schabi.newpipe.extractor.StreamExtractor; import org.schabi.newpipe.extractor.StreamInfo; +import org.schabi.newpipe.extractor.VideoStream; import org.schabi.newpipe.extractor.services.youtube.YoutubeStreamExtractor; import java.io.IOException; public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase { - private YoutubeStreamExtractor extractor; + private StreamExtractor extractor; public void setUp() throws IOException, ExtractionException { - extractor = new YoutubeStreamExtractor("https://www.youtube.com/watch?v=i6JTvzrpBy0", - new Downloader()); + extractor = ServiceList.getService("Youtube") + .getExtractorInstance("https://www.youtube.com/watch?v=i6JTvzrpBy0", + new Downloader()); } public void testGetInvalidTimeStamp() throws ParsingException { @@ -24,8 +28,9 @@ public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase { } public void testGetValidTimeStamp() throws ExtractionException, IOException { - YoutubeStreamExtractor extractor = - new YoutubeStreamExtractor("https://youtu.be/FmG385_uUys?t=174", new Downloader()); + StreamExtractor extractor=ServiceList.getService("Youtube") + .getExtractorInstance("https://youtu.be/FmG385_uUys?t=174", + new Downloader()); assertTrue(Integer.toString(extractor.getTimeStamp()), extractor.getTimeStamp() == 174); } @@ -73,7 +78,7 @@ public class YoutubeStreamExtractorRestrictedTest extends AndroidTestCase { } public void testGetVideoStreams() throws ParsingException { - for(StreamInfo.VideoStream s : extractor.getVideoStreams()) { + for(VideoStream s : extractor.getVideoStreams()) { assertTrue(s.url, s.url.contains("https://")); assertTrue(s.resolution.length() > 0); diff --git a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java index 6836f223e..ab5074f3b 100644 --- a/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java +++ b/app/src/main/java/org/schabi/newpipe/ActionBarHandler.java @@ -13,6 +13,7 @@ import android.widget.ArrayAdapter; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.StreamInfo; +import org.schabi.newpipe.extractor.VideoStream; import java.util.List; @@ -75,7 +76,7 @@ class ActionBarHandler { } } - public void setupStreamList(final List videoStreams) { + public void setupStreamList(final List videoStreams) { if (activity != null) { selectedVideoStream = 0; @@ -83,7 +84,7 @@ class ActionBarHandler { // this array will be shown in the dropdown menu for selecting the stream/resolution. String[] itemArray = new String[videoStreams.size()]; for (int i = 0; i < videoStreams.size(); i++) { - StreamInfo.VideoStream item = videoStreams.get(i); + VideoStream item = videoStreams.get(i); itemArray[i] = MediaFormat.getNameById(item.format) + " " + item.resolution; } int defaultResolution = getDefaultResolution(videoStreams); @@ -108,13 +109,13 @@ class ActionBarHandler { } - private int getDefaultResolution(final List videoStreams) { + private int getDefaultResolution(final List videoStreams) { String defaultResolution = defaultPreferences .getString(activity.getString(R.string.default_resolution_key), activity.getString(R.string.default_resolution_value)); for (int i = 0; i < videoStreams.size(); i++) { - StreamInfo.VideoStream item = videoStreams.get(i); + VideoStream item = videoStreams.get(i); if (defaultResolution.equals(item.resolution)) { return i; } diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java b/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java index a273cd896..9ba18bdcc 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java @@ -73,7 +73,7 @@ public class VideoItemDetailActivity extends AppCompatActivity { StreamingService[] serviceList = ServiceList.getServices(); //StreamExtractor videoExtractor = null; for (int i = 0; i < serviceList.length; i++) { - if (serviceList[i].getUrlIdHandler().acceptUrl(videoUrl)) { + if (serviceList[i].getUrlIdHandlerInstance().acceptUrl(videoUrl)) { arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, i); currentStreamingService = i; //videoExtractor = ServiceList.getService(i).getExtractorInstance(); diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java index 1a35a4d38..14c495dbe 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemDetailFragment.java @@ -46,6 +46,7 @@ import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; import java.util.ArrayList; import java.util.Vector; +import org.schabi.newpipe.extractor.AudioStream; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.ParsingException; import org.schabi.newpipe.extractor.ServiceList; @@ -53,6 +54,7 @@ import org.schabi.newpipe.extractor.StreamExtractor; import org.schabi.newpipe.extractor.StreamInfo; import org.schabi.newpipe.extractor.StreamPreviewInfo; import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.VideoStream; import org.schabi.newpipe.extractor.services.youtube.YoutubeStreamExtractor; @@ -388,8 +390,8 @@ public class VideoItemDetailFragment extends Fragment { descriptionView.setMovementMethod(LinkMovementMethod.getInstance()); // parse streams - Vector streamsToUse = new Vector<>(); - for (StreamInfo.VideoStream i : info.video_streams) { + Vector streamsToUse = new Vector<>(); + for (VideoStream i : info.video_streams) { if (useStream(i, streamsToUse)) { streamsToUse.add(i); } @@ -555,7 +557,7 @@ public class VideoItemDetailFragment extends Fragment { // website which was crawled. Then the ui has to understand this and act right. if (info.audio_streams != null) { - StreamInfo.AudioStream audioStream = + AudioStream audioStream = info.audio_streams.get(getPreferredAudioStreamId(info)); String audioSuffix = "." + MediaFormat.getSuffixById(audioStream.format); @@ -564,7 +566,7 @@ public class VideoItemDetailFragment extends Fragment { } if (info.video_streams != null) { - StreamInfo.VideoStream selectedStreamItem = info.video_streams.get(selectedStreamId); + VideoStream selectedStreamItem = info.video_streams.get(selectedStreamId); String videoSuffix = "." + MediaFormat.getSuffixById(selectedStreamItem.format); args.putString(DownloadDialog.FILE_SUFFIX_VIDEO, videoSuffix); args.putString(DownloadDialog.VIDEO_URL, selectedStreamItem.url); @@ -591,7 +593,7 @@ public class VideoItemDetailFragment extends Fragment { boolean useExternalAudioPlayer = PreferenceManager.getDefaultSharedPreferences(activity) .getBoolean(activity.getString(R.string.use_external_audio_player_key), false); Intent intent; - StreamInfo.AudioStream audioStream = + AudioStream audioStream = info.audio_streams.get(getPreferredAudioStreamId(info)); if (!useExternalAudioPlayer && android.os.Build.VERSION.SDK_INT >= 18) { //internal music player: explicit intent @@ -754,8 +756,8 @@ public class VideoItemDetailFragment extends Fragment { .show(); } - private boolean useStream(StreamInfo.VideoStream stream, Vector streams) { - for(StreamInfo.VideoStream i : streams) { + private boolean useStream(VideoStream stream, Vector streams) { + for(VideoStream i : streams) { if(i.resolution.equals(stream.resolution)) { return false; } @@ -847,7 +849,7 @@ public class VideoItemDetailFragment extends Fragment { public void playVideo(final StreamInfo info) { // ----------- THE MAGIC MOMENT --------------- - StreamInfo.VideoStream selectedVideoStream = + VideoStream selectedVideoStream = info.video_streams.get(actionBarHandler.getSelectedVideoStream()); if (PreferenceManager.getDefaultSharedPreferences(activity) diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java index 533c69e85..265663aed 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListActivity.java @@ -357,7 +357,8 @@ public class VideoItemListActivity extends AppCompatActivity } private void searchSuggestions(String query) { - suggestionSearchRunnable = new SuggestionSearchRunnable(streamingService.getSearchEngineInstance(), + suggestionSearchRunnable = + new SuggestionSearchRunnable(streamingService.getSearchEngineInstance(new Downloader()), query); searchThread = new Thread(suggestionSearchRunnable); searchThread.start(); diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java index fc20aadb9..10d4997f4 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemListFragment.java @@ -185,7 +185,7 @@ public class VideoItemListFragment extends ListFragment { private void startSearch(String query, int page) { currentRequestId++; terminateThreads(); - searchRunnable = new SearchRunnable(streamingService.getSearchEngineInstance(), + searchRunnable = new SearchRunnable(streamingService.getSearchEngineInstance(new Downloader()), query, page, currentRequestId); searchThread = new Thread(searchRunnable); searchThread.start(); diff --git a/app/src/main/java/org/schabi/newpipe/extractor/AbstractVideoInfo.java b/app/src/main/java/org/schabi/newpipe/extractor/AbstractVideoInfo.java index 79d43770b..5da44c3d4 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/AbstractVideoInfo.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/AbstractVideoInfo.java @@ -22,6 +22,7 @@ import android.graphics.Bitmap; /**Common properties between StreamInfo and StreamPreviewInfo.*/ public abstract class AbstractVideoInfo { + public int service_id = -1; public String id = ""; public String title = ""; public String uploader = ""; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/AudioStream.java b/app/src/main/java/org/schabi/newpipe/extractor/AudioStream.java new file mode 100644 index 000000000..a464926c9 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/extractor/AudioStream.java @@ -0,0 +1,46 @@ +package org.schabi.newpipe.extractor; + +/** + * Created by Christian Schabesberger on 04.03.16. + * + * Copyright (C) Christian Schabesberger 2016 + * AudioStream.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 AudioStream { + public String url = ""; + public int format = -1; + public int bandwidth = -1; + public int sampling_rate = -1; + + public AudioStream(String url, int format, int bandwidth, int samplingRate) { + this.url = url; this.format = format; + this.bandwidth = bandwidth; this.sampling_rate = samplingRate; + } + + // reveals wether two streams are the same, but have diferent urls + public boolean equalStats(AudioStream cmp) { + return format == cmp.format + && bandwidth == cmp.bandwidth + && sampling_rate == cmp.sampling_rate; + } + + // revelas wether two streams are equal + public boolean equals(AudioStream cmp) { + return equalStats(cmp) + && url == cmp.url; + } +} diff --git a/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java b/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java index 55cae46fa..2eab7e3bc 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java @@ -37,7 +37,7 @@ public class DashMpdParser { } } - public static List getAudioStreams(String dashManifestUrl, + public static List getAudioStreams(String dashManifestUrl, Downloader downloader) throws DashMpdParsingException { String dashDoc; @@ -46,7 +46,7 @@ public class DashMpdParser { } catch(IOException ioe) { throw new DashMpdParsingException("Could not get dash mpd: " + dashManifestUrl, ioe); } - Vector audioStreams = new Vector<>(); + Vector audioStreams = new Vector<>(); try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(new StringReader(dashDoc)); @@ -83,7 +83,7 @@ public class DashMpdParser { } else if(currentMimeType.equals(MediaFormat.M4A.mimeType)) { format = MediaFormat.M4A.id; } - audioStreams.add(new StreamInfo.AudioStream(parser.getText(), + audioStreams.add(new AudioStream(parser.getText(), format, currentBandwidth, currentSamplingRate)); } break; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java b/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java index 677287afe..851025c28 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/SearchEngine.java @@ -26,17 +26,29 @@ import java.util.Vector; */ @SuppressWarnings("ALL") -public interface SearchEngine { - public class NothingFoundException extends ExtractionException { +public abstract class SearchEngine { + public static class NothingFoundException extends ExtractionException { public NothingFoundException(String message) { super(message); } } - ArrayList suggestionList(String query,String contentCountry, Downloader dl) + private StreamPreviewInfoCollector collector; + + public SearchEngine(StreamUrlIdHandler urlIdHandler, int serviceId) { + collector = new StreamPreviewInfoCollector(urlIdHandler, serviceId); + } + + public StreamPreviewInfoCollector getStreamPreviewInfoCollector() { + return collector; + } + + public abstract ArrayList suggestionList( + String query,String contentCountry, Downloader dl) throws ExtractionException, IOException; //Result search(String query, int page); - StreamPreviewInfoCollector search(String query, int page, String contentCountry, Downloader dl) + public abstract StreamPreviewInfoCollector search( + String query, int page, String contentCountry, Downloader dl) throws ExtractionException, IOException; } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java b/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java index 0d5bce51e..7306faabb 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/ServiceList.java @@ -31,7 +31,7 @@ import org.schabi.newpipe.extractor.services.youtube.YoutubeService; public class ServiceList { private static final String TAG = ServiceList.class.toString(); private static final StreamingService[] services = { - new YoutubeService() + new YoutubeService(0) }; public static StreamingService[] getServices() { return services; diff --git a/app/src/main/java/org/schabi/newpipe/extractor/StreamExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/StreamExtractor.java index b1f0249fb..8c5873cfc 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/StreamExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/StreamExtractor.java @@ -26,7 +26,9 @@ import java.util.List; @SuppressWarnings("ALL") -public interface StreamExtractor { +public abstract class StreamExtractor { + + private int serviceId; public class ExctractorInitException extends ExtractionException { public ExctractorInitException(String message) { @@ -49,6 +51,10 @@ public interface StreamExtractor { } } + public StreamExtractor(String url, Downloader dl, int serviceId) { + this.serviceId = serviceId; + } + public abstract int getTimeStamp() throws ParsingException; public abstract String getTitle() throws ParsingException; public abstract String getDescription() throws ParsingException; @@ -58,9 +64,9 @@ public interface StreamExtractor { public abstract String getUploadDate() throws ParsingException; public abstract String getThumbnailUrl() throws ParsingException; public abstract String getUploaderThumbnailUrl() throws ParsingException; - public abstract List getAudioStreams() throws ParsingException; - public abstract List getVideoStreams() throws ParsingException; - public abstract List getVideoOnlyStreams() throws ParsingException; + public abstract List getAudioStreams() throws ParsingException; + public abstract List getVideoStreams() throws ParsingException; + public abstract List getVideoOnlyStreams() throws ParsingException; public abstract String getDashMpdUrl() throws ParsingException; public abstract int getAgeLimit() throws ParsingException; public abstract String getAverageRating() throws ParsingException; @@ -70,4 +76,7 @@ public interface StreamExtractor { public abstract List getRelatedVideos() throws ParsingException; public abstract StreamUrlIdHandler getUrlIdConverter(); public abstract String getPageUrl(); + public int getServiceId() { + return serviceId; + } } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/StreamInfo.java b/app/src/main/java/org/schabi/newpipe/extractor/StreamInfo.java index fdcbfd027..963aa8298 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/StreamInfo.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/StreamInfo.java @@ -55,6 +55,7 @@ public class StreamInfo extends AbstractVideoInfo { StreamUrlIdHandler uiconv = extractor.getUrlIdConverter(); + streamInfo.service_id = extractor.getServiceId(); streamInfo.webpage_url = extractor.getPageUrl(); streamInfo.id = uiconv.getVideoId(extractor.getPageUrl()); streamInfo.title = extractor.getTitle(); @@ -231,12 +232,15 @@ public class StreamInfo extends AbstractVideoInfo { public List related_videos = null; //in seconds. some metadata is not passed using a StreamInfo object! public int start_position = 0; - //todo: public int service_id = -1; public List errors = new Vector<>(); public StreamInfo() {} + public void addException(Exception e) { + errors.add(e); + } + /**Creates a new StreamInfo object from an existing AbstractVideoInfo. * All the shared properties are copied to the new StreamInfo.*/ @SuppressWarnings("WeakerAccess") @@ -262,57 +266,4 @@ public class StreamInfo extends AbstractVideoInfo { this.duration = ((StreamPreviewInfo)avi).duration; } } - - public static class VideoStream { - //url of the stream - public String url = ""; - public int format = -1; - public String resolution = ""; - - public VideoStream(String url, int format, String res) { - this.url = url; this.format = format; resolution = res; - } - - // reveals wether two streams are the same, but have diferent urls - public boolean equalStats(VideoStream cmp) { - return format == cmp.format - && resolution == cmp.resolution; - } - - // revelas wether two streams are equal - public boolean equals(VideoStream cmp) { - return equalStats(cmp) - && url == cmp.url; - } - } - - @SuppressWarnings("unused") - public static class AudioStream { - public String url = ""; - public int format = -1; - public int bandwidth = -1; - public int sampling_rate = -1; - - public AudioStream(String url, int format, int bandwidth, int samplingRate) { - this.url = url; this.format = format; - this.bandwidth = bandwidth; this.sampling_rate = samplingRate; - } - - // reveals wether two streams are the same, but have diferent urls - public boolean equalStats(AudioStream cmp) { - return format == cmp.format - && bandwidth == cmp.bandwidth - && sampling_rate == cmp.sampling_rate; - } - - // revelas wether two streams are equal - public boolean equals(AudioStream cmp) { - return equalStats(cmp) - && url == cmp.url; - } - } - - public void addException(Exception e) { - errors.add(e); - } } \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/extractor/StreamPreviewInfoCollector.java b/app/src/main/java/org/schabi/newpipe/extractor/StreamPreviewInfoCollector.java index c22670065..d30a7d5dd 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/StreamPreviewInfoCollector.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/StreamPreviewInfoCollector.java @@ -23,11 +23,13 @@ import org.schabi.newpipe.extractor.services.youtube.YoutubeStreamUrlIdHandler; */ public class StreamPreviewInfoCollector { - SearchResult result = new SearchResult(); - StreamUrlIdHandler urlIdHandler = null; + private SearchResult result = new SearchResult(); + private StreamUrlIdHandler urlIdHandler = null; + private int serviceId = -1; - public StreamPreviewInfoCollector(StreamUrlIdHandler handler) { + public StreamPreviewInfoCollector(StreamUrlIdHandler handler, int serviceId) { urlIdHandler = handler; + this.serviceId = serviceId; } public void setSuggestion(String suggestion) { @@ -46,6 +48,7 @@ public class StreamPreviewInfoCollector { try { StreamPreviewInfo resultItem = new StreamPreviewInfo(); // importand information + resultItem.service_id = serviceId; resultItem.webpage_url = extractor.getWebPageUrl(); if (urlIdHandler == null) { throw new ParsingException("Error: UrlIdHandler not set"); @@ -81,7 +84,6 @@ public class StreamPreviewInfoCollector { addError(e); } - result.resultList.add(resultItem); } catch (Exception e) { addError(e); diff --git a/app/src/main/java/org/schabi/newpipe/extractor/StreamingService.java b/app/src/main/java/org/schabi/newpipe/extractor/StreamingService.java index 5e929e9d1..de9a4ebfe 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/StreamingService.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/StreamingService.java @@ -22,16 +22,25 @@ import java.io.IOException; * along with NewPipe. If not, see . */ -public interface StreamingService { - class ServiceInfo { +public abstract class StreamingService { + public class ServiceInfo { public String name = ""; } - ServiceInfo getServiceInfo(); - StreamExtractor getExtractorInstance(String url, Downloader downloader) + + private int serviceId; + + public StreamingService(int id) { + serviceId = id; + } + + public abstract ServiceInfo getServiceInfo(); + + public abstract StreamExtractor getExtractorInstance(String url, Downloader downloader) throws IOException, ExtractionException; - SearchEngine getSearchEngineInstance(); - - StreamUrlIdHandler getUrlIdHandler(); - + public abstract SearchEngine getSearchEngineInstance(Downloader downloader); + public abstract StreamUrlIdHandler getUrlIdHandlerInstance(); + public final int getServiceId() { + return serviceId; + } } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/VideoStream.java b/app/src/main/java/org/schabi/newpipe/extractor/VideoStream.java new file mode 100644 index 000000000..8f49b193d --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/extractor/VideoStream.java @@ -0,0 +1,44 @@ +package org.schabi.newpipe.extractor; + +/** + * Created by Christian Schabesberger on 04.03.16. + * + * Copyright (C) Christian Schabesberger 2016 + * VideoStream.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 VideoStream { + //url of the stream + public String url = ""; + public int format = -1; + public String resolution = ""; + + public VideoStream(String url, int format, String res) { + this.url = url; this.format = format; resolution = res; + } + + // reveals wether two streams are the same, but have diferent urls + public boolean equalStats(VideoStream cmp) { + return format == cmp.format + && resolution == cmp.resolution; + } + + // revelas wether two streams are equal + public boolean equals(VideoStream cmp) { + return equalStats(cmp) + && url == cmp.url; + } +} diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index 7d18ebc25..dc173626a 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -29,7 +29,7 @@ public class YoutubeParsingHelper { String days = "0"; String hours = "0"; String minutes = "0"; - String seconds = "0"; + String seconds; switch(splitInput.length) { case 4: 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 d25c70f07..110f3d513 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 @@ -14,6 +14,7 @@ import org.schabi.newpipe.extractor.SearchEngine; import org.schabi.newpipe.extractor.StreamExtractor; import org.schabi.newpipe.extractor.StreamPreviewInfoCollector; import org.schabi.newpipe.extractor.StreamPreviewInfoExtractor; +import org.schabi.newpipe.extractor.StreamUrlIdHandler; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -47,15 +48,18 @@ import javax.xml.parsers.ParserConfigurationException; * along with NewPipe. If not, see . */ -public class YoutubeSearchEngine implements SearchEngine { +public class YoutubeSearchEngine extends SearchEngine { private static final String TAG = YoutubeSearchEngine.class.toString(); + public YoutubeSearchEngine(StreamUrlIdHandler urlIdHandler, int serviceId) { + super(urlIdHandler, serviceId); + } + @Override public StreamPreviewInfoCollector search(String query, int page, String languageCode, Downloader downloader) throws IOException, ExtractionException { - StreamPreviewInfoCollector collector = new StreamPreviewInfoCollector( - new YoutubeStreamUrlIdHandler()); + StreamPreviewInfoCollector collector = getStreamPreviewInfoCollector(); Uri.Builder builder = new Uri.Builder(); builder.scheme("https") .authority("www.youtube.com") diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java index f35ae4044..94f9fb28e 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java @@ -30,7 +30,12 @@ import java.io.IOException; * along with NewPipe. If not, see . */ -public class YoutubeService implements StreamingService { +public class YoutubeService extends StreamingService { + + public YoutubeService(int id) { + super(id); + } + @Override public ServiceInfo getServiceInfo() { ServiceInfo serviceInfo = new ServiceInfo(); @@ -42,19 +47,19 @@ public class YoutubeService implements StreamingService { throws ExtractionException, IOException { StreamUrlIdHandler urlIdHandler = new YoutubeStreamUrlIdHandler(); if(urlIdHandler.acceptUrl(url)) { - return new YoutubeStreamExtractor(url, downloader) ; + return new YoutubeStreamExtractor(url, downloader, getServiceId()); } else { throw new IllegalArgumentException("supplied String is not a valid Youtube URL"); } } @Override - public SearchEngine getSearchEngineInstance() { - return new YoutubeSearchEngine(); + public SearchEngine getSearchEngineInstance(Downloader downloader) { + return new YoutubeSearchEngine(getUrlIdHandlerInstance(), getServiceId()); } @Override - public StreamUrlIdHandler getUrlIdHandler() { + public StreamUrlIdHandler getUrlIdHandlerInstance() { return new YoutubeStreamUrlIdHandler(); } } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 00857bbba..9f33e8cc5 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -10,6 +10,7 @@ import org.jsoup.nodes.Element; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.ScriptableObject; +import org.schabi.newpipe.extractor.AudioStream; import org.schabi.newpipe.extractor.ExtractionException; import org.schabi.newpipe.extractor.Downloader; import org.schabi.newpipe.extractor.Parser; @@ -19,6 +20,7 @@ import org.schabi.newpipe.extractor.StreamPreviewInfo; import org.schabi.newpipe.extractor.StreamUrlIdHandler; import org.schabi.newpipe.extractor.StreamExtractor; import org.schabi.newpipe.extractor.MediaFormat; +import org.schabi.newpipe.extractor.VideoStream; import java.io.IOException; import java.util.List; @@ -47,7 +49,7 @@ import java.util.regex.Pattern; * along with NewPipe. If not, see . */ -public class YoutubeStreamExtractor implements StreamExtractor { +public class YoutubeStreamExtractor extends StreamExtractor { // exceptions @@ -181,7 +183,9 @@ public class YoutubeStreamExtractor implements StreamExtractor { private Downloader downloader; - public YoutubeStreamExtractor(String pageUrl, Downloader dl) throws ExtractionException, IOException { + public YoutubeStreamExtractor(String pageUrl, Downloader dl, int serviceId) + throws ExtractionException, IOException { + super(pageUrl, dl, serviceId); //most common videoInfo fields are now set in our superclass, for all services downloader = dl; this.pageUrl = pageUrl; @@ -430,8 +434,8 @@ public class YoutubeStreamExtractor implements StreamExtractor { @Override - public List getAudioStreams() throws ParsingException { - Vector audioStreams = new Vector<>(); + public List getAudioStreams() throws ParsingException { + Vector audioStreams = new Vector<>(); try{ String encoded_url_map; // playerArgs could be null if the video is age restricted @@ -458,7 +462,7 @@ public class YoutubeStreamExtractor implements StreamExtractor { + decryptSignature(tags.get("s"), decryptionCode); } - audioStreams.add(new StreamInfo.AudioStream(streamUrl, + audioStreams.add(new AudioStream(streamUrl, itagItem.mediaFormatId, itagItem.bandWidth, itagItem.samplingRate)); @@ -472,8 +476,8 @@ public class YoutubeStreamExtractor implements StreamExtractor { } @Override - public List getVideoStreams() throws ParsingException { - Vector videoStreams = new Vector<>(); + public List getVideoStreams() throws ParsingException { + Vector videoStreams = new Vector<>(); try{ String encoded_url_map; @@ -501,7 +505,7 @@ public class YoutubeStreamExtractor implements StreamExtractor { streamUrl = streamUrl + "&signature=" + decryptSignature(tags.get("s"), decryptionCode); } - videoStreams.add(new StreamInfo.VideoStream( + videoStreams.add(new VideoStream( streamUrl, itagItem.mediaFormatId, itagItem.resolutionString)); @@ -524,7 +528,7 @@ public class YoutubeStreamExtractor implements StreamExtractor { } @Override - public List getVideoOnlyStreams() throws ParsingException { + public List getVideoOnlyStreams() throws ParsingException { return null; } From 0c716c12d769cea04025abe0090462bea2c6cc4f Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 4 Mar 2016 13:51:50 +0100 Subject: [PATCH 2/2] fixed viewcount test again --- .../newpipe/extractor/youtube/YoutubeSearchEngineTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java index 07b1f27ad..39f04f4a0 100644 --- a/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java +++ b/app/src/androidTest/java/org/schabi/newpipe/extractor/youtube/YoutubeSearchEngineTest.java @@ -43,7 +43,7 @@ public class YoutubeSearchEngineTest extends AndroidTestCase { SearchEngine engine = ServiceList.getService("Youtube") .getSearchEngineInstance(new Downloader()); - result = engine.search("bla", + result = engine.search("lefloid", 0, "de", new Downloader()).getSearchResult(); suggestionReply = engine.suggestionList("hello","de",new Downloader()); } @@ -96,7 +96,7 @@ public class YoutubeSearchEngineTest extends AndroidTestCase { // that specific link used for this test, there are no videos with less // than 10.000 views, so we can test against that. for(StreamPreviewInfo i : result.resultList) { - assertTrue(Long.toString(i.view_count), i.view_count >= 10000); + assertTrue(i.title + ": " + Long.toString(i.view_count), i.view_count >= 10000); } }