Last active
August 4, 2021 00:29
-
-
Save a3r0id/5d08b5e0cc9e272791a566194802f5c6 to your computer and use it in GitHub Desktop.
Constantly scrape live price data from Gemeni.com's ticker. Updates to a json file so the data can be easily manipulated.
This file contains 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
from requests_html import HTMLSession | |
from json import dumps, load | |
from time import sleep | |
from datetime import datetime, timezone | |
from traceback import format_exc | |
fileName = 'prices.json' | |
updateIntervals = 60 #SECONDS | |
with open('log.txt', 'a+') as ff: | |
ff.write("Starting @ %s\n" % (str(datetime.now()))) | |
# REQUIRES WEBDRIVER TO BE IN PATH OR CWD | |
session = HTMLSession() | |
while True: | |
# YOU CAN ADD MORE SYMBOLS BELOW AS LONG AS THEY ARE ALSO FEATURED ON GEMINI.COM | |
currencies = [ | |
["Bitcoin", "BTCUSD"], | |
["Ether", "ETHUSD"], | |
["Litecoin", "LTCUSD"], | |
["XRP", "XRPUSD"], | |
["Chainlink", "LINKUSD"], | |
["Bitcoin Cash", "BCHUSD"], | |
["Dogecoin", "DOGEUSD"], | |
["0x", "ZRXUSD"], | |
["PAX Gold", "PAXGUSD"] | |
] | |
t = datetime.now() | |
utc_time = t.replace(tzinfo = timezone.utc) | |
prices = { | |
'meta': { | |
'timeUTC': str(utc_time), | |
'nextUpdateSeconds': float(updateIntervals) | |
}, | |
'prices': {} | |
} | |
r = session.get('https://www.gemini.com/prices/') | |
if r.ok: | |
try: | |
r.html.render() | |
except Exception as f: | |
tb = format_exc() | |
with open('log.txt', 'a') as ff: | |
ff.write("\n" + tb) | |
try: | |
r.close() | |
except: | |
pass | |
continue | |
body = r.html.find('body', first=True) | |
try: | |
r.close() | |
except: | |
pass | |
index = 0 | |
text_lines = body.text.split('\n') | |
for line in text_lines: | |
for type_ in currencies: | |
if type_[0] in line: | |
prices['prices'][type_[0].lower()] = { | |
'price': text_lines[index + 2], | |
'24HourChange': text_lines[index + 3], | |
'percentChange': text_lines[index + 4], | |
'marketCap': text_lines[index + 5] | |
} | |
index += 1 | |
else: | |
prices['error'] = 'HTTP Status: %s' % (r.status_code,) | |
with open(fileName, 'w+') as f: | |
f.write(dumps(prices, indent=4)) | |
sleep(updateIntervals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment