Created
January 28, 2014 20:44
-
-
Save gabegaster/8675985 to your computer and use it in GitHub Desktop.
scrape twine data and push to tempodb
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
import requests | |
import json | |
import datetime | |
import time | |
import pytz | |
import tempodb | |
# Modify these with your credentials found at: http://tempo-db.com/manage/ | |
API_KEY = 'intentionally left blank' | |
API_SECRET = 'fffffffffffffffffffff' | |
SERIES_KEY = 'the_kitchen' | |
twine_id = "00000----------" # twine id | |
URL = "https://twine.cc/%s/rt?cached=1" % twine_id | |
# see: http://community.supermechanical.com/index.php?p=/discussion/7/api/p1 | |
# cookie copied from result of : | |
# wget -o log.txt --quiet -O temp.txt --keep-session-cookies --save-cookies cookies.txt --no-check-certificate --post-data="email=REPLACEWITHYOUREMAIL&password=REPLACEWITHYOURPASS" | |
# make your user_id a cookie | |
COOKIE = {'user_id':"FFFFFFFF==|000123012010|000000000000000"} | |
tz = pytz.timezone("America/Chicago") | |
client = tempodb.Client(API_KEY, API_SECRET) | |
def log(temp): | |
dp = tempodb.DataPoint(datetime.datetime.now(tz), temp) | |
print dp | |
line = ",".join(map(str,(datetime.datetime.now(tz),temp)))+"\n" | |
print line | |
client.write_key(SERIES_KEY, [dp]) | |
with open("temp.log","a") as f: | |
f.write(line) | |
def request(): | |
r = requests.get(URL,cookies=COOKIE) | |
data = json.loads(r.content) | |
age = data['age'] | |
temp= data['values'][1][1]/100 | |
return age, temp | |
def main(): | |
old_age = 10**10 | |
old_temp = None | |
tries = 0 | |
while 1: | |
try: | |
new_age, new_temp = request() | |
print "ages: %s, tries:%s"%(new_age,tries) | |
if new_age < old_age or new_temp != old_temp: | |
log(new_temp) | |
old_temp = new_temp | |
tries = 0 | |
else: | |
tries += 1 | |
old_age = new_age | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
pass | |
time.sleep(60) | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment