Last active
November 8, 2016 17:05
-
-
Save benjaminsavoy/f7c7aa2ce8d53f5422be6031c603f8b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveGeneric, TypeSynonymInstances, FlexibleInstances, OverloadedStrings #-} | |
import Data.Aeson | |
import Data.DateTime | |
import Data.Text | |
import Control.Monad | |
import qualified Data.ByteString.Lazy as B | |
import Network.HTTP.Conduit (simpleHttp) | |
import GHC.Generics | |
class Queryable t where | |
uri :: String | |
data Query t = | |
Query { pageCount :: Int | |
, totalCount :: Int | |
, items :: [t] | |
} deriving (Show, Generic) | |
data Region = | |
Region { region_id :: Int | |
, region_name :: String | |
, region_href :: String | |
} deriving (Show, Generic, Queryable) | |
instance Queryable Region where | |
uri = "regions" | |
data Order = | |
Order { order_buy :: Bool | |
, order_issued :: DateTime | |
, order_price :: Int | |
, order_volume :: Int | |
, order_duration :: Int | |
, order_id :: Int | |
, order_minVolume :: Int | |
, order_range :: String | |
, order_stationID :: Int | |
, order_type :: Int | |
} deriving (Show, Generic, Queryable) | |
instance Queryable Order where | |
uri = "market/10000002/orders/all" | |
type RegionQuery = Query Region | |
type OrderQuery = Query Order | |
instance FromJSON OrderQuery | |
instance FromJSON RegionQuery | |
instance FromJSON Region where | |
parseJSON (Object v) = | |
Region <$> v .: "id" | |
<*> v .: "name" | |
<*> v .: "href" | |
instance FromJSON Order where | |
parseJSON (Object v) = | |
Order <$> v .: "buy" | |
<*> v .: "issued" | |
<*> v .: "price" | |
<*> v .: "volume" | |
<*> v .: "duration" | |
<*> v .: "id" | |
<*> v .: "minVolume" | |
<*> v .: "range" | |
<*> v .: "stationID" | |
<*> v .: "type" | |
regionURL :: String | |
regionURL = "https://crest-tq.eveonline.com/regions/" | |
getRegion :: IO B.ByteString | |
getRegion = simpleHttp regionURL | |
ordersURL :: String | |
ordersURL = "https://crest-tq.eveonline.com/market/10000002/orders/all/" | |
getOrders :: IO B.ByteString | |
getOrders = simpleHttp ordersURL | |
main :: IO () | |
main = do | |
num <- regionID "The Forge" | |
print num | |
regionID :: String -> IO Int | |
regionID regionName = do | |
d <- (eitherDecode <$> getRegion) :: IO (Either String RegionQuery) | |
case d of | |
Left err -> fail err | |
Right ps -> return (region_id (Prelude.head (Prelude.filter (\x -> ((region_name x) == regionName)) (items ps)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment