Skip to content

Instantly share code, notes, and snippets.

@Aareon
Last active January 4, 2016 18:31
Show Gist options
  • Save Aareon/0acc43a14d6034161852 to your computer and use it in GitHub Desktop.
Save Aareon/0acc43a14d6034161852 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
#Requires /util/parse.py & /util/__init__.py
"""
"worth <optional: amount> <coin> <coin target>";
Ex: ".worth 5 doge ltc"
Replies to user with value of an amount of one coin for another. Cryptonator API.
"""
import urllib2, json, re
from util import output
from parse import parse_json
from util.hook import *
json_data = 'data.json'
parse_json(json_data)
data = parse_json.data
@hook(cmds=['worth'])
def coin_pair_get(code, input):
output.info("Getting coin info...")
coin = []
try:
route_site = 'https://'+data["coin_ex"]+'/api/full/'
coin_pair = input.group(2) #everything after the trigger
if len(coin_pair.split(' ')) == 2:
coin.append(coin_pair.split(' ')[0]), coin.append(coin_pair.split(' ')[1])
elif ' ' in coin_pair:
coin.append(coin_pair.split(' ')[1]), coin.append(coin_pair.split(' ')[2])
else:
code.reply("You formatted your request improperly. Try !worth 1 ldoge ltc")
response = urllib2.urlopen(route_site+coin[0]+'-'+coin[1])
page = response.read()
page_contents = json.loads(page)
page_error = page_contents["error"]
if "Pair not found" not in page_error:
page_price = float(page_contents["ticker"]["price"])
try:
price_market = page_contents["ticker"]["markets"][0]["market"]
price_market_price = page_contents["ticker"]["markets"][0]["price"]
flag = 1
except IndexError:
flag = 0
if len(coin_pair.split(' ')) == 2:
coin_check = 1
else:
coin_check = float(coin_pair.split(' ')[0])
print(coin_check)
coin_worth = float(page_price*coin_check)
print(str(coin_check)+"*"+str(page_price)+"="+str(coin_worth))
if flag == 1:
if len(coin_pair.split(' ')) == 2:
code.reply('{blue}'+str(coin_check)+' {black}'+coin[0].upper()+' is worth {blue}%.8f'%coin_worth+' {black}'+str(coin[1]).upper()+' ({blue}%.8f'%page_price+'{black}) on '+price_market)
else:
code.reply('{blue}'+str(coin_check)+' {black}'+coin[0].upper()+' is worth {blue}%.8f'%coin_worth+' {black}'+str(coin[1]).upper()+' ({blue}%.8f'%page_price+'{black}) on '+price_market)
else:
code.reply('{blue}'+str(coin_check)+' {black}'+coin[0].upper()+' is worth {blue}%.8f'%coin_worth+' {black}'+str(coin[1]).upper()+' ({blue}%.8f'%page_price+'{black}) according to Cryptonator')
else:
code.reply(page_error)
return()
except urllib2.HTTPError:
code.reply("Error 404: Not found")
except IndexError:
pass
@Aareon
Copy link
Author

Aareon commented Jan 3, 2016

Requires /util/parse.py

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