blob: cf557a07fe1400e8eea006373d735e9a020f3a35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
{-# LANGUAGE OverloadedStrings #-}
module Main
( main
) where
import HTTP
import System.Environment (getArgs, getProgName)
import Data.List (intercalate)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Text.Encoding (encodeUtf8)
import Data.Aeson (eitherDecodeStrict)
import Model.URL
import Model.Json.Search
import Model.Perfume
import PerfumeParser (parsePerfumes)
main :: IO ()
main = do
args <- getArgs
case args of
[] -> do
progName <- T.pack <$> getProgName
T.putStrLn (T.concat ["Usage: ", progName, " material"])
materials -> do
eitherPages <- fmap sequence <$> sequence . map getHtmlPerfumes . map T.pack $ materials
case eitherPages of
Left error ->
T.putStrLn error
Right perfumesSequences ->
let commonPerfumes = getCommonPerfumes perfumesSequences
outputName = (intercalate "-" materials) ++ ".csv"
in T.writeFile outputName (csvPerfumes commonPerfumes)
getHtmlPerfumes :: Text -> IO (Either Text [Perfume])
getHtmlPerfumes material = do
eitherPage <- getPage (getIdURL material)
case eitherPage of
Left error ->
return . Left $ error
Right page ->
case eitherDecodeStrict (encodeUtf8 page) :: Either String [Search] of
Left error ->
return . Left . T.pack $ error
Right searches ->
case getMaterialIdentifier searches of
Nothing ->
return . Left $ "No material identifier found"
Just identifier -> do
fmap parsePerfumes <$> getPage (getMaterialURL identifier)
|