change filelocation, edit readme

This commit is contained in:
qvalentin 2021-06-27 11:36:46 +02:00
parent 63a1d64293
commit 74fa5cb880
3 changed files with 62 additions and 44 deletions

View File

@ -5,4 +5,15 @@ Alternative client for the [WebNowPlaying-BrowserExtension](https://github.com/t
Requires [websocketd](https://github.com/joewalnes/websocketd). Requires [websocketd](https://github.com/joewalnes/websocketd).
Run it with:
`websocketd --port=8974 wnpClient` `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`.

View File

@ -1,19 +1,22 @@
module Main where module Main where
import Control.Monad
import Control.Exception import Control.Exception
import System.IO import Control.Monad
import System.Directory
import Network.HTTP.Client
import Network.HTTP.Simple
import Network.URI
import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Char8 as B8
import GHC.IO.Encoding (setLocaleEncoding) import GHC.IO.Encoding (setLocaleEncoding)
import Lib 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 :: IO ()
main = do main = do
setLocaleEncoding utf8 setLocaleEncoding utf8
createDirectoryIfMissing False =<< workingDir
join $ (writeFile <$> fileName) <*> pure ""
forever $ do forever $ do
line <- getLine line <- getLine
dispatch line dispatch line
@ -25,37 +28,40 @@ dispatch ('A':'L':'B':'U':'M':':':album) = writeToFile album 2
dispatch ('C' : 'O' : 'V' : 'E' : 'R' : ':' : cover) = handleCover cover dispatch ('C' : 'O' : 'V' : 'E' : 'R' : ':' : cover) = handleCover cover
dispatch _ = return () dispatch _ = return ()
writeToFile :: String -> Int -> IO () writeToFile :: String -> Int -> IO ()
writeToFile text position = do writeToFile text position = do
contents <- lines <$> readFile filename contents <- lines <$> (readFile =<< fileName)
let newContent = unlines $ take position contents ++ [text] ++ drop (position + 1) contents let newContent = unlines $ take position contents ++ [text] ++ drop (position + 1) contents
bracketOnError (openTempFile "." "temp") bracketOnError
(openTempFile "." "temp")
( \(tempName, tempHandle) -> do ( \(tempName, tempHandle) -> do
hClose tempHandle hClose tempHandle
removeFile tempName) removeFile tempName
)
( \(tempName, tempHandle) -> do ( \(tempName, tempHandle) -> do
hPutStr tempHandle newContent hPutStr tempHandle newContent
hClose tempHandle hClose tempHandle
renameFile tempName filename) renameFile tempName =<< fileName
)
handleCover :: String -> IO () handleCover :: String -> IO ()
handleCover url = do handleCover url = do
contents <- lines <$> readFile filename contents <- lines <$> (readFile =<< fileName)
when ((contents !! 3 ) /= url && url /= "") $ do if length contents > 3 && (contents !! 3) /= url && url /= ""
writeToFile (url) 3 then do
writeToFile url 3
request <- parseRequest url request <- parseRequest url
let fileName = Prelude.last . pathSegments . getUri $ request let coverFileName = Prelude.last . pathSegments . getUri $ request
resp <- httpBS request join $ B8.writeFile <$> coverName <*> (getResponseBody <$> httpBS request)
B8.writeFile coverName $ getResponseBody resp else writeToFile url 3
-- B8.writeFile coverName $ getResponseBody resp
filename :: FilePath fileName :: IO FilePath
filename= "wnpClient.txt" fileName = (</> "wnpClient.txt") <$> workingDir
coverName :: IO FilePath
coverName = (</> "cover.jpg") <$> workingDir
coverName :: FilePath workingDir :: IO FilePath
coverName="cover.jpg" workingDir = (</> ".wnpClient") <$> getHomeDirectory

View File

@ -51,6 +51,7 @@ executable wnpClient
, network-uri , network-uri
, http-conduit , http-conduit
, bytestring , bytestring
, filepath
default-language: Haskell2010 default-language: Haskell2010
test-suite wnpClient-test test-suite wnpClient-test