Skip to content

Instantly share code, notes, and snippets.

@jhodges10
Created May 25, 2018 16:23
Show Gist options
  • Save jhodges10/afc71833ae889b75d19bcc5d686e0d45 to your computer and use it in GitHub Desktop.
Save jhodges10/afc71833ae889b75d19bcc5d686e0d45 to your computer and use it in GitHub Desktop.
Particle Photon Dash Treasury Python
import json
import requests
import os
from datetime import datetime
# Set this to be the change delta you want to trigger your notifications
delta_setting = int(os.getenv('DELTA_SETTING'))
def get_mn_count():
mn_url = "https://stats.masternode.me/network-report/latest/json"
try:
response = requests.request("GET", mn_url)
if response.status_code is not 200:
mn_count = 4700
else:
network_stats = json.loads(response.text)['formatted']
mn_count = str(network_stats['f_mn_count']).replace(',', '')
except:
mn_count = 4700
return mn_count
def poll_dash_central():
proposal_hash = os.getenv('PROPOSAL_HASH')
url = "https://www.dashcentral.org/api/v1/proposal?hash={}".format(proposal_hash)
data = requests.get(url)
proposal_data = data.json()['proposal']
min_quorum = int(get_mn_count())/10
proposal_data['current_ratio'] = round((((proposal_data['yes']-proposal_data['no'])/min_quorum)*10), 2)
return proposal_data
def check_file(name):
if os.path.exists('cache/{}.json'.format(name)):
return True
else:
return False
def write_json(data, name):
if not os.path.exists('./cache'):
os.makedirs('./cache')
with open("cache/{}.json".format(name), 'w') as json_stuff:
json.dump(data, json_stuff)
return True
def read_json(filename):
# Made this file path HARD CODED from dash_ninja.py
try:
with open("cache/{}.json".format(filename), 'r') as json_stuff:
data_dict = json.loads(json_stuff)
except TypeError as e:
try:
with open("cache/{}.json".format(filename), 'r') as json_stuff:
json_stuff = json_stuff.read()
data_dict = json.loads(json_stuff)
except Exception as e:
print(e)
data_dict = {}
return data_dict
def particle_ping(msg_type="yes"):
if msg_type == "yes":
url = "https://api.particle.io/v1/devices/YOURDEVICEIDHERE/voteHandler"
payload = "access_token=YOURTOKENHERE&args=yesVote"
headers = {
'Content-Type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print("Succesfully pinged the Particle Photon")
else:
url = "https://api.particle.io/v1/devices/YOURDEVICDEIDHERE/voteHandler"
payload = "access_token=YOURTOKENHERE&args=noVote"
headers = {
'Content-Type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print("Succesfully pinged the Particle Photon")
def check_for_updates():
new_data = poll_dash_central()
# Check to make sure that we have cached data before continuing
if check_file('dc_data'):
old_data = read_json("dc_data")
else:
write_json(new_data, "dc_data")
old_data = read_json("dc_data")
# Calculate the difference between our old data and our new data
change_delta = (int(new_data['remaining_yes_votes_until_funding']) -
(int(old_data['remaining_yes_votes_until_funding'])))
yes_delta = 0
no_delta = 0
comment_delta = 0
try:
yes_delta = int(new_data['yes']) - int(old_data['yes'])
no_delta = int(new_data['no']) - int(old_data['no'])
comment_delta = int(new_data['comment_amount']) - int(old_data['comment_amount'])
except Exception as e:
print(e)
deltas = {"yes_delta": yes_delta,
"no_delta": no_delta,
"comment_delta": comment_delta
}
new_data['deltas'] = deltas
if yes_delta > delta_setting:
# Send "yesVote" to Particle
particle_ping(msg_type="yes")
elif no_delta > delta_setting:
# Send "noVote" to Particle
particle_ping(msg_type="no")
else:
pass
if yes_delta > delta_setting or no_delta > delta_setting:
write_json(new_data, "dc_data") # Update our persistent storage
print("Ran once, fired off update. Delta Y:{} N:{}".format(yes_delta, no_delta))
else:
print("Ran once. Waiting until next run to notify. Delta too small (Y:{} N:{})".format(yes_delta, no_delta))
if __name__ == "__main__":
check_for_updates()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment