Skip to content

Commit e11c6e3

Browse files
committed
Code refactoring
* Add getters * Add javadoc * Use Mediaformat instead of the id of media format
1 parent 98358cb commit e11c6e3

13 files changed

Lines changed: 321 additions & 31 deletions

File tree

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,44 @@
2323
import java.io.Serializable;
2424

2525
public abstract class InfoItem implements Serializable {
26-
public enum InfoType {
27-
STREAM,
28-
PLAYLIST,
29-
CHANNEL
30-
}
31-
3226
public final InfoType info_type;
27+
public int service_id = -1;
28+
public String url;
29+
public String name;
30+
public String thumbnail_url;
3331

3432
public InfoItem(InfoType infoType) {
3533
this.info_type = infoType;
3634
}
3735

38-
public int service_id = -1;
39-
public String url;
40-
public String name;
41-
public String thumbnail_url;
36+
public InfoType getInfoType() {
37+
return info_type;
38+
}
39+
40+
public int getServiceId() {
41+
return service_id;
42+
}
43+
44+
public String getUrl() {
45+
return url;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getThumbnailUrl() {
53+
return thumbnail_url;
54+
}
4255

4356
@Override
4457
public String toString() {
4558
return getClass().getSimpleName() + "[url=\"" + url + "\", name=\"" + name + "\"]";
4659
}
60+
61+
public enum InfoType {
62+
STREAM,
63+
PLAYLIST,
64+
CHANNEL
65+
}
4766
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/**
26-
* Static data about various media formats support by Newpipe, eg mime type, extension
26+
* Static data about various media formats support by NewPipe, eg mime type, extension
2727
*/
2828

2929
public enum MediaFormat {
@@ -103,4 +103,40 @@ public static MediaFormat getFromMimeType(String mimeType) {
103103
}
104104
return null;
105105
}
106+
107+
/**
108+
* Get the media format by it's id.
109+
* @param id the id
110+
* @return the id of the media format or null.
111+
*/
112+
public static MediaFormat getFormatById(int id) {
113+
for (MediaFormat vf: values()) {
114+
if (vf.id == id) return vf;
115+
}
116+
return null;
117+
}
118+
119+
/**
120+
* Get the name of the format
121+
* @return the name of the format
122+
*/
123+
public String getName() {
124+
return name;
125+
}
126+
127+
/**
128+
* Get the filename extension
129+
* @return the filename extension
130+
*/
131+
public String getSuffix() {
132+
return suffix;
133+
}
134+
135+
/**
136+
* Get the mime type
137+
* @return the mime type
138+
*/
139+
public String getMimeType() {
140+
return mimeType;
141+
}
106142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
129129

130130
String mp3Url = responseObject.getString("http_mp3_128_url");
131131
if (mp3Url != null && !mp3Url.isEmpty()) {
132-
audioStreams.add(new AudioStream(mp3Url, MediaFormat.MP3.id, 128));
132+
audioStreams.add(new AudioStream(mp3Url, MediaFormat.MP3, 128));
133133
} else {
134134
throw new ExtractionException("Could not get SoundCloud's track audio url");
135135
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class ItagItem {
7676
new ItagItem(313, VIDEO_ONLY, WEBM, "2160p"),
7777
new ItagItem(315, VIDEO_ONLY, WEBM, "2160p60", 60)
7878
};
79-
8079
/*//////////////////////////////////////////////////////////////////////////
8180
// Utils
8281
//////////////////////////////////////////////////////////////////////////*/
@@ -115,7 +114,7 @@ public enum ItagType {
115114
public ItagItem(int id, ItagType type, MediaFormat format, String resolution) {
116115
this.id = id;
117116
this.itagType = type;
118-
this.mediaFormatId = format.id;
117+
this.mediaFormat = format;
119118
this.resolutionString = resolution;
120119
this.fps = 30;
121120
}
@@ -128,21 +127,27 @@ public ItagItem(int id, ItagType type, MediaFormat format, String resolution) {
128127
public ItagItem(int id, ItagType type, MediaFormat format, String resolution, int fps) {
129128
this.id = id;
130129
this.itagType = type;
131-
this.mediaFormatId = format.id;
130+
this.mediaFormat = format;
132131
this.resolutionString = resolution;
133132
this.fps = fps;
134133
}
135134

136135
public ItagItem(int id, ItagType type, MediaFormat format, int avgBitrate) {
137136
this.id = id;
138137
this.itagType = type;
139-
this.mediaFormatId = format.id;
138+
this.mediaFormat = format;
140139
this.avgBitrate = avgBitrate;
141140
}
142141

142+
private final MediaFormat mediaFormat;
143+
144+
145+
public MediaFormat getMediaFormat() {
146+
return mediaFormat;
147+
}
148+
143149
public int id;
144150
public ItagType itagType;
145-
public int mediaFormatId;
146151

147152
// Audio fields
148153
public int avgBitrate = -1;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public long getLikeCount() throws ParsingException {
223223
try {
224224
likesString = button.select("span.yt-uix-button-content").first().text();
225225
} catch (NullPointerException e) {
226-
//if this ckicks in our button has no content and thefore likes/dislikes are disabled
226+
//if this kicks in our button has no content and therefore likes/dislikes are disabled
227227
return -1;
228228
}
229229
return Integer.parseInt(Utils.removeNonDigitCharacters(likesString));
@@ -329,7 +329,7 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
329329
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.AUDIO).entrySet()) {
330330
ItagItem itag = entry.getValue();
331331

332-
AudioStream audioStream = new AudioStream(entry.getKey(), itag.mediaFormatId, itag.avgBitrate);
332+
AudioStream audioStream = new AudioStream(entry.getKey(), itag.getMediaFormat(), itag.avgBitrate);
333333
if (!Stream.containSimilarStream(audioStream, audioStreams)) {
334334
audioStreams.add(audioStream);
335335
}
@@ -348,7 +348,7 @@ public List<VideoStream> getVideoStreams() throws IOException, ExtractionExcepti
348348
for (Map.Entry<String, ItagItem> entry : getItags(URL_ENCODED_FMT_STREAM_MAP, ItagItem.ItagType.VIDEO).entrySet()) {
349349
ItagItem itag = entry.getValue();
350350

351-
VideoStream videoStream = new VideoStream(entry.getKey(), itag.mediaFormatId, itag.resolutionString);
351+
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString);
352352
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
353353
videoStreams.add(videoStream);
354354
}
@@ -367,7 +367,7 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
367367
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.VIDEO_ONLY).entrySet()) {
368368
ItagItem itag = entry.getValue();
369369

370-
VideoStream videoStream = new VideoStream(entry.getKey(), itag.mediaFormatId, itag.resolutionString, true);
370+
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString, true);
371371
if (!Stream.containSimilarStream(videoStream, videoOnlyStreams)) {
372372
videoOnlyStreams.add(videoStream);
373373
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@
2020
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23+
import org.schabi.newpipe.extractor.MediaFormat;
24+
2325
public class AudioStream extends Stream {
2426
public int average_bitrate = -1;
2527

28+
/**
29+
* Create a new audio stream
30+
* @param url the url
31+
* @param format the id of the format
32+
* @param averageBitrate the average bit rate
33+
* @deprecated use {@link AudioStream#AudioStream(String, MediaFormat, int)} instead
34+
*/
35+
@Deprecated
2636
public AudioStream(String url, int format, int averageBitrate) {
37+
this(url, MediaFormat.getFormatById(format), averageBitrate);
38+
}
39+
40+
/**
41+
* Create a new audio stream
42+
* @param url the url
43+
* @param format the format
44+
* @param averageBitrate the average bitrate
45+
*/
46+
public AudioStream(String url, MediaFormat format, int averageBitrate) {
2747
super(url, format);
2848
this.average_bitrate = averageBitrate;
2949
}
@@ -33,4 +53,12 @@ public boolean equalStats(Stream cmp) {
3353
return super.equalStats(cmp) && cmp instanceof AudioStream &&
3454
average_bitrate == ((AudioStream) cmp).average_bitrate;
3555
}
56+
57+
/**
58+
* Get the average bitrate
59+
* @return the average bitrate or -1
60+
*/
61+
public int getAverageBitrate() {
62+
return average_bitrate;
63+
}
3664
}

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

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

3+
import org.schabi.newpipe.extractor.MediaFormat;
4+
35
import java.io.Serializable;
46
import java.util.List;
57

68
public abstract class Stream implements Serializable {
9+
private final MediaFormat mediaFormat;
710
public String url;
811
public int format = -1;
912

10-
public Stream(String url, int format) {
13+
public Stream(String url, MediaFormat format) {
1114
this.url = url;
12-
this.format = format;
15+
this.format = format.id;
16+
this.mediaFormat = format;
1317
}
1418

1519
/**
@@ -36,4 +40,12 @@ public static boolean containSimilarStream(Stream stream, List<? extends Stream>
3640
}
3741
return false;
3842
}
43+
44+
public String getUrl() {
45+
return url;
46+
}
47+
48+
public MediaFormat getFormat() {
49+
return mediaFormat;
50+
}
3951
}

0 commit comments

Comments
 (0)