basic code
This commit is contained in:
parent
f7c577fa9f
commit
3661cf237d
11 changed files with 300 additions and 0 deletions
61
app/Main.hs
Normal file
61
app/Main.hs
Normal file
|
@ -0,0 +1,61 @@
|
|||
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 qualified Data.ByteString.Char8 as B8
|
||||
import GHC.IO.Encoding (setLocaleEncoding)
|
||||
import Lib
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
setLocaleEncoding utf8
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
filename :: FilePath
|
||||
filename= "wnpClient.txt"
|
||||
|
||||
|
||||
coverName :: FilePath
|
||||
coverName="cover.jpg"
|
Loading…
Add table
Add a link
Reference in a new issue