r/dailyprogrammer • u/jnazario 2 0 • Aug 19 '15
[2015-08-19] Challenge #228 [Intermediate] Use a Web Service to Find Bitcoin Prices
Desciption
Modern web services are the core of the net. One website can leverage 1 or more other sites for rich data and mashups. Some notable examples include the Google maps API which has been layered with crime data, bus schedule apps, and more.
Today's a bit of a departure from the typical challenge, there's no puzzle to solve but there is code to write. For this challenge, you'll be asked to implement a call to a simple RESTful web API for Bitcoin pricing. This API was chosen because it's freely available and doesn't require any signup or an API key. Furthermore, it's a simple GET request to get the data you need. Other APIs work in much the same way but often require API keys for use.
The Bitcoin API we're using is documented here: http://bitcoincharts.com/about/markets-api/ Specifically we're interested in the /v1/trades.csv endpoint.
Your native code API (e.g. the code you write and run locally) should take the following parameters:
The short name of the bitcoin market. Legitimate values are (choose one):
bitfinex bitstamp btce itbit anxhk hitbtc kraken bitkonan bitbay rock cbx cotr vcx
The short name of the currency you wish to see the price for Bitcoin in. Legitimate values are (choose one):
KRW NMC IDR RON ARS AUD BGN BRL BTC CAD CHF CLP CNY CZK DKK EUR GAU GBP HKD HUF ILS INR JPY LTC MXN NOK NZD PEN PLN RUB SAR SEK SGD SLL THB UAH USD XRP ZAR
The API call you make to the bitcoincharts.com site will yield a plain text response of the most recent trades, formatted as CSV with the following fields: UNIX timestamp, price in that currency, and amount of the trade. For example:
1438015468,349.250000000000,0.001356620000Your API should return the current value of Bitcoin according to that exchange in that currency. For example, your API might look like this (in F# notation to show types and args):
val getCurrentBitcoinPrice : exchange:string -> currency:string -> floatWhich basically says take two string args to describe the exchange by name and the currency I want the price in and return the latest price as a floating point value. In the above example my code would return 349.25.
Part of today's challenge is in understanding the API documentation, such as the format of the URL and what endpoint to contact.
Note
Many thanks to /u/adrian17 for finding this API for this challenge - it doesn't require any signup to use.
10
u/hutsboR 3 0 Aug 19 '15 edited Aug 19 '15
Is anyone else getting no data in the response body from the
trades.csvendpoint? It seems like their examples are providing an empty response.Here's what I'm getting:
Nothing when visiting the URL in the browser, either. It's worth noting I AM getting JSON back from the JSON endpoints.
5
u/hutsboR 3 0 Aug 19 '15 edited Aug 19 '15
Elixir: Elixir is surprisingly good at working with HTTP. Here's a clean little functional solution that not only retrieves and parses the data but handles bad responses. (Yes, 302 means that your market or currency is invalid...) Uses the beautiful HTTPoison library.
We can play with it directly in IEx (Elixir's REPL):
9
u/[deleted] Aug 19 '15
...
3
u/binaryblade Aug 19 '15
screw the csv, this is what go was mean for. Hell a bunch of this code is just listing the available market currency combinations.
3
u/minikomi Aug 19 '15 edited Aug 19 '15
Racket (scheme):
Can be used as a script if you save it and chmod +x
edit: added list flags
3
u/Flynn58 Aug 19 '15
Alternatively, simply call their Weighted_Prices json, which allows you to directly pull a price weighted from all the available exchanges.
Python 3
Output:
3
u/Pantstown Aug 19 '15 edited Aug 19 '15
Not really sure if I did this right, so let me know if I totally shit the bed, and I'll try it again.
One thing I'm definitely not sure if it's a node/http/internet thing or if it's just the API being weird, but I was receiving two chunks of data, which was screwing with my output. Since I only needed first chunk (presuming that it holds the most recent trade), I just made an if statement that checks if data has been received.
Also, it seems like the API is failing for every currency but USD. Anyone else experiencing that?
Anyways...Node/Javascript
Output:
99% of calls be like:
3
u/a_Happy_Tiny_Bunny Aug 19 '15 edited Aug 20 '15
Haskell
Most code is error checking, import statements, and the lists of markets and currencies.
The program detects erroneous user input, and also reports when the response has no body.
Sample GHCi (interpreter) session.
I feel like this might have been a good place for me to use a Maybe IO monad transformer, but I've never done so... perhaps I'll come back to it later.
EDIT: Code that uses monad transformers. I learnt a lot. The code is longer, but I think the logic itself (the common functions) are shorter and clearer. I also changed so the behavior: it now repeatedly asks for input until the input given is correct WITHOUT redisplaying the menu, and it used to print "Price: " before "This API is flaky..." when the API call didn't fetch what was expected.
3
u/reyley Aug 19 '15
python 3
It's my first submission and even though it's a small chunk I would like feedback... Thanks!
I'm not sure I got how the mechanic is supposed to work but if you import the code and type: get_exchange_rate(exchange, currency) you will get the exchange rate as float.
3
u/Depth_Magnet Aug 19 '15
I noticed some valid endpoints don't work, so I added 404 handling to my code to deal with that instead of having it throw an error. Most of this code is actually error handling or input validation.
Python2 Code:
2
u/FluffyOgreJet Aug 19 '15
Bash 4
2
u/FIuffyRabbit Aug 19 '15
Go / Golang
I didn't bother checking all of the response codes for what they mean I just know that I got some bad responses. A lot of the verbosity in go stems from error checking.
2
u/NoobOfProgramming Aug 19 '15 edited Aug 19 '15
edit: this is Python 3
This is my first time doing a web thingy. Am i doing it right?
2
u/TeeDawl Aug 19 '15
Could someone help me by describing how this would work under c++? I'm really curious.
Have much thanks.
2
u/stargazer418 Aug 19 '15
Python 2.7
First response, be gentle pls. Most combinations just return 404 errors, not sure if that's on my end or on theirs.
2
u/XDtsFsoVZV Aug 19 '15
Python 3
2
u/codeman869 Aug 19 '15 edited Aug 19 '15
Ruby: Used HTTParty and CSV as well as implemented some standard error handling.
2
u/ullerrm Aug 19 '15
Python 3
2
u/lawonga Aug 23 '15
Golang
2
u/errorseven Aug 23 '15
Autohotkey - It works, can't say I followed instructions exactly as I didn't wrap into into a function, out of time for now.
2
u/rodrigocaldeira Aug 28 '15
LOLCODE
1
u/og_king_jah Aug 19 '15
F#. Uses the FSharp.Data HTTP utilities because the framework API is trash.
1
u/JakDrako Aug 19 '15
VB.Net
1
u/Yopu Aug 19 '15
Kotlin
1
u/curtmack Aug 19 '15
Haskell
Got to learn how to use Network.HTTP, so that's something. I wish there was an easy way to override the "Prelude.read: no parse" error message when you enter something that isn't a valid currency though.
1
u/enterharry Aug 19 '15 edited Aug 19 '15
This API really sucks as other have said. Their documented example doesn't even work.
JavaScript (NodeJS)
This can be made into a node module or a web server. I SHOULD check for valid currencies and exchanges and other error logging but laziness.
1
u/Tarmen Aug 19 '15 edited Aug 19 '15
Nim:
Alternative:
1
u/Fulgere Aug 19 '15
My Java solution drew heavily upon other examples of code, but I still did my first API call (so, whoo!). For the life of me, I could not figure out how to allow the user to input the bitcoin market and currency as arguments.
1
u/VikingofRock Aug 19 '15
Rust
I'd love some feedback on making this more idiomatic Rust, if anyone is interested. I feel like I have the fundamentals of the language down but I don't think I am writing fully idiomatic code yet.
Example outputs:
1
u/wahoyaho Aug 20 '15
C#
1
u/[deleted] Aug 20 '15
Prolog: a PrologScript in SWI 7.
Usage:
1
u/Scroph 0 0 Aug 20 '15
PHP, it seems adequate for this challenge :
1
u/lacraig2 Aug 20 '15
Java one-liner
Giving the user the ability to change the market and currency in 2 lines.
1
u/[deleted] Aug 23 '15 edited Aug 23 '15
Python 3
Getting the data back from the API was the easy part. I wasted a lot of time trying to get the entire process either multithreaded or multiprocessed and wasn't getting any real improvement in performance no matter what I tried.
I'll need to look into that from the ground up with simpler examples, I guess.
Edited because I forgot to format the price. Took some doing. Need to clean up the code when it ain't 4:30 am
1
u/danneu Aug 25 '15 edited Aug 25 '15
Clojure
Parses the body as a stream until newline since the response is so big. Assumes non-200 response is an error.
1
u/skav3n Aug 26 '15
Python 3:
1
u/liaobaishan Aug 28 '15
ruby:
1
u/[deleted] Sep 01 '15 edited Sep 01 '15
I consider myself a beginner-to-possibly-intermediate C# programmer. This code seems to do the trick just fine, except for those cases where 404 is returned.
C# code:
I like to comment everything. If there is a more elegant way to do this, please let me know (coming from C, I can't get over how easy string manipulation is in a modern language). Either way this challenge was a good learning opportunity.
EDIT: I just realized I was supposed to check for only valid currency/exchange strings. That is simple enough, but I got to go to bed now so I'll probably fix it tomorrow
1
u/mandeepy Dec 29 '15
Sorry for the late post. I don't browse this sub, however I'd like to post my solution in case others need help with APIs.
0
u/[deleted] Aug 19 '15
[deleted] — view deleted comment