Skip to content

Commit 21e542e

Browse files
committed
Refactor extractor
- Refactor info classes and extractors - Reformat and fix indentation - Organize packages and classes - Rename variables/methods and fix regex - Change the capitalization - Add methods to playlist extractor
1 parent 7581c12 commit 21e542e

53 files changed

Lines changed: 1047 additions & 906 deletions

Some content is hidden

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

Downloader.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.io.IOException;
66
import java.util.Map;
77

8-
/**
8+
/*
99
* Created by Christian Schabesberger on 28.01.16.
1010
*
1111
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
@@ -27,26 +27,35 @@
2727

2828
public interface Downloader {
2929

30-
/**Download the text file at the supplied URL as in download(String),
30+
/**
31+
* Download the text file at the supplied URL as in download(String),
3132
* but set the HTTP header field "Accept-Language" to the supplied string.
32-
* @param siteUrl the URL of the text file to return the contents of
33+
*
34+
* @param siteUrl the URL of the text file to return the contents of
3335
* @param language the language (usually a 2-character code) to set as the preferred language
3436
* @return the contents of the specified text file
35-
* @throws IOException*/
37+
* @throws IOException
38+
*/
3639
String download(String siteUrl, String language) throws IOException, ReCaptchaException;
3740

38-
/**Download the text file at the supplied URL as in download(String),
41+
/**
42+
* Download the text file at the supplied URL as in download(String),
3943
* but set the HTTP header field "Accept-Language" to the supplied string.
40-
* @param siteUrl the URL of the text file to return the contents of
44+
*
45+
* @param siteUrl the URL of the text file to return the contents of
4146
* @param customProperties set request header properties
4247
* @return the contents of the specified text file
43-
* @throws IOException*/
48+
* @throws IOException
49+
*/
4450
String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException;
4551

46-
/**Download (via HTTP) the text file located at the supplied URL, and return its contents.
52+
/**
53+
* Download (via HTTP) the text file located at the supplied URL, and return its contents.
4754
* Primarily intended for downloading web pages.
55+
*
4856
* @param siteUrl the URL of the text file to download
4957
* @return the contents of the specified text file
50-
* @throws IOException*/
58+
* @throws IOException
59+
*/
5160
String download(String siteUrl) throws IOException, ReCaptchaException;
5261
}

Extractor.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
4+
5+
import java.io.Serializable;
6+
7+
public abstract class Extractor implements Serializable {
8+
private final int serviceId;
9+
private final String url;
10+
private final UrlIdHandler urlIdHandler;
11+
private final StreamInfoItemCollector previewInfoCollector;
12+
13+
public Extractor(UrlIdHandler urlIdHandler, int serviceId, String url) {
14+
this.urlIdHandler = urlIdHandler;
15+
this.serviceId = serviceId;
16+
this.url = url;
17+
this.previewInfoCollector = new StreamInfoItemCollector(urlIdHandler, serviceId);
18+
}
19+
20+
public String getUrl() {
21+
return url;
22+
}
23+
24+
public UrlIdHandler getUrlIdHandler() {
25+
return urlIdHandler;
26+
}
27+
28+
public int getServiceId() {
29+
return serviceId;
30+
}
31+
32+
protected StreamInfoItemCollector getStreamPreviewInfoCollector() {
33+
return previewInfoCollector;
34+
}
35+
}

Info.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.schabi.newpipe.extractor;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
import java.util.Vector;
6+
7+
public abstract class Info implements Serializable {
8+
9+
public int service_id = -1;
10+
/**
11+
* Id of this Info object <br>
12+
* e.g. Youtube: https://www.youtube.com/watch?v=RER5qCTzZ7 > RER5qCTzZ7
13+
*/
14+
public String id = "";
15+
public String url = "";
16+
public String name = "";
17+
18+
public List<Throwable> errors = new Vector<>();
19+
}

InfoItem.java

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

33
import java.io.Serializable;
44

5-
/**
5+
/*
66
* Created by the-scrabi on 11.02.17.
77
*
88
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
@@ -22,7 +22,7 @@
2222
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2323
*/
2424

