From 74fa5cb88041198b7763ae72a3a4e26cff3c1de3 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 27 Jun 2021 11:36:46 +0200 Subject: [PATCH] change filelocation, edit readme --- README.md | 13 ++++++- app/Main.hs | 92 ++++++++++++++++++++++++++----------------------- wnpClient.cabal | 1 + 3 files changed, 62 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 37013d0..18c1206 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,15 @@ Alternative client for the [WebNowPlaying-BrowserExtension](https://github.com/t Requires [websocketd](https://github.com/joewalnes/websocketd). -`websocketd --port=8974 wnpClient` \ No newline at end of file +Run it with: +`websocketd --port=8974 wnpClient` + + + +The information is stored in a text file: `~/.wnpClient/wnpClient.txt`. + +From there you can read it with whatever application you want to use. +For example with [Executor](https://extensions.gnome.org/extension/2932/executor/) for the gnome desktop environment. +`head ~/.wnpClient/wnpClient.txt -n 2 | sed ':a;N;$!ba;s/\n/ by /g'` + +The cover of the current song is also downloaded to `~/.wnpClient/cover.jpg`. \ No newline at end of file diff --git a/app/Main.hs b/app/Main.hs index 48a2eea..da1ffc2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,61 +1,67 @@ module Main where -import Control.Monad import Control.Exception -import System.IO -import System.Directory -import Network.HTTP.Client -import Network.HTTP.Simple -import Network.URI +import Control.Monad import qualified Data.ByteString.Char8 as B8 import GHC.IO.Encoding (setLocaleEncoding) import Lib +import Network.HTTP.Client +import Network.HTTP.Simple +import Network.URI +import System.Directory +import System.FilePath +import System.IO main :: IO () main = do - setLocaleEncoding utf8 - forever $ do - line <- getLine - dispatch line + setLocaleEncoding utf8 + createDirectoryIfMissing False =<< workingDir + join $ (writeFile <$> fileName) <*> pure "" + forever $ do + line <- getLine + dispatch line dispatch :: String -> IO () -dispatch ('T':'I':'T':'L':'E':':':title) = writeToFile title 0 -dispatch ('A':'R':'T':'I':'S':'T':':':artist) = writeToFile artist 1 -dispatch ('A':'L':'B':'U':'M':':':album) = writeToFile album 2 -dispatch ('C':'O':'V':'E':'R':':':cover) = handleCover cover -dispatch _ = return () - - - - -writeToFile:: String -> Int -> IO() -writeToFile text position= do - contents <- lines <$> readFile filename - let newContent = unlines $ take position contents ++ [text] ++ drop (position+1) contents - bracketOnError (openTempFile "." "temp") - (\(tempName,tempHandle) -> do - hClose tempHandle - removeFile tempName) - (\(tempName,tempHandle) -> do - hPutStr tempHandle newContent - hClose tempHandle - renameFile tempName filename) +dispatch ('T' : 'I' : 'T' : 'L' : 'E' : ':' : title) = writeToFile title 0 +dispatch ('A' : 'R' : 'T' : 'I' : 'S' : 'T' : ':' : artist) = writeToFile artist 1 +dispatch ('A' : 'L' : 'B' : 'U' : 'M' : ':' : album) = writeToFile album 2 +dispatch ('C' : 'O' : 'V' : 'E' : 'R' : ':' : cover) = handleCover cover +dispatch _ = return () +writeToFile :: String -> Int -> IO () +writeToFile text position = do + contents <- lines <$> (readFile =<< fileName) + let newContent = unlines $ take position contents ++ [text] ++ drop (position + 1) contents + bracketOnError + (openTempFile "." "temp") + ( \(tempName, tempHandle) -> do + hClose tempHandle + removeFile tempName + ) + ( \(tempName, tempHandle) -> do + hPutStr tempHandle newContent + hClose tempHandle + renameFile tempName =<< fileName + ) handleCover :: String -> IO () -handleCover url= do - contents <- lines <$> readFile filename - when ((contents !! 3 ) /= url && url /= "") $ do - writeToFile (url) 3 - request <- parseRequest url - let fileName = Prelude.last . pathSegments . getUri $ request - resp <- httpBS request - B8.writeFile coverName $ getResponseBody resp +handleCover url = do + contents <- lines <$> (readFile =<< fileName) + if length contents > 3 && (contents !! 3) /= url && url /= "" + then do + writeToFile url 3 + request <- parseRequest url + let coverFileName = Prelude.last . pathSegments . getUri $ request + join $ B8.writeFile <$> coverName <*> (getResponseBody <$> httpBS request) + else writeToFile url 3 +-- B8.writeFile coverName $ getResponseBody resp -filename :: FilePath -filename= "wnpClient.txt" +fileName :: IO FilePath +fileName = ( "wnpClient.txt") <$> workingDir +coverName :: IO FilePath +coverName = ( "cover.jpg") <$> workingDir -coverName :: FilePath -coverName="cover.jpg" \ No newline at end of file +workingDir :: IO FilePath +workingDir = ( ".wnpClient") <$> getHomeDirectory \ No newline at end of file diff --git a/wnpClient.cabal b/wnpClient.cabal index 2832b78..b84a22b 100644 --- a/wnpClient.cabal +++ b/wnpClient.cabal @@ -51,6 +51,7 @@ executable wnpClient , network-uri , http-conduit , bytestring + , filepath default-language: Haskell2010 test-suite wnpClient-test