Created
June 18, 2016 03:07
-
-
Save howdydoody123/b2df1da622fe8823857cc0aaf456cd99 to your computer and use it in GitHub Desktop.
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
''' | |
A small module for getting the prices of bitcoin and litecoin from https://preev.com | |
Works for both Python 2 and 3. Easily importable and requires only a simple call. | |
''' | |
try: | |
from urllib import urlopen | |
except ImportError: | |
# py 3 | |
from urllib.request import urlopen | |
import json | |
def _get_avg(data, coin): | |
prices = [] | |
for entry in data[coin]['usd']: | |
prices.append(float(data[coin]['usd'][entry]['last'])) | |
return sum(prices) / len(prices) | |
def get_data(url): | |
resp = urlopen(url) | |
content = resp.read() | |
try: | |
return json.loads(content) | |
except TypeError: | |
return json.loads(content.decode('utf-8')) | |
def ltc(): | |
url = 'http://preev.com/pulse/units:ltc+usd/sources:btce' | |
data = get_data(url) | |
return round(_get_avg(data, 'ltc'), 2) | |
def btc(): | |
url = 'http://preev.com/pulse/units:btc+usd/sources:bitfinex+bitstamp+btce' | |
data = get_data(url) | |
return round(_get_avg(data, 'btc'), 2) | |
if __name__ == '__main__': | |
print(ltc()) | |
print(btc()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment