Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Created January 18, 2021 21:53
Show Gist options
  • Save evgenii-malov/ec7a36426720aba81de67b40c69a7e8e to your computer and use it in GitHub Desktop.
Save evgenii-malov/ec7a36426720aba81de67b40c69a7e8e to your computer and use it in GitHub Desktop.
Bitcoin price monitor in haskell
-- see video https://youtu.be/M4_OW2_zXQo
import Network.HTTP.Conduit
import Text.JSON.JPath
import Data.ByteString.Lazy.Char8 (unpack)
import Control.Concurrent
import Control.Exception
import Control.Applicative
import Control.Monad.Loops
u = "https://api.bittrex.com/api/v1.1/public/getticker?market=USD-BTC"
parsed d = head $ (jPath "result/Ask" d) <|> ["parse_price_error"]
act = try $ (simpleHttp u) >>= (\d -> threadDelay 1000000 >> return (parsed $ unpack d)) :: IO (Either HttpException [Char])
pe pp (Right p) = if pp==p then return p else print p >> return p
pe pp (Left e) = print er >> threadDelay 1000000 >> return er where er = show e
main = iterateUntilM (\_->False) (\pp -> act >>= (pe pp)) "start"
@evgenii-malov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment