Last active
March 21, 2019 12:48
-
-
Save sparkslabs/cc6e6820bb540fc8ca5e1a2721cb1316 to your computer and use it in GitHub Desktop.
Watch a specific petition (assumes you have toilet (figlet) installed
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
#!/usr/bin/python3 | |
# | |
# This is a quick and dirty script to watch the petition, and back off when there's failures | |
# It also tries to resync back to checking once every 5 minutes. | |
# It also logs the data | |
# There's some hardcoded stuff. Deal with it. | |
# | |
# Accessing the json feed for this data is likely to be less disruptive. | |
# | |
import os | |
import requests | |
import sys | |
import time | |
def log(data): | |
f = open("/home/michael/petition_data.log","a+") | |
f.write(str(time.time())) | |
f.write("|") | |
f.write(str(time.asctime(time.localtime() ) )) | |
f.write("|") | |
f.write(str(data)) | |
f.write("\n") | |
f.close() | |
def monitor_petition(): | |
result = requests.get("https://petition.parliament.uk/petitions/241584.json") | |
if result.status_code != 200: | |
print("FAILED", time.time(), time.asctime(time.localtime()) ) | |
return False | |
os.system("clear") | |
data = result.json() | |
sigs = str(data["data"]["attributes"]["signature_count"]) | |
log(sigs) | |
print("Petition - https://petition.parliament.uk/petitions/241584") | |
print(data["data"]["attributes"]["action"]) | |
print() | |
print(data["data"]["attributes"]["background"]) | |
print() | |
os.system("toilet -f mono12 " + sigs) | |
t = time.localtime(time.time()) | |
print('As of %02d:%02d %s signatures' % (t.tm_hour, t.tm_min, sigs) ) | |
os.system('espeak "As of %02d:%02d %s signatures"' % (t.tm_hour, t.tm_min, sigs) ) | |
return True | |
os.system("clear") | |
pref_delay = 300 # five minutes | |
sleep_time = pref_delay # Current delay | |
back_off = 1.8 | |
resync = 1.2 | |
while True: | |
result = monitor_petition() | |
if result: | |
if sleep_time > pref_delay: | |
sleep_time = int(sleep_time/resync) | |
print("resync:") | |
if sleep_time < pref_delay: | |
sleep_time = pref_delay | |
else: | |
sleep_time = int(sleep_time * back_off) | |
print("backoff:") | |
print("------------------") | |
os.system("tail -5 /home/michael/petition_data.log") | |
print("------------------") | |
print("sleep until:", sleep_time, time.asctime(time.localtime(time.time()+sleep_time)) ) | |
time.sleep(sleep_time) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment