Skip to content

Commit 79e49ce

Browse files
committed
Merge branch 'feature-YTsubtitles' of https://github.com/tonakriz/NewPipeExtractor into feature-YTsubtitles
2 parents 320c0d8 + d657e38 commit 79e49ce

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
@@ -75,49 +75,7 @@ public long getLength() {
7575

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

12381
@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
@@ -28,6 +28,7 @@
2828
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2929
import org.schabi.newpipe.extractor.exceptions.ParsingException;
3030
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
31+
import org.schabi.newpipe.extractor.utils.Parser;
3132

3233
import java.io.IOException;
3334
import java.util.List;
@@ -54,6 +55,50 @@ protected UrlIdHandler getUrlIdHandler() throws ParsingException {
5455

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

58103
public abstract long getViewCount() throws ParsingException;
59104
public abstract long getLikeCount() throws ParsingException;

0 commit comments

Comments
 (0)