Last active
June 12, 2017 01:10
-
-
Save simonmichael/90aa4866e255a7f3701ee43d577022f5 to your computer and use it in GitHub Desktop.
hledger-get-crypto-prices json fetcher prototype
This file contains hidden or 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
#!/usr/bin/env stack | |
{- stack runghc --verbosity info | |
--package hledger-lib | |
--package hledger | |
--package here | |
--package aeson | |
--package http-conduit | |
--package conduit-extra | |
--package decimal | |
--package text | |
--package time | |
-} | |
-- Get a few cryptocurrency prices. | |
-- Trying to replicate "curl -s 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LTC&tsyms=USD' | python -m json.tool" :) | |
-- {-# OPTIONS_GHC -Wno-missing-signatures -Wno-name-shadowing #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
import Data.String.Here | |
import Conduit | |
import Control.Monad (mzero) | |
import Data.Aeson -- (Value(..), FromJSON, parseJSON) | |
-- import Data.Aeson (decode) | |
import Data.Conduit.Binary (sinkLbs) | |
import Data.Decimal | |
import Data.Map | |
import Data.Text | |
import Data.Time.Calendar (fromGregorian) | |
import GHC.Generics (Generic) | |
import Hledger | |
import Hledger.Cli | |
import Network.HTTP.Client.Conduit | |
------------------------------------------------------------------------------ | |
cmdmode = hledgerCommandMode | |
[here| get-crypto-prices | |
Fetch today's prices (in USD) for common cryptocurrencies (BTC, ETH, LTC) from | |
cryptocompare.com and print them as P market price directives, to be appended | |
to a journal or prices file. | |
FLAGS | |
|] | |
[] | |
[generalflagsgroup1] | |
[] | |
([], Nothing) -- Just $ argsFlag "[]") | |
------------------------------------------------------------------------------ | |
main :: IO () | |
main = do | |
let url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LTC&tsyms=USD" | |
req <- parseRequest url | |
pricesjson <- withManager $ runResourceT $ withResponse req $ \rsp -> responseBody rsp $$ sinkLbs | |
print (decode pricesjson :: Maybe (Map Text (Map Text Double))) -- Decimal | |
-- d <- getCurrentDay | |
-- instance Generic Decimal | |
-- instance FromJSON Decimal where parseJSON = genericParseJSON defaultOptions | |
-- -- fromJSON = fromJSON . read | |
-- type MarketPrices = [MarketPrice] | |
-- instance FromJSON MarketPrice where | |
-- parseJSON (Object v) = MarketPrice | |
-- <$> pure (fromGregorian 1 1 1) --, -- dummy date to be replaced at runtime | |
-- <*> pure ("XXX") | |
-- <*> pure (Amount{acommodity="YYY", aquantity=1.1, aprice=NoPrice, astyle=amountstyle, amultiplier=False}) | |
-- -- TVShow | |
-- -- <$> v .: "id" | |
-- -- <*> v .: "name" | |
-- -- <*> v .: "overview" | |
-- parseJSON _ = mzero | |
-- instance FromJSON MarketPrice where | |
-- parseJSON (Object v) = TVShow | |
-- <$> v .: "id" | |
-- <*> v .: "name" | |
-- <*> v .: "overview" | |
-- parseJSON _ = mzero | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment