-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the ability to pre-process audio playlists
* this is actually a really good feature * it's also designed in a way that's very easy to extend, which would enable pre-processing more URL patterns - for each URL pattern interceptor: * asked whether it should apply to a requested URL * if so, it implements a line parser that extracts URLs from text * currently, two URL pattern interceptors are implemented - .m3u audio playlists - .html files * this includes URLs that imply a directory index will be returned from the web server in .html format * current implementation only parses audio files from <a> tags - includes logic to dedupe duplicate audio files * when the same audio file is available in multiple formats, applies a ranking of format preference and only returns one URL for the audio file in the most favorable format notes: ====== * this feature had previously been implemented in the javascript SPA - depended upon a CORS proxy to bypass browser security rules to be able to download the playlist URL (ex: m3u, html) * now this feature is available to all clients automagically, and there are no cross-origin concerns * I left the javascript implementation in the SPA for reference sake, but all relevent code has been commented out
- Loading branch information
1 parent
999bf09
commit f021067
Showing
10 changed files
with
570 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...en_bank/exoplayer_airplay_receiver/service/playlist_extractors/BasePlaylistExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.github.warren_bank.exoplayer_airplay_receiver.service.playlist_extractors; | ||
|
||
import com.github.warren_bank.exoplayer_airplay_receiver.utils.StringUtils; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
|
||
public abstract class BasePlaylistExtractor { | ||
|
||
protected abstract boolean isParserForUrl(String strUrl); | ||
|
||
protected abstract void parseLine(String line, URL context, ArrayList<String> matches); | ||
|
||
protected void preParse(URL context) {} | ||
|
||
protected void postParse(URL context, ArrayList<String> matches) {} | ||
|
||
public ArrayList<String> expandPlaylist(String strUrl) { | ||
// https://developer.android.com/reference/java/nio/charset/Charset#standard-charsets | ||
// https://en.wikipedia.org/wiki/Extended_ASCII#ISO_8859_and_proprietary_adaptations | ||
// https://en.wikipedia.org/wiki/ISO/IEC_8859-1 | ||
return expandPlaylist(strUrl, "ISO-8859-1"); | ||
} | ||
|
||
public ArrayList<String> expandPlaylist(String strUrl, String charsetName) { | ||
Charset cs = null; | ||
|
||
if ((charsetName == null) || charsetName.isEmpty()) { | ||
cs = Charset.defaultCharset(); // UTF-8 | ||
} | ||
else { | ||
try { | ||
cs = Charset.forName(charsetName); | ||
} | ||
catch (Exception e) { | ||
cs = Charset.defaultCharset(); // UTF-8 | ||
} | ||
} | ||
|
||
return expandPlaylist(strUrl, cs); | ||
} | ||
|
||
protected ArrayList<String> expandPlaylist(String strUrl, Charset cs) { | ||
if (!isParserForUrl(strUrl)) | ||
return null; | ||
|
||
ArrayList<String> matches = new ArrayList<String>(); | ||
BufferedReader in = null; | ||
|
||
try { | ||
URL url; | ||
String line; | ||
|
||
// ascii encoded | ||
url = new URL(strUrl); | ||
|
||
// Read all the text returned by the server | ||
in = new BufferedReader(new InputStreamReader(url.openStream(), cs)); | ||
|
||
// remove ascii encoding | ||
url = new URL(StringUtils.decodeURL(strUrl)); | ||
|
||
preParse(url); | ||
while ((line = in.readLine()) != null) { | ||
// `line` is one line of text; readLine() strips the newline character(s) | ||
parseLine(line, url, matches); | ||
} | ||
postParse(url, matches); | ||
} | ||
catch (Exception e) { | ||
} | ||
finally { | ||
if (in != null) { | ||
try { | ||
in.close(); | ||
} | ||
catch(Exception e) {} | ||
} | ||
|
||
// normalize that non-null return value must include matches | ||
if (matches.isEmpty()) | ||
matches = null; | ||
} | ||
|
||
return matches; | ||
} | ||
|
||
} |
Oops, something went wrong.