Skip to content

Instantly share code, notes, and snippets.

@gmr
Created August 30, 2016 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmr/71102828b20d89780389ce5c8fe8e23f to your computer and use it in GitHub Desktop.
Save gmr/71102828b20d89780389ce5c8fe8e23f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import datetime
import decimal
from os import path
import pickle
import re
import requests
import time
from email import utils
class UTC(datetime.tzinfo):
"""UTC"""
def utcoffset(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return datetime.timedelta(0)
PATTERN = re.compile(r'activos\.\.\.\s+(?P<percent>\d{1,3}\.\d+)')
last_value = 3
last_modified = utils.parsedate_to_datetime('Wed, 24 Aug 2016 19:35:53 GMT')
if path.exists('amomentincrime.dat'):
last_modified, last_value = pickle.load(open('amomentincrime.dat', 'rb'))
while True:
response = requests.get('http://amomentincrime.com')
matches = PATTERN.search(response.text)
value = decimal.Decimal(matches.group(1))
if value != last_value:
modified = utils.parsedate_to_datetime(response.headers['Last-Modified'])
delta = modified - last_modified
with open('amomentincrime.log', 'a') as handle:
handle.write('{}% to {}% in {} seconds\n'.format(last_value, value, delta.seconds))
print('Now at {}% after {} seconds at {}%'.format(value, delta.seconds, last_value))
last_modified = modified
last_value = value
with open('amomentincrime.dat', 'wb') as handle:
pickle.dump((last_modified, last_value), handle)
else:
last = datetime.datetime.now(UTC()) - last_modified
print('Last change was {} seconds ago'.format(last.seconds))
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment