Skip to content

Commit 91aa52f

Browse files
committed
Merge branch 'dev'
2 parents e5125c1 + 0501a2f commit 91aa52f

63 files changed

Lines changed: 515 additions & 429 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.extractor;
22

3+
import edu.umd.cs.findbugs.annotations.NonNull;
34
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
45
import org.schabi.newpipe.extractor.exceptions.ParsingException;
56

@@ -14,42 +15,28 @@ public abstract class Extractor {
1415
*/
1516
private final StreamingService service;
1617

17-
/**
18-
* Dirty/original url that was passed in the constructor.
19-
* <p>
20-
* What makes a url "dirty" or not is, for example, the additional parameters
21-
* (not important as—in this case—the id):
22-
* <pre>
23-
* https://www.youtube.com/watch?v=a9Zf_258aTI<i>&amp;t=4s</i> → <i><b>&amp;t=4s</b></i>
24-
* </pre>
25-
* But as you can imagine, the time parameter is very important when calling {@link org.schabi.newpipe.extractor.stream.StreamExtractor#getTimeStamp()}.
26-
*/
27-
private final String originalUrl;
18+
private final UrlIdHandler urlIdHandler;
2819

29-
/**
30-
* The cleaned url, result of passing the {@link #originalUrl} to the associated urlIdHandler ({@link #getUrlIdHandler()}).
31-
* <p>
32-
* Is lazily-cleaned by calling {@link #getCleanUrl()}
33-
*/
3420
@Nullable
35-
private String cleanUrl;
3621
private boolean pageFetched = false;
3722
private final Downloader downloader;
3823

39-
public Extractor(final StreamingService service, final String url) {
24+
public Extractor(final StreamingService service, final UrlIdHandler urlIdHandler) {
4025
if(service == null) throw new NullPointerException("service is null");
41-
if(url == null) throw new NullPointerException("url is null");
26+
if(urlIdHandler == null) throw new NullPointerException("UrlIdHandler is null");
4227
this.service = service;
43-
this.originalUrl = url;
28+
this.urlIdHandler = urlIdHandler;
4429
this.downloader = NewPipe.getDownloader();
4530
if(downloader == null) throw new NullPointerException("downloader is null");
4631
}
4732

4833
/**
49-
* @return a {@link UrlIdHandler} of the current extractor type (e.g. a ChannelExtractor should return a channel url handler).
34+
* @return The {@link UrlIdHandler} of the current extractor object (e.g. a ChannelExtractor should return a channel url handler).
5035
*/
5136
@Nonnull
52-
protected abstract UrlIdHandler getUrlIdHandler() throws ParsingException;
37+
protected UrlIdHandler getUrlIdHandler() {
38+
return urlIdHandler;
39+
}
5340

5441
/**
5542
* Fetch the current page.
@@ -79,7 +66,9 @@ protected boolean isPageFetched() {
7966
public abstract void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException;
8067

8168
@Nonnull
82-
public abstract String getId() throws ParsingException;
69+
public String getId() throws ParsingException {
70+
return urlIdHandler.getId();
71+
}
8372

8473
/**
8574
* Get the name
@@ -90,26 +79,13 @@ protected boolean isPageFetched() {
9079
public abstract String getName() throws ParsingException;
9180

9281
@Nonnull
93-
public String getOriginalUrl() {
94-
return originalUrl;
82+
public String getOriginalUrl() throws ParsingException {
83+
return urlIdHandler.getOriginalUrl();
9584
}
9685

97-
/**
98-
* Get a clean url and as a fallback the original url.
99-
* @return the clean url or the original url
100-
*/
10186
@Nonnull
102-
public String getCleanUrl() {
103-
if (cleanUrl != null && !cleanUrl.isEmpty()) return cleanUrl;
104-
105-
try {
106-
cleanUrl = getUrlIdHandler().cleanUrl(originalUrl);
107-
} catch (Exception e) {
108-
cleanUrl = null;
109-
// Fallback to the original url
110-
return originalUrl;
111-
}
112-
return cleanUrl;
87+
public String getUrl() throws ParsingException {
88+
return urlIdHandler.getUrl();
11389
}
11490

11591
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313
public abstract class ListExtractor<R extends InfoItem> extends Extractor {
1414

15-
public ListExtractor(StreamingService service, String url) {
16-
super(service, url);
15+
public ListExtractor(StreamingService service, ListUrlIdHandler urlIdHandler) {
16+
super(service, urlIdHandler);
1717
}
1818

1919
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
4+
5+
public abstract class ListUrlIdHandler extends UrlIdHandler {
6+
7+
protected String[] contentFilter;
8+
protected String sortFilter;
9+
10+
public ListUrlIdHandler setQuery(String id, String[] contentFilter, String softFilter) throws ParsingException {
11+
setId(id);
12+
this.contentFilter = contentFilter;
13+
this.sortFilter = softFilter;
14+
return this;
15+
}
16+
17+
public ListUrlIdHandler setUrl(String url) throws ParsingException {
18+
return (ListUrlIdHandler) super.setUrl(url);
19+
}
20+
21+
public ListUrlIdHandler setId(String id) throws ParsingException {
22+
return (ListUrlIdHandler) super.setId(id);
23+
}
24+
25+
/**
26+
* Will returns content filter the corresponding extractor can handle like "channels", "videos", "music", etc.
27+
*
28+
* @return filter that can be applied when building a query for getting a list
29+
*/
30+
public String[] getAvailableContentFilter() {
31+
return new String[0];
32+
}
33+
34+
/**
35+
* Will returns sort filter the corresponding extractor can handle like "A-Z", "oldest first", "size", etc.
36+
*
37+
* @return filter that can be applied when building a query for getting a list
38+
*/
39+
public String[] getAvailableSortFilter() {
40+
return new String[0];
41+
}
42+
}

extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
44
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
5+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
56
import org.schabi.newpipe.extractor.kiosk.KioskList;
67
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
78
import org.schabi.newpipe.extractor.search.SearchEngine;
@@ -62,22 +63,52 @@ public String toString() {
6263
return serviceId + ":" + serviceInfo.getName();
6364
}
6465

66+
////////////////////////////////////////////
67+
// Url Id handler
68+
////////////////////////////////////////////
6569
public abstract UrlIdHandler getStreamUrlIdHandler();
66-
public abstract UrlIdHandler getChannelUrlIdHandler();
67-
public abstract UrlIdHandler getPlaylistUrlIdHandler();
70+
public abstract ListUrlIdHandler getChannelUrlIdHandler();
71+
public abstract ListUrlIdHandler getPlaylistUrlIdHandler();
6872

73+
74+
////////////////////////////////////////////
75+
// Extractor
76+
////////////////////////////////////////////
6977
public abstract SearchEngine getSearchEngine();
7078
public abstract SuggestionExtractor getSuggestionExtractor();
71-
public abstract StreamExtractor getStreamExtractor(String url);
72-
public abstract KioskList getKioskList() throws ExtractionException;
73-
public abstract ChannelExtractor getChannelExtractor(String url);
74-
public abstract PlaylistExtractor getPlaylistExtractor(String url);
7579
public abstract SubscriptionExtractor getSubscriptionExtractor();
80+
public abstract KioskList getKioskList() throws ExtractionException;
81+
82+
public abstract ChannelExtractor getChannelExtractor(ListUrlIdHandler urlIdHandler) throws ExtractionException;
83+
public abstract PlaylistExtractor getPlaylistExtractor(ListUrlIdHandler urlIdHandler) throws ExtractionException;
84+
public abstract StreamExtractor getStreamExtractor(UrlIdHandler urlIdHandler) throws ExtractionException;
85+
86+
public ChannelExtractor getChannelExtractor(String id, String[] contentFilter, String sortFilter) throws ExtractionException {
87+
return getChannelExtractor(getChannelUrlIdHandler().setQuery(id, contentFilter, sortFilter));
88+
}
89+
90+
public PlaylistExtractor getPlaylistExtractor(String id, String[] contentFilter, String sortFilter) throws ExtractionException {
91+
return getPlaylistExtractor(getPlaylistUrlIdHandler().setQuery(id, contentFilter, sortFilter));
92+
}
93+
94+
public ChannelExtractor getChannelExtractor(String url) throws ExtractionException {
95+
return getChannelExtractor(getChannelUrlIdHandler().setUrl(url));
96+
}
97+
98+
public PlaylistExtractor getPlaylistExtractor(String url) throws ExtractionException {
99+
return getPlaylistExtractor(getPlaylistUrlIdHandler().setUrl(url));
100+
}
101+
102+
public StreamExtractor getStreamExtractor(String url) throws ExtractionException {
103+
return getStreamExtractor(getStreamUrlIdHandler().setUrl(url));
104+
}
105+
106+
76107

77108
/**
78109
* figure out where the link is pointing to (a channel, video, playlist, etc.)
79110
*/
80-
public final LinkType getLinkTypeByUrl(String url) {
111+
public final LinkType getLinkTypeByUrl(String url) throws ParsingException {
81112
UrlIdHandler sH = getStreamUrlIdHandler();
82113
UrlIdHandler cH = getChannelUrlIdHandler();
83114
UrlIdHandler pH = getPlaylistUrlIdHandler();

extractor/src/main/java/org/schabi/newpipe/extractor/UrlIdHandler.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.extractor;
22

3+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
34
import org.schabi.newpipe.extractor.exceptions.ParsingException;
45

56
/*
@@ -22,16 +23,52 @@
2223
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2324
*/
2425

25-
public interface UrlIdHandler {
26+
public abstract class UrlIdHandler {
2627

27-
String getUrl(String id) throws ParsingException;
28-
String getId(String url) throws ParsingException;
29-
String cleanUrl(String complexUrl) throws ParsingException;
28+
protected String id = "";
29+
protected String originalUrl = "";
30+
31+
public abstract String onGetIdFromUrl(String url) throws ParsingException;
32+
public abstract String getUrl() throws ParsingException;
33+
public abstract boolean onAcceptUrl(final String url) throws ParsingException;
34+
35+
36+
public UrlIdHandler setUrl(String url) throws ParsingException {
37+
if(url == null) throw new IllegalArgumentException("url can not be null");
38+
originalUrl = url;
39+
id = onGetIdFromUrl(url);
40+
return this;
41+
}
42+
43+
public UrlIdHandler setId(String id) throws ParsingException {
44+
if(id == null) throw new IllegalArgumentException("id can not be null");
45+
this.id = id;
46+
if(!acceptUrl(getUrl())) {
47+
throw new ParsingException("Malformed unacceptable url: " + getUrl());
48+
}
49+
return this;
50+
}
51+
52+
public String getId() {
53+
return id;
54+
}
55+
56+
public String getOriginalUrl() throws ParsingException {
57+
return (originalUrl == null || originalUrl.isEmpty())
58+
? getUrl()
59+
: originalUrl;
60+
}
3061

3162
/**
3263
* When a VIEW_ACTION is caught this function will test if the url delivered within the calling
3364
* Intent was meant to be watched with this Service.
3465
* Return false if this service shall not allow to be called through ACTIONs.
3566
*/
36-
boolean acceptUrl(String url);
67+
public boolean acceptUrl(final String url) {
68+
try {
69+
return onAcceptUrl(url);
70+
} catch (Exception e) {
71+
return false;
72+
}
73+
}
3774
}

extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelExtractor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.schabi.newpipe.extractor.channel;
22

33
import org.schabi.newpipe.extractor.ListExtractor;
4+
import org.schabi.newpipe.extractor.ListUrlIdHandler;
45
import org.schabi.newpipe.extractor.StreamingService;
56
import org.schabi.newpipe.extractor.UrlIdHandler;
7+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
68
import org.schabi.newpipe.extractor.exceptions.ParsingException;
79
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
810

@@ -30,8 +32,8 @@
3032

3133
public abstract class ChannelExtractor extends ListExtractor<StreamInfoItem> {
3234

33-
public ChannelExtractor(StreamingService service, String url) {
34-
super(service, url);
35+
public ChannelExtractor(StreamingService service, ListUrlIdHandler urlIdHandler) {
36+
super(service, urlIdHandler);
3537
}
3638

3739
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
5454

5555
// important data
5656
int serviceId = extractor.getServiceId();
57-
String url = extractor.getCleanUrl();
57+
String url = extractor.getUrl();
5858
String originalUrl = extractor.getOriginalUrl();
5959
String id = extractor.getId();
6060
String name = extractor.getName();

extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import org.schabi.newpipe.extractor.ListExtractor;
24+
import org.schabi.newpipe.extractor.ListUrlIdHandler;
2425
import org.schabi.newpipe.extractor.StreamingService;
2526
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2627
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -33,10 +34,9 @@ public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> {
3334
private final String id;
3435

3536
public KioskExtractor(StreamingService streamingService,
36-
String url,
37-
String kioskId)
38-
throws ExtractionException {
39-
super(streamingService, url);
37+
ListUrlIdHandler urlIdHandler,
38+
String kioskId) {
39+
super(streamingService, urlIdHandler);
4040
this.id = kioskId;
4141
}
4242

extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static KioskInfo getInfo(KioskExtractor extractor) throws ExtractionExcep
7171
int serviceId = extractor.getServiceId();
7272
String name = extractor.getName();
7373
String id = extractor.getId();
74-
String url = extractor.getCleanUrl();
74+
String url = extractor.getUrl();
7575
String originalUrl = extractor.getOriginalUrl();
7676

7777
KioskInfo info = new KioskInfo(serviceId, id, url, originalUrl, name);

extractor/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public KioskExtractor getExtractorById(String kioskId, String nextPageUrl)
7373
throw new ExtractionException("No kiosk found with the type: " + kioskId);
7474
} else {
7575
return ke.extractorFactory.createNewKiosk(NewPipe.getService(service_id),
76-
ke.handler.getUrl(kioskId), kioskId);
76+
ke.handler.setId(kioskId).getUrl(), kioskId);
7777
}
7878
}
7979

0 commit comments

Comments
 (0)