playlists-0.5.1: Library and executable for working with playlist files.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Playlist

Synopsis

Playlist Types

data Track Source #

A single music file or streaming URL.

Constructors

Track 

Fields

Instances

Instances details
Eq Track Source # 
Instance details

Defined in Text.Playlist.Types

Methods

(==) :: Track -> Track -> Bool #

(/=) :: Track -> Track -> Bool #

Show Track Source # 
Instance details

Defined in Text.Playlist.Types

Methods

showsPrec :: Int -> Track -> ShowS #

show :: Track -> String #

showList :: [Track] -> ShowS #

type Playlist = [Track] Source #

A list of Tracks.

Playlist Formats

data Format Source #

Playlist formats.

Instances

Instances details
Eq Format Source # 
Instance details

Defined in Text.Playlist.Types

Methods

(==) :: Format -> Format -> Bool #

(/=) :: Format -> Format -> Bool #

Read Format Source # 
Instance details

Defined in Text.Playlist.Types

Show Format Source # 
Instance details

Defined in Text.Playlist.Types

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"

resolve Source #

Arguments

:: forall m. Monad m 
=> Playlist

A Playlist that may contain references to other playlists.

-> (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 Playlist. (All tracks should be files and not links to other playlists.)

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:

  1. 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.
  2. 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.