Last active
August 29, 2015 13:57
-
-
Save huslage/9742307 to your computer and use it in GitHub Desktop.
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
#!env python | |
import json | |
import requests | |
from sseclient import SSEClient | |
def toStars(rating): | |
b = "" | |
for i in range(0,rating): | |
b += "*" | |
return b | |
accessToken = '<TOKEN>' | |
sparkID = '<ID>' | |
msApiKey = '<KEY>' | |
msBaseUrl = "http://magicseaweed.com/api/" + msApiKey + "/forecast/?spot_id="; | |
msParams = { "units": "us", "fields": "timestamp,swell.*,wind.speed,wind.unit,wind.compassDirection,solidRating" } | |
posturl = 'https://api.spark.io/v1/devices/'+sparkID+'/surf' | |
postheaders = {'Content-Type': 'application/x-www-form-urlencoded'} | |
postparams = {} | |
postparams['access_token'] = accessToken | |
spots = { | |
"2456": "Playa Guiones", | |
"362": "Higgins Beach", | |
"852": "Old Orchard Beach", | |
"401": "Topsail Island", | |
"650": "Carolina Beach", | |
} | |
h = {"Authorization" : "Bearer " + accessToken} | |
r = SSEClient('https://api.spark.io/v1/events/refresh', headers=h) | |
for l in r: | |
try: | |
j = json.loads(l.data) | |
spotID = j['data'] | |
msUrl = msBaseUrl + spotID | |
cb = requests.get(msUrl, params=msParams) | |
ms = cb.json() | |
spotName = spots[spotID] | |
today = ms[4] | |
tomorrow = ms[12] | |
todayrating = toStars(today["solidRating"]) | |
tomorrowrating = toStars(tomorrow["solidRating"]) | |
todaystr = "! {}-{}ft {} {}{} {}".format(today["swell"]["minBreakingHeight"],today["swell"]["maxBreakingHeight"],todayrating,today["wind"]["speed"],today["wind"]["unit"],today["wind"]["compassDirection"]) | |
tomorrowstr = "! {}-{}ft {} {}{} {}".format(tomorrow["swell"]["minBreakingHeight"],tomorrow["swell"]["maxBreakingHeight"],tomorrowrating,tomorrow["wind"]["speed"],tomorrow["wind"]["unit"],tomorrow["wind"]["compassDirection"]) | |
updatestr = "{}/{}/{}".format(spotName,todaystr,tomorrowstr) | |
postparams['args'] = updatestr | |
print postparams | |
ps = requests.post(posturl,data=postparams) | |
print ps.url, ps.text | |
except ValueError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment