From 8d5235ab6d1335a8c89b022633d02adfa018ba7a Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sun, 5 Jul 2015 22:56:28 -0400 Subject: [PATCH] a bunch of shit to make it suck less --- Database.hs | 24 ++++++++++-------------- Search.hs | 6 +++++- Utils.hs | 13 +++++++++---- youtube-mpd.cabal | 4 +++- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Database.hs b/Database.hs index bfe1065..478e6aa 100644 --- a/Database.hs +++ b/Database.hs @@ -34,21 +34,19 @@ toValue = C.unsafeCoerce fromId :: MP.Id -> Int fromId = C.unsafeCoerce -changeTag :: MP.Metadata -> TIO.Text -> Maybe MP.Song -> IO (MP.Response [B.ByteString]) -changeTag _ _ Nothing = error "empty playlist" +--changeTag :: MP.Metadata -> TIO.Text -> Either MP.Id -> IO (MP.Response [B.ByteString]) -changeTag tag tagval' (Just song) = do +changeTag tag tagval' (Right sid) = do let tagval = encodeUtf8 tagval' - case (MP.sgId song) of - Nothing -> error "tried to modify a non-existent track" - (Just sid) -> addTagId sid tag (BC.unpack tagval) + in addTagId sid tag (BC.unpack tagval) +changeTag _ _ (Left _) = error "error changing tag" changeArtist = changeTag MP.Artist changeTitle = changeTag MP.Title -changeBoth track (artist, title) = - changeArtist artist track >> - changeTitle title track +changeBoth trackid (artist, title) = + changeArtist artist trackid >> + changeTitle title trackid addTagId :: MP.Id -> MP.Metadata -> String -> IO (Response [B.ByteString]) addTagId sid tag tagVal = MP.withMPD $ @@ -66,9 +64,7 @@ addSingle track = do let trackUrl = url track let trackDesc = title track fullUrl <- readProcess "youtube-dl" ["-g", "-f", "bestaudio", (TIO.unpack trackUrl)] "" - MP.withMPD $ MP.add $ toPath fullUrl - (Right pl) <- (MP.withMPD $ MP.playlistInfo Nothing) - let lastTrack = listToMaybe $ reverse pl - either (const $ changeTitle trackDesc lastTrack) - (changeBoth lastTrack) + newId <- MP.withMPD $ MP.addId (toPath fullUrl) Nothing + either (const $ changeTitle trackDesc newId) + (changeBoth newId) (parseTrack trackDesc) diff --git a/Search.hs b/Search.hs index 194b040..73712a2 100644 --- a/Search.hs +++ b/Search.hs @@ -17,6 +17,8 @@ import Control.Exception import Network.HTTP.Base (urlEncode, urlDecode) import Types +maxResults = 10 + makeURL :: T.Text -> T.Text makeURL vid = "https://youtube.com/watch?v=" `T.append` vid @@ -39,7 +41,9 @@ baseURI = "https://www.googleapis.com/youtube/v3/" searchRequest :: String -> String -> String searchRequest keyword accessTok = baseURI ++ - "search?part=snippet&q=" ++ + "search?maxResults=" ++ + (show maxResults) ++ + "&part=snippet&q=" ++ keyword ++ "&type=video&access_token=" ++ accessTok diff --git a/Utils.hs b/Utils.hs index b1880c0..13fcb47 100644 --- a/Utils.hs +++ b/Utils.hs @@ -6,15 +6,20 @@ import qualified Data.Text as TIO import Data.Text.Encoding import Data.Maybe import Control.Applicative +import Control.Monad import Data.Attoparsec.Text +import qualified Data.Attoparsec.Text as AT + +repeated xs = TIO.concat <$> (many1 $ string xs) + +isSep = choice [string "by", + repeated "-", + repeated "|"] -isSep = string "by" <|> - string " - " <|> - string "- " isTrack = do artist <- manyTill anyChar isSep title <- many1 anyChar - return (TIO.pack artist, TIO.pack title) + return (TIO.strip $ TIO.pack artist, TIO.strip $ TIO.pack title) parseTrack = parseOnly isTrack diff --git a/youtube-mpd.cabal b/youtube-mpd.cabal index 14bc163..5f0c92a 100644 --- a/youtube-mpd.cabal +++ b/youtube-mpd.cabal @@ -7,5 +7,7 @@ Synopsis: play youtube music easier Build-Type: Simple Executable youtube-mpd - Build-Depends: base, handa-gdata, process, directory, http-conduit, bytestring, aeson, containers, text + Build-Depends: base, HTTP, handa-gdata, process, directory, http-conduit, bytestring, aeson, containers, text, libmpd, MissingH, mtl, attoparsec Main-Is: Main.hs + Extensions: OverloadedStrings + GHC-Options: -fwarn-incomplete-patterns -Werror