2023-08-10 00:55:07 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module ICAFocus where
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Types
|
|
|
|
import Data.Aeson.KeyMap
|
|
|
|
import Data.Text
|
|
|
|
import Text.Read
|
2023-08-11 18:54:04 +02:00
|
|
|
import Data.Int (Int32)
|
2023-08-10 00:55:07 +02:00
|
|
|
import Data.ByteString.Lazy
|
2023-08-11 18:54:04 +02:00
|
|
|
|
|
|
|
import Misc
|
2023-08-10 00:55:07 +02:00
|
|
|
|
|
|
|
newtype ImageLink = ImageLink { getLink :: String }
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
newtype ImageData = ImageData { getData :: ByteString }
|
2023-08-11 18:54:04 +02:00
|
|
|
|
|
|
|
type ProductID = Int32
|
|
|
|
|
2023-08-10 00:55:07 +02:00
|
|
|
instance Show ImageData where
|
|
|
|
show _ = "ImageData { ... }"
|
|
|
|
|
|
|
|
data ICAFocusItem a = ICAFocusItem
|
|
|
|
{ name :: Text
|
2023-08-11 18:54:04 +02:00
|
|
|
, costPerKg :: Float
|
|
|
|
, costPerUnit :: Float
|
2023-08-10 00:55:07 +02:00
|
|
|
, image :: a
|
|
|
|
}
|
|
|
|
deriving (Show, Functor)
|
|
|
|
|
|
|
|
type ICAFocusData = ICAFocusItem ImageLink
|
|
|
|
type ICAFocusProduct = ICAFocusItem ImageData
|
|
|
|
|
2023-08-11 18:54:04 +02:00
|
|
|
apiEndpoint :: ProductID -> String
|
2023-08-10 01:56:19 +02:00
|
|
|
apiEndpoint i = "https://handlaprivatkund.ica.se/stores/1004247/api/v4/products/bop?retailerProductId=" ++ show i
|
|
|
|
|
2023-08-10 00:55:07 +02:00
|
|
|
instance FromJSON ICAFocusData where
|
|
|
|
parseJSON (Object v) = do
|
2023-08-10 01:56:19 +02:00
|
|
|
productMap <- v .: "entities" .:: "product"
|
2023-08-10 00:55:07 +02:00
|
|
|
product <- case elems productMap of
|
|
|
|
[Object x] -> pure x
|
|
|
|
[] -> fail "No products"
|
|
|
|
_ -> fail "Too many products"
|
|
|
|
|
2023-08-10 01:56:19 +02:00
|
|
|
ICAFocusItem
|
|
|
|
<$> product .: "name"
|
|
|
|
<*> (unpackEither . readEither =<< product .: "price" .:: "current" .:: "amount")
|
2023-08-11 18:54:04 +02:00
|
|
|
<*> (unpackEither . readEither =<< product .: "price" .:: "unit" .:: "current" .:: "amount")
|
|
|
|
<*> (ImageLink <$> product .: "image" .:: "src")
|
2023-08-10 00:55:07 +02:00
|
|
|
|