From 3902de00a6924d5d36290d903ad0c2259fc4dd68 Mon Sep 17 00:00:00 2001 From: tonakriz Date: Wed, 22 Nov 2017 18:45:49 +0100 Subject: [PATCH] Removed duplicated code --- .../soundcloud/SoundcloudStreamExtractor.java | 44 +----------------- .../youtube/YoutubeStreamExtractor.java | 44 +----------------- .../extractor/stream/StreamExtractor.java | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 86 deletions(-) diff --git a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java index ba2cd2d14..0624c2ef8 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java @@ -76,49 +76,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor { @Override public long getTimeStamp() throws ParsingException { - String timeStamp; - try { - timeStamp = Parser.matchGroup1("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)", getOriginalUrl()); - } catch (Parser.RegexException e) { - // catch this instantly since an url does not necessarily have to have a time stamp - - // -2 because well the testing system will then know its the regex that failed :/ - // not good i know - return -2; - } - - if (!timeStamp.isEmpty()) { - try { - String secondsString = ""; - String minutesString = ""; - String hoursString = ""; - try { - secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp); - minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp); - hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp); - } catch (Exception e) { - //it could be that time is given in another method - if (secondsString.isEmpty() //if nothing was got, - && minutesString.isEmpty()//treat as unlabelled seconds - && hoursString.isEmpty()) { - secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp); - } - } - - int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); - int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); - int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); - - //don't trust BODMAS! - return seconds + (60 * minutes) + (3600 * hours); - //Log.d(TAG, "derived timestamp value:"+ret); - //the ordering varies internationally - } catch (ParsingException e) { - throw new ParsingException("Could not get timestamp.", e); - } - } else { - return 0; - } + return getTimestampSeconds("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)"); } @Override diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 8dbb9ead3..9036dfcd9 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -203,49 +203,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { */ @Override public long getTimeStamp() throws ParsingException { - String timeStamp; - try { - timeStamp = Parser.matchGroup1("((#|&|\\?)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)", getOriginalUrl()); - } catch (Parser.RegexException e) { - // catch this instantly since an url does not necessarily have to have a time stamp - - // -2 because well the testing system will then know its the regex that failed :/ - // not good i know - return -2; - } - - if (!timeStamp.isEmpty()) { - try { - String secondsString = ""; - String minutesString = ""; - String hoursString = ""; - try { - secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp); - minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp); - hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp); - } catch (Exception e) { - //it could be that time is given in another method - if (secondsString.isEmpty() //if nothing was got, - && minutesString.isEmpty()//treat as unlabelled seconds - && hoursString.isEmpty()) { - secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp); - } - } - - int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); - int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); - int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); - - //don't trust BODMAS! - return seconds + (60 * minutes) + (3600 * hours); - //Log.d(TAG, "derived timestamp value:"+ret); - //the ordering varies internationally - } catch (ParsingException e) { - throw new ParsingException("Could not get timestamp.", e); - } - } else { - return 0; - } + return getTimestampSeconds("((#|&|\\?)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)"); } @Override diff --git a/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java index c9b1edd31..8ba9768bd 100644 --- a/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java @@ -25,6 +25,7 @@ import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.utils.Parser; import java.io.IOException; import java.util.List; @@ -51,6 +52,50 @@ public abstract class StreamExtractor extends Extractor { public abstract long getLength() throws ParsingException; public abstract long getTimeStamp() throws ParsingException; + protected long getTimestampSeconds(String regexPattern) throws ParsingException { + String timeStamp; + try { + timeStamp = Parser.matchGroup1(regexPattern, getOriginalUrl()); + } catch (Parser.RegexException e) { + // catch this instantly since an url does not necessarily have to have a time stamp + + // -2 because well the testing system will then know its the regex that failed :/ + // not good i know + return -2; + } + + if (!timeStamp.isEmpty()) { + try { + String secondsString = ""; + String minutesString = ""; + String hoursString = ""; + try { + secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp); + minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp); + hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp); + } catch (Exception e) { + //it could be that time is given in another method + if (secondsString.isEmpty() //if nothing was got, + && minutesString.isEmpty()//treat as unlabelled seconds + && hoursString.isEmpty()) { + secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp); + } + } + + int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString); + int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString); + int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString); + + //don't trust BODMAS! + return seconds + (60 * minutes) + (3600 * hours); + //Log.d(TAG, "derived timestamp value:"+ret); + //the ordering varies internationally + } catch (ParsingException e) { + throw new ParsingException("Could not get timestamp.", e); + } + } else { + return 0; + }}; public abstract long getViewCount() throws ParsingException; public abstract long getLikeCount() throws ParsingException;