6 changed files with 103 additions and 6 deletions
@ -0,0 +1,43 @@ |
|||
{-# LANGUAGE OverloadedStrings #-} |
|||
module M3U where |
|||
|
|||
import Types |
|||
import qualified Data.Text as TIO |
|||
import Data.Text.Encoding |
|||
import Data.Maybe |
|||
import Control.Applicative |
|||
import Data.Monoid |
|||
import Data.Attoparsec.Text |
|||
import qualified Data.Attoparsec.Text as AT |
|||
import Utils |
|||
import Playlists |
|||
import Search |
|||
|
|||
m3uHeader = "#EXTM3U" |
|||
trackHeader = "#EXTINF" |
|||
|
|||
makeHeader title = |
|||
mconcat [trackHeader, |
|||
":-1,", |
|||
title] |
|||
|
|||
--toExtinf :: SearchResult -> TIO.Text |
|||
toExtinf track = |
|||
let trackTitle = title track |
|||
in case parseTrack trackTitle of |
|||
Right (artist, title) -> |
|||
makeHeader (artist `mappend` " - " `mappend` title `mappend` "\n") |
|||
Left trackname -> makeHeader $ TIO.pack (trackname `mappend` "\n") |
|||
|
|||
singleTrack track = do |
|||
let extinf = toExtinf track |
|||
trackUrl <- TIO.pack <$> (getUrl $ url track) |
|||
return $ extinf `mappend` trackUrl `mappend` "\n" |
|||
|
|||
genm3u :: [SearchResult] -> IO M3U |
|||
genm3u srs = do |
|||
tracks <- mapM singleTrack srs |
|||
return $ |
|||
M3U $ m3uHeader `mappend` |
|||
"\n" `mappend` |
|||
(mconcat tracks) |
@ -0,0 +1,21 @@ |
|||
module Playlists where |
|||
|
|||
import qualified Data.Text as TIO |
|||
import System.Process (readProcess) |
|||
import Utils |
|||
|
|||
getUrl :: TIO.Text -> IO String |
|||
-- Gets a direct url using youtube-dl |
|||
-- (if it is installed, otherwise we might fallback to some shitty code) |
|||
|
|||
getUrl yourl = readProcess "youtube-dl" |
|||
["-g", |
|||
"-f", |
|||
"bestaudio", |
|||
TIO.unpack yourl, |
|||
"--no-cache-dir"] "" |
|||
downUrl yourl = readProcess "youtube-dl" |
|||
["-f", |
|||
"bestaudio", |
|||
TIO.unpack yourl, |
|||
"--no-cache-dir"] "" |
@ -0,0 +1,22 @@ |
|||
module WebServer where |
|||
|
|||
import Search |
|||
import Network.MPD.Applicative.CurrentPlaylist |
|||
import Types |
|||
import Database |
|||
import Data.List |
|||
import System.Environment (getArgs) |
|||
import qualified Data.Text.IO as TIO (putStrLn, putStr) |
|||
import qualified Control.Monad as M |
|||
import M3U |
|||
|
|||
getPlaylist args = do |
|||
let mode = head args |
|||
let searchTerm = intercalate " " $ tail args |
|||
case mode of |
|||
--"single" -> M.join (addSingle <$> firstResult searchTerm) >> return () |
|||
"all" -> do |
|||
m3ulist <- (M.join (genm3u <$> search searchTerm)) |
|||
let (M3U filecontents) = m3ulist |
|||
return filecontents |
|||
_ -> error "unknown mode" |
Reference in new issue