25-
public interface InfoItem extends Serializable{
25+
public interface InfoItem extends Serializable {
2626
enum InfoType {
2727
STREAM,
2828
PLAYLIST,

InfoItemCollector.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66
import java.util.Vector;
77

8-
/**
8+
/*
99
* Created by Christian Schabesberger on 12.02.17.
1010
*
1111
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
@@ -37,24 +37,29 @@ public InfoItemCollector(int serviceId) {
3737
public List<InfoItem> getItemList() {
3838
return itemList;
3939
}
40+
4041
public List<Throwable> getErrors() {
4142
return errors;
4243
}
44+
4345
protected void addFromCollector(InfoItemCollector otherC) throws ExtractionException {
44-
if(serviceId != otherC.serviceId) {
46+
if (serviceId != otherC.serviceId) {
4547
throw new ExtractionException("Service Id does not equal: "
4648
+ NewPipe.getNameOfService(serviceId)
4749
+ " and " + NewPipe.getNameOfService(otherC.serviceId));
4850
}
4951
errors.addAll(otherC.errors);
5052
itemList.addAll(otherC.itemList);
5153
}
54+
5255
protected void addError(Exception e) {
5356
errors.add(e);
5457
}
58+
5559
protected void addItem(InfoItem item) {
5660
itemList.add(item);
5761
}
62+
5863
protected int getServiceId() {
5964
return serviceId;
6065
}

MediaFormat.java

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

3-
/**
3+
/*
44
* Created by Adam Howard on 08/11/15.
55
*
66
* Copyright (c) Christian Schabesberger <chris.schabesberger@mailbox.org>
@@ -22,7 +22,9 @@
2222
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2323
*/
2424

25-
/**Static data about various media formats support by Newpipe, eg mime type, extension*/
25+
/**
26+
* Static data about various media formats support by Newpipe, eg mime type, extension
27+
*/
2628

2729
public enum MediaFormat {
2830
//video and audio combined formats
@@ -48,35 +50,44 @@ public enum MediaFormat {
4850
this.mimeType = mimeType;
4951
}
5052

51-
/**Return the friendly name of the media format with the supplied id
53+
/**
54+
* Return the friendly name of the media format with the supplied id
55+
*
5256
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
5357
* @return the friendly name of the MediaFormat associated with this ids,
54-
* or an empty String if none match it.*/
58+
* or an empty String if none match it.
59+
*/
5560
public static String getNameById(int ident) {
5661
for (MediaFormat vf : MediaFormat.values()) {
57-
if(vf.id == ident) return vf.name;
62+
if (vf.id == ident) return vf.name;
5863
}
5964
return "";
6065
}
6166

62-
/**Return the file extension of the media format with the supplied id
67+
/**
68+
* Return the file extension of the media format with the supplied id
69+
*
6370
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
6471
* @return the file extension of the MediaFormat associated with this ids,
65-
* or an empty String if none match it.*/
72+
* or an empty String if none match it.
73+
*/
6674
public static String getSuffixById(int ident) {
6775
for (MediaFormat vf : MediaFormat.values()) {
68-
if(vf.id == ident) return vf.suffix;
76+
if (vf.id == ident) return vf.suffix;
6977
}
7078
return "";
7179
}
7280

73-
/**Return the MIME type of the media format with the supplied id
81+
/**
82+
* Return the MIME type of the media format with the supplied id
83+
*
7484
* @param ident the id of the media format. Currently an arbitrary, NewPipe-specific number.
7585
* @return the MIME type of the MediaFormat associated with this ids,
76-
* or an empty String if none match it.*/
86+
* or an empty String if none match it.
87+
*/
7788
public static String getMimeById(int ident) {
7889
for (MediaFormat vf : MediaFormat.values()) {
79-
if(vf.id == ident) return vf.mimeType;
90+
if (vf.id == ident) return vf.mimeType;
8091
}
8192
return "";
8293
}

NewPipe.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.schabi.newpipe.extractor;
22

33
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
4-
import org.schabi.newpipe.extractor.services.youtube.YoutubeService;
54

6-
/**
5+
/*
76
* Created by Christian Schabesberger on 23.08.15.
87
*
98
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
@@ -23,34 +22,37 @@
2322
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2423
*/
2524

26-
/**Provides access to the video streaming services supported by NewPipe.
27-
* Currently only Youtube until the API becomes more stable.*/
25+
/**
26+
* Provides access to the video streaming services supported by NewPipe.
27+
* Currently only Youtube until the API becomes more stable.
28+
*/
2829

2930
@SuppressWarnings("ALL")
3031
public class NewPipe {
32+
private static final String TAG = NewPipe.class.toString();
3133

3234
private NewPipe() {
3335
}
3436

35-
private static final String TAG = NewPipe.class.toString();
36-
37-
3837
private static Downloader downloader = null;
3938

4039
public static StreamingService[] getServices() {
4140
return ServiceList.serviceList;
4241
}
43-
public static StreamingService getService(int serviceId)throws ExtractionException {
44-
for(StreamingService s : ServiceList.serviceList) {
45-
if(s.getServiceId() == serviceId) {
42+
43+
public static StreamingService getService(int serviceId) throws ExtractionException {
44+
for (StreamingService s : ServiceList.serviceList) {
45+
if (s.getServiceId() == serviceId) {
4646
return s;
4747
}
4848
}
4949
return null;
5050
}
51+
5152
public static StreamingService getService(String serviceName) throws ExtractionException {
5253
return ServiceList.serviceList[getIdOfService(serviceName)];
5354
}
55+
5456
public static String getNameOfService(int id) {
5557
try {
5658
return getService(id).getServiceInfo().name;
@@ -60,9 +62,10 @@ public static String getNameOfService(int id) {
6062
return "";
6163
}
6264
}
65+
6366
public static int getIdOfService(String serviceName) {
64-
for(int i = 0; i < ServiceList.serviceList.length; i++) {
65-
if(ServiceList.serviceList[i].getServiceInfo().name.equals(serviceName)) {
67+
for (int i = 0; i < ServiceList.serviceList.length; i++) {
68+
if (ServiceList.serviceList[i].getServiceInfo().name.equals(serviceName)) {
6669
return i;
6770
}
6871
}
@@ -78,8 +81,8 @@ public static Downloader getDownloader() {
7881
}
7982

8083
public static StreamingService getServiceByUrl(String url) {
81-
for(StreamingService s : ServiceList.serviceList) {
82-
if(s.getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) {
84+
for (StreamingService s : ServiceList.serviceList) {
85+
if (s.getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) {
8386
return s;
8487
}
8588
}

ServiceList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.schabi.newpipe.extractor.services.youtube.YoutubeService;
44

5-
/**
5+
/*
66
* Created by the-scrabi on 18.02.17.
77
*/
88

0 commit comments

Comments
 (0)