-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoodPls.hs
80 lines (59 loc) · 2.65 KB
/
FoodPls.hs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{-# LANGUAGE DeriveGeneric #-}
import Data.Aeson
import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Lazy.Char8 hiding (concat, head, putStr,
unlines)
import GHC.Generics
import Network.HTTP
import Prelude hiding (putStr)
import Rainbow
import Test.QuickCheck
lunchIds = [
("Linsen", "b672efaf-032a-4bb8-d2a5-08d558129279"),
("Kårrestaurangen", "21f31565-5c2b-4b47-d2a1-08d558129279"),
("Express", "3d519481-1667-4cad-d2a3-08d558129279"),
("Hyllan", "a7f0f75b-c1cb-4fc3-d2a6-08d558129279"),
("S.M.A.K", "3ac68e11-bcee-425e-d2a8-08d558129279"),
("L's kitchen", "c74da2cf-aa1a-4d3a-9ba6-08d5569587a1"),
-- ("L's Resto", "c6742862-3cc5-47b1-d2a4-08d558129279"), -- Output gets quite long with these
-- ("L's Express", "871c63d7-4ddb-46b8-d2a0-08d558129279"),
("Kokboken", "c74da2cf-aa1a-4d3a-9ba6-08d5569587a1")] :: [(String, String)]
getLunchUrl :: String -> String
getLunchUrl i = "http://carbonateapiprod.azurewebsites.net/api/v1/mealprovidingunits/" ++ i ++ "/dishoccurrences"
getLunch :: String -> IO String
getLunch url = do
rsp <- Network.HTTP.simpleHTTP (getRequest url)
getResponseBody rsp
getLunchFromId :: String -> String -> IO Lunch
getLunchFromId n i = do
body <- getLunch (getLunchUrl i)
return (n, decode (pack body) :: Maybe [Recipe])
lunches = do
ls <- sequence [getLunchFromId n i | (n, i) <- lunchIds]
printer <- byteStringMakerFromEnvironment
mapM_ BS.putStr . chunksToByteStrings printer $ printLunches ls
printLunches :: [Lunch] -> [Chunk String]
printLunches [] = []
printLunches (l:ls) = printLunch l ++ printLunches ls
printLunch :: ([Char], Maybe [Recipe]) -> [Chunk [Char]]
printLunch (name, Just recepies) = concat ([underline (bold (chunk (name ++ "\n")) & fore blue)]:[printRecipe r | r <- recepies]) ++ [chunk "\n"]
printLunch (name, Nothing) = [underline (bold (chunk (name ++ "\n\n")) & fore blue)]
printRecipe :: Recipe -> [Chunk String]
printRecipe rc = [bold (chunk (dishTypeName (dishType rc) ++ ": ")), chunk (printDisplayName (head (displayNames rc)))]
printDisplayName :: DisplayName -> String
printDisplayName d = (dishDisplayName d ++ "\n")
type Lunch = (String, Maybe [Recipe])
data DisplayName = DisplayName {
dishDisplayName :: String
} deriving (Show, Generic)
instance FromJSON DisplayName
data DishType = DishType {
dishTypeName :: String
} deriving (Show, Generic)
instance FromJSON DishType
data Recipe = Recipe {
dishType :: DishType,
displayNames :: [DisplayName]
} deriving (Generic, Show)
instance FromJSON Recipe
main = lunches