Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Last active December 7, 2016 09:56
Show Gist options
  • Select an option

  • Save sourceperl/b75a5233d9264c5f4186fbdfa90233b9 to your computer and use it in GitHub Desktop.

Select an option

Save sourceperl/b75a5233d9264c5f4186fbdfa90233b9 to your computer and use it in GitHub Desktop.
Check a thingspeak feed is update
#!/usr/bin/env python3
import http.client
import dateutil.parser
import datetime
import time
import json
ZERO = datetime.timedelta(0)
class UTC(datetime.tzinfo):
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return ZERO
utc = UTC()
while True:
connection = http.client.HTTPSConnection('api.thingspeak.com')
headers = {'Content-type': 'application/json'}
connection.request('GET', '/channels/181289/feeds/last.json', headers=headers)
response = connection.getresponse().read()
d = json.loads(response.decode())
now = datetime.datetime.now(utc)
update = dateutil.parser.parse(d['created_at'])
delta = now - update
print('refresh %.0f s ago' % delta.total_seconds())
time.sleep(30.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment