|
1 | | -package org.schabi.newpipe.extractor.stream; |
2 | | - |
3 | | -import com.grack.nanojson.JsonObject; |
4 | | -import com.grack.nanojson.JsonParser; |
5 | | -import com.grack.nanojson.JsonParserException; |
6 | | - |
7 | | -import org.schabi.newpipe.extractor.MediaFormat; |
8 | | -import org.schabi.newpipe.extractor.NewPipe; |
9 | | -import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
10 | | -import org.schabi.newpipe.extractor.exceptions.ParsingException; |
11 | | -import org.schabi.newpipe.extractor.utils.ExtractorLogger; |
12 | | -import org.schabi.newpipe.extractor.utils.Parser; |
13 | | - |
14 | | -import java.io.IOException; |
15 | | -import java.util.regex.Pattern; |
16 | | - |
17 | | -import javax.annotation.Nonnull; |
18 | | - |
19 | | -public final class SoundcloudHlsUtils { |
20 | | - private static final String TAG = HlsAudioStream.class.getSimpleName(); |
21 | | - private static final Pattern MP3_HLS_PATTERN = |
22 | | - Pattern.compile("https://cf-hls-media\\.sndcdn.com/playlist/" |
23 | | - + "([a-zA-Z0-9]+)\\.128\\.mp3/playlist\\.m3u8"); |
24 | | - private static final Pattern AAC_HLS_PATTERN = |
25 | | - Pattern.compile("https://playback\\.media-streaming\\.soundcloud\\.cloud/" |
26 | | - + "([a-zA-Z0-9]+)/aac_160k/[a-f0-9\\-]+/playlist\\.m3u8"); |
27 | | - private static final Pattern OPUS_HLS_PATTERN = |
28 | | - Pattern.compile("https://cf-hls-opus-media\\.sndcdn\\.com/" |
29 | | - + "playlist/([a-zA-Z0-9]+)\\.64\\.opus/playlist\\.m3u8"); |
30 | | - |
31 | | - private SoundcloudHlsUtils() { } |
32 | | - |
33 | | - /** |
34 | | - * Calls the API endpoint url for this stream to get the url for retrieving the |
35 | | - * actual byte data for playback (returns the m3u8 playlist url for HLS streams, |
36 | | - * and the url to get the full binary track for progressives streams)<p> |
37 | | - * |
38 | | - * NOTE: this returns a different url every time! (for SoundCloud) |
39 | | - * @param apiStreamUrl The url to call to get the actual stream data url |
40 | | - * @return The url for playing the audio (e.g. playlist.m3u8) |
41 | | - * @throws IOException If there's a problem calling the endpoint |
42 | | - * @throws ExtractionException for the same reason |
43 | | - */ |
44 | | - public static String getStreamContentUrl(final String apiStreamUrl) |
45 | | - throws IOException, ExtractionException { |
46 | | - ExtractorLogger.d(TAG, "Fetching content url for " + apiStreamUrl); |
47 | | - final String response = NewPipe.getDownloader() |
48 | | - .get(apiStreamUrl) |
49 | | - .validateResponseCode() |
50 | | - .responseBody(); |
51 | | - final JsonObject urlObject; |
52 | | - try { |
53 | | - urlObject = JsonParser.object().from(response); |
54 | | - } catch (final JsonParserException e) { |
55 | | - // TODO: Improve error message. |
56 | | - throw new ParsingException("Could not parse stream content from URL (" |
57 | | - + response + ")", e); |
58 | | - } |
59 | | - |
60 | | - return urlObject.getString("url"); |
61 | | - } |
62 | | - |
63 | | - @Nonnull |
64 | | - public static String extractHlsPlaylistId(final String hlsPlaylistUrl, |
65 | | - final MediaFormat mediaFormat) |
66 | | - throws ExtractionException { |
67 | | - switch (mediaFormat) { |
68 | | - case MP3: return extractHlsMp3PlaylistId(hlsPlaylistUrl); |
69 | | - case M4A: return extractHlsAacPlaylistId(hlsPlaylistUrl); |
70 | | - case OPUS: return extractHlsOpusPlaylistId(hlsPlaylistUrl); |
71 | | - default: |
72 | | - throw new IllegalArgumentException("Unsupported media format: " + mediaFormat); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - private static String extractHlsMp3PlaylistId(final String hlsPlaylistUrl) |
77 | | - throws ExtractionException { |
78 | | - return Parser.matchGroup1(MP3_HLS_PATTERN, hlsPlaylistUrl); |
79 | | - } |
80 | | - |
81 | | - private static String extractHlsAacPlaylistId(final String hlsPlaylistUrl) |
82 | | - throws ExtractionException { |
83 | | - return Parser.matchGroup1(AAC_HLS_PATTERN, hlsPlaylistUrl); |
84 | | - } |
85 | | - |
86 | | - private static String extractHlsOpusPlaylistId(final String hlsPlaylistUrl) |
87 | | - throws ExtractionException { |
88 | | - return Parser.matchGroup1(OPUS_HLS_PATTERN, hlsPlaylistUrl); |
89 | | - } |
90 | | -} |
| 1 | +package org.schabi.newpipe.extractor.stream; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonObject; |
| 4 | +import com.grack.nanojson.JsonParser; |
| 5 | +import com.grack.nanojson.JsonParserException; |
| 6 | + |
| 7 | +import org.schabi.newpipe.extractor.MediaFormat; |
| 8 | +import org.schabi.newpipe.extractor.NewPipe; |
| 9 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 10 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 11 | +import org.schabi.newpipe.extractor.utils.ExtractorLogger; |
| 12 | +import org.schabi.newpipe.extractor.utils.Parser; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +import javax.annotation.Nonnull; |
| 18 | + |
| 19 | +public final class SoundcloudHlsUtils { |
| 20 | + private static final String TAG = HlsAudioStream.class.getSimpleName(); |
| 21 | + private static final Pattern MP3_HLS_PATTERN = |
| 22 | + Pattern.compile("https://cf-hls-media\\.sndcdn.com/playlist/" |
| 23 | + + "([a-zA-Z0-9]+)\\.128\\.mp3/playlist\\.m3u8"); |
| 24 | + private static final Pattern AAC_HLS_PATTERN = |
| 25 | + Pattern.compile("https://playback\\.media-streaming\\.soundcloud\\.cloud/" |
| 26 | + + "([a-zA-Z0-9]+)/aac_160k/[a-f0-9\\-]+/playlist\\.m3u8"); |
| 27 | + private static final Pattern OPUS_HLS_PATTERN = |
| 28 | + Pattern.compile("https://cf-hls-opus-media\\.sndcdn\\.com/" |
| 29 | + + "playlist/([a-zA-Z0-9]+)\\.64\\.opus/playlist\\.m3u8"); |
| 30 | + |
| 31 | + private SoundcloudHlsUtils() { } |
| 32 | + |
| 33 | + /** |
| 34 | + * Calls the API endpoint url for this stream to get the url for retrieving the |
| 35 | + * actual byte data for playback (returns the m3u8 playlist url for HLS streams, |
| 36 | + * and the url to get the full binary track for progressives streams)<p> |
| 37 | + * |
| 38 | + * NOTE: this returns a different url every time! (for SoundCloud) |
| 39 | + * @param apiStreamUrl The url to call to get the actual stream data url |
| 40 | + * @return The url for playing the audio (e.g. playlist.m3u8) |
| 41 | + * @throws IOException If there's a problem calling the endpoint |
| 42 | + * @throws ExtractionException for the same reason |
| 43 | + */ |
| 44 | + public static String getStreamContentUrl(final String apiStreamUrl) |
| 45 | + throws IOException, ExtractionException { |
| 46 | + ExtractorLogger.d(TAG, "Fetching content url for {url}", apiStreamUrl); |
| 47 | + final String response = NewPipe.getDownloader() |
| 48 | + .get(apiStreamUrl) |
| 49 | + .validateResponseCode() |
| 50 | + .responseBody(); |
| 51 | + final JsonObject urlObject; |
| 52 | + try { |
| 53 | + urlObject = JsonParser.object().from(response); |
| 54 | + } catch (final JsonParserException e) { |
| 55 | + // TODO: Improve error message. |
| 56 | + throw new ParsingException("Could not parse stream content from URL (" |
| 57 | + + response + ")", e); |
| 58 | + } |
| 59 | + |
| 60 | + return urlObject.getString("url"); |
| 61 | + } |
| 62 | + |
| 63 | + @Nonnull |
| 64 | + public static String extractHlsPlaylistId(final String hlsPlaylistUrl, |
| 65 | + final MediaFormat mediaFormat) |
| 66 | + throws ExtractionException { |
| 67 | + switch (mediaFormat) { |
| 68 | + case MP3: return extractHlsMp3PlaylistId(hlsPlaylistUrl); |
| 69 | + case M4A: return extractHlsAacPlaylistId(hlsPlaylistUrl); |
| 70 | + case OPUS: return extractHlsOpusPlaylistId(hlsPlaylistUrl); |
| 71 | + default: |
| 72 | + throw new IllegalArgumentException("Unsupported media format: " + mediaFormat); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static String extractHlsMp3PlaylistId(final String hlsPlaylistUrl) |
| 77 | + throws ExtractionException { |
| 78 | + return Parser.matchGroup1(MP3_HLS_PATTERN, hlsPlaylistUrl); |
| 79 | + } |
| 80 | + |
| 81 | + private static String extractHlsAacPlaylistId(final String hlsPlaylistUrl) |
| 82 | + throws ExtractionException { |
| 83 | + return Parser.matchGroup1(AAC_HLS_PATTERN, hlsPlaylistUrl); |
| 84 | + } |
| 85 | + |
| 86 | + private static String extractHlsOpusPlaylistId(final String hlsPlaylistUrl) |
| 87 | + throws ExtractionException { |
| 88 | + return Parser.matchGroup1(OPUS_HLS_PATTERN, hlsPlaylistUrl); |
| 89 | + } |
| 90 | +} |
0 commit comments