Skip to content

Commit b37dbbf

Browse files
authored
Merge pull request #43 from tonakriz/refactor
Possible removation of duplicated code
2 parents b9d0941 + 3902de0 commit b37dbbf

3 files changed

Lines changed: 47 additions & 86 deletions

File tree

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,49 +76,7 @@ public long getLength() {
7676

7777
@Override
7878
public long getTimeStamp() throws ParsingException {
79-
String timeStamp;
80-
try {
81-
timeStamp = Parser.matchGroup1("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)", getOriginalUrl());
82-
} catch (Parser.RegexException e) {
83-
// catch this instantly since an url does not necessarily have to have a time stamp
84-
85-
// -2 because well the testing system will then know its the regex that failed :/
86-
// not good i know
87-
return -2;
88-
}
89-
90-
if (!timeStamp.isEmpty()) {
91-
try {
92-
String secondsString = "";
93-
String minutesString = "";
94-
String hoursString = "";
95-
try {
96-
secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp);
97-
minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp);
98-
hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp);
99-
} catch (Exception e) {
100-
//it could be that time is given in another method
101-
if (secondsString.isEmpty() //if nothing was got,
102-
&& minutesString.isEmpty()//treat as unlabelled seconds
103-
&& hoursString.isEmpty()) {
104-
secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp);
105-
}
106-
}
107-
108-
int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString);
109-
int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString);
110-
int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString);
111-
112-
//don't trust BODMAS!
113-
return seconds + (60 * minutes) + (3600 * hours);
114-
//Log.d(TAG, "derived timestamp value:"+ret);
115-
//the ordering varies internationally
116-
} catch (ParsingException e) {
117-
throw new ParsingException("Could not get timestamp.", e);
118-
}
119-
} else {
120-
return 0;
121-
}
79+
return getTimestampSeconds("(#t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)");
12280
}
12381

12482
@Override

src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -203,49 +203,7 @@ public long getLength() throws ParsingException {
203203
*/
204204
@Override
205205
public long getTimeStamp() throws ParsingException {
206-
String timeStamp;
207-
try {
208-
timeStamp = Parser.matchGroup1("((#|&|\\?)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)", getOriginalUrl());
209-
} catch (Parser.RegexException e) {
210-
// catch this instantly since an url does not necessarily have to have a time stamp
211-
212-
// -2 because well the testing system will then know its the regex that failed :/
213-
// not good i know
214-
return -2;
215-
}
216-
217-
if (!timeStamp.isEmpty()) {
218-
try {
219-
String secondsString = "";
220-
String minutesString = "";
221-
String hoursString = "";
222-
try {
223-
secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp);
224-
minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp);
225-
hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp);
226-
} catch (Exception e) {
227-
//it could be that time is given in another method
228-
if (secondsString.isEmpty() //if nothing was got,
229-
&& minutesString.isEmpty()//treat as unlabelled seconds
230-
&& hoursString.isEmpty()) {
231-
secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp);
232-
}
233-
}
234-
235-
int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString);
236-
int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString);
237-
int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString);
238-
239-
//don't trust BODMAS!
240-
return seconds + (60 * minutes) + (3600 * hours);
241-
//Log.d(TAG, "derived timestamp value:"+ret);
242-
//the ordering varies internationally
243-
} catch (ParsingException e) {
244-
throw new ParsingException("Could not get timestamp.", e);
245-
}
246-
} else {
247-
return 0;
248-
}
206+
return getTimestampSeconds("((#|&|\\?)t=\\d{0,3}h?\\d{0,3}m?\\d{1,3}s?)");
249207
}
250208

251209
@Override

src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.schabi.newpipe.extractor.UrlIdHandler;
2626
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2727
import org.schabi.newpipe.extractor.exceptions.ParsingException;
28+
import org.schabi.newpipe.extractor.utils.Parser;
2829

2930
import java.io.IOException;
3031
import java.util.List;
@@ -51,6 +52,50 @@ protected UrlIdHandler getUrlIdHandler() throws ParsingException {
5152

5253
public abstract long getLength() throws ParsingException;
5354
public abstract long getTimeStamp() throws ParsingException;
55+
protected long getTimestampSeconds(String regexPattern) throws ParsingException {
56+
String timeStamp;
57+
try {
58+
timeStamp = Parser.matchGroup1(regexPattern, getOriginalUrl());
59+
} catch (Parser.RegexException e) {
60+
// catch this instantly since an url does not necessarily have to have a time stamp
61+
62+
// -2 because well the testing system will then know its the regex that failed :/
63+
// not good i know
64+
return -2;
65+
}
66+
67+
if (!timeStamp.isEmpty()) {
68+
try {
69+
String secondsString = "";
70+
String minutesString = "";
71+
String hoursString = "";
72+
try {
73+
secondsString = Parser.matchGroup1("(\\d{1,3})s", timeStamp);
74+
minutesString = Parser.matchGroup1("(\\d{1,3})m", timeStamp);
75+
hoursString = Parser.matchGroup1("(\\d{1,3})h", timeStamp);
76+
} catch (Exception e) {
77+
//it could be that time is given in another method
78+
if (secondsString.isEmpty() //if nothing was got,
79+
&& minutesString.isEmpty()//treat as unlabelled seconds
80+
&& hoursString.isEmpty()) {
81+
secondsString = Parser.matchGroup1("t=(\\d+)", timeStamp);
82+
}
83+
}
84+
85+
int seconds = secondsString.isEmpty() ? 0 : Integer.parseInt(secondsString);
86+
int minutes = minutesString.isEmpty() ? 0 : Integer.parseInt(minutesString);
87+
int hours = hoursString.isEmpty() ? 0 : Integer.parseInt(hoursString);
88+
89+
//don't trust BODMAS!
90+
return seconds + (60 * minutes) + (3600 * hours);
91+
//Log.d(TAG, "derived timestamp value:"+ret);
92+
//the ordering varies internationally
93+
} catch (ParsingException e) {
94+
throw new ParsingException("Could not get timestamp.", e);
95+
}
96+
} else {
97+
return 0;
98+
}};
5499

55100
public abstract long getViewCount() throws ParsingException;
56101
public abstract long getLikeCount() throws ParsingException;

0 commit comments

Comments
 (0)