Skip to content

Instantly share code, notes, and snippets.

@lonyelon
Created September 2, 2020 09:06
Show Gist options
  • Save lonyelon/875e9f89749b3575fd39504cad6696f9 to your computer and use it in GitHub Desktop.
Save lonyelon/875e9f89749b3575fd39504cad6696f9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import json
import urllib.request
import tweepy, time
from credentials import *
LIST_PATH = "/tmp/cryptoprices"
API_KEYS_PATH = "credentials"
# Optional for the Twitter API
apiPublic = ""
apiPrivate = ""
apiToken = ""
apiToken2 = ""
currencyList = []
# Shows the help menu
def showHelp():
print("Usage: " + sys.argv[0] + " [get,show,tweet,savedata]")
print()
exit()
# Gets the price of a coin with respect to another (DEFAULT: EUR)
def getPrice(coin, inc = "EUR"):
val = 1
for a in currencyList:
if a[0] == inc:
val = a[1]
break
for a in currencyList:
if a[0] == coin:
return a[1]/val
# Gets the [truncated] price of a coin with respect to another (DEFAULT: EUR)
def getPriceNice(coin, inc = "EUR"):
return round(getPrice(coin, inc), 2);
# Reads the coin list stored in the system
def getList():
listFile = open(LIST_PATH, "r")
for a in listFile.readlines():
b = a.split("\t")
currencyList.append([b[0], float(b[1])])
listFile.close()
# Main
if __name__ == "__main__":
# Check arguments
if len(sys.argv) != 2:
showHelp()
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
showHelp()
# Get the list from the bitpay api and change values to EUR, then save it
if sys.argv[1] == "get":
# Get the json from bitpay
url = "https://bitpay.com/api/rates"
jsonURL = urllib.request.urlopen(url)
jsonObject=json.load(jsonURL)
# Create the list
for a in jsonObject:
currencyList.append([a["code"], a["rate"]])
# Change values to euros
eur = getPrice("EUR")
for a in currencyList:
a[1] = eur/a[1]
# Save data
file = open(LIST_PATH, "w")
for a in currencyList:
file.write(a[0] + "\t" + str(a[1]) + "\n")
file.close()
# Show the interesting currencies in terminal
elif sys.argv[1] == "show":
getList()
# Show results
print();
print("Crypto prices right now: ")
print("\033[33mBTC price: " + str(getPrice("BTC")) + "€")
print("\033[34mETH price: " + str(getPrice("ETH")) + "€")
print("\033[0mBTC/ETH price: " + str(getPrice("BTC")/getPrice("ETH")) + "ETH")
print()
# Tweet the results (only works when twitter api is configured)
elif sys.argv[1] == "tweet":
getList()
auth = tweepy.OAuthHandler(apiPublic, apiPrivate)
auth.set_access_token(apiToken, apiToken2)
api = tweepy.API(auth)
msg = "Current coin prices:\n"
msg += "BTC: " + str(getPriceNice("BTC")) + "€ | " + str(getPriceNice("BTC", "ETH")) + "ETH\n"
msg += "ETH: " + str(round(getPrice("ETH"), 2)) + "€"
api.update_status(msg)
# Error
else:
showHelp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment