Skip to content

Instantly share code, notes, and snippets.

@ibaaj
Last active May 4, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibaaj/7519c4fe4caef154cb7ff197964fe6c8 to your computer and use it in GitHub Desktop.
Save ibaaj/7519c4fe4caef154cb7ff197964fe6c8 to your computer and use it in GitHub Desktop.
Ethereum Notification System
import urllib.request
import json
import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
import os
from pushbullet import Pushbullet
# get your api key here: https://www.worldcoinindex.com/apiservice
urlWorldCoinIndexAPI = 'https://www.worldcoinindex.com/apiservice/json?key=XXXXXXXXXXX'
# get your access token on pushbullet.com
pb = Pushbullet("XXXXXXXXXXXXX")
r = urllib.request.urlopen(urlWorldCoinIndexAPI).read()
data = json.loads(r.decode('utf-8'))
eth = 0.0
for market in data['Markets']:
if market['Name'] == 'Ethereum':
eth = float(market['Price_eur'])
t = datetime.datetime.now()
fmt = '%Y-%m-%d %H:%M:%S'
tstring = t.strftime(fmt)
currentHour = int(t.strftime('%H'))
if os.path.exists('data.csv'):
with open('data.csv', 'a+') as f:
writer = csv.writer(f)
writer.writerow([tstring, eth])
f.close()
else:
with open('data.csv', 'w+') as f:
writer = csv.writer(f)
writer.writerow(['date','value'])
writer.writerow([tstring, eth])
f.close()
valueAtMidnight = 0.0
if os.path.exists('day-start.txt'):
with open('day-start.txt', 'r+') as f:
valueAtMidnight = float(f.read())
if currentHour == 0:
valueAtMidnight = eth
f.seek(0)
text = '%.4f' % valueAtMidnight
f.write(text)
f.close()
else:
with open('day-start.txt', 'w+') as f:
text = '%.4f' % eth
f.write(text)
f.close()
valueAtMidnight = eth
variation = round( (1 - valueAtMidnight/eth)*100, 4)
if abs(variation) >= 4:
# avoid being flooded
if os.path.exists('notif-date.txt'):
with open('notif-date.txt', 'r+') as f:
lastdate = datetime.datetime.strptime(f.read().split('\n')[0], fmt)
diff = (lastdate - t).days
if diff != -1:
sign = '+' if variation > 0 else ''
text = '%.4f' % variation
pb.push_note('Alert ETH : Variation de ' + sign +''+ text +'%', "")
f.seek(0)
f.write(tstring)
f.close()
else:
with open('notif-date.txt', 'w+') as f:
f.write(tstring)
f.close()
sign = '+' if variation > 0 else ''
text = '%.4f' % variation
pb.push_note('Alert ETH : Variation de ' + sign +''+ text +'%', "")
if currentHour == 18:
datestrings = []
ethvalues = []
with open('./data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
cdate = datetime.datetime.strptime(row['date'], fmt)
diff = (cdate - t).days
if diff == -1:
datestrings.append(row['date'])
ethvalues.append(float(row['value']))
dates = [dateutil.parser.parse(s) for s in datestrings]
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
ax.set_xticks(dates)
xfmt = md.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates,ethvalues, 'o-')
plt.savefig('graph.png')
with open("graph.png", "rb") as pic:
file_data = pb.upload_file(pic, "ETH Daily Graph", file_type="image/png")
pb.push_file(**file_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment