Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Track = Track {
- trackURL :: Text
- trackTitle :: Maybe Text
- trackDuration :: Maybe Float
- type Playlist = [Track]
- data Format
- parsePlaylist :: Format -> ByteString -> Either String Playlist
- parserForFormat :: Format -> Parser Playlist
- generatePlaylist :: Format -> Playlist -> ByteString
- fileNameToFormat :: FilePath -> Maybe Format
- appendExtension :: Format -> FilePath -> FilePath
- resolve :: forall m. Monad m => Playlist -> (Text -> m Playlist) -> m Playlist
Playlist Types
A single music file or streaming URL.
Track | |
|
Playlist Formats
Parsing and Generating
parsePlaylist :: Format -> ByteString -> Either String Playlist Source #
Parse a playlist from a ByteString
. Parsing may fail in which
case an error message is returned in Left
.
content <- BS.getContents case parsePlaylist M3U content of Left err -> fail $ "failed to parse playlist: " ++ err Right x -> return x
parserForFormat :: Format -> Parser Playlist Source #
Return the appropriate attoparsec parser for the given playlist format.
generatePlaylist :: Format -> Playlist -> ByteString Source #
Generate a lazy ByteString
containing playlist data from the
given playlist and in the given format.
BL.putStr $ generatePlaylist M3U somePlaylist
Utility Functions
fileNameToFormat :: FilePath -> Maybe Format Source #
Try to figure out a file's format from it's file extension.
>>>
fileNameToFormat "foo.m3u"
Just M3U
>>>
fileNameToFormat "foo.M3U"
Just M3U
>>>
fileNameToFormat "foo.txt"
Nothing
appendExtension :: Format -> FilePath -> FilePath Source #
Given a file name that does not have a file extension, return a file name with the appropriate extension included based on the given format.
>>>
appendExtension M3U "foo"
"foo.m3u"
:: forall m. Monad m | |
=> Playlist | A |
-> (Text -> m Playlist) | Downloading function. This function should take a URL and return a parsed playlist. It's expected that the URL points to another playlist that needs to be parsed and possibly resolved. |
-> m Playlist | A fully resolved |
If the given Playlist
contains tracks that reference remote
playlists, this function will recursively download and process
these playlists. Returns a flattened playlist that should not
contain any references to other playlists.
You supply the downloading function as the second argument. Use whichever HTTP library that makes you happy.
There are two error conditions that are ignored by this function:
- The nesting of playlists exceeds a (hard-coded) limit. In this case no playlists beyond the limit are processed. Open a pull request if you'd like to have a resolveN function that allows you to specific the depth limit or one that returns an error.
- A downloaded playlist contains a syntax error. In this case the playlist is consider to have no tracks and is ignored. Open a pull request if you want a version of this function that returns some sort of an error instead of ignoring bad playlists.