Skip to content

Instantly share code, notes, and snippets.

@ping13
Last active December 23, 2015 17:09
Show Gist options
  • Save ping13/6667297 to your computer and use it in GitHub Desktop.
Save ping13/6667297 to your computer and use it in GitHub Desktop.
Simple counter script for http://geobeer.ch
#!/usr/local/bin/python2.7
import gspread
import json
import httplib
import urllib2
import urllib
import re
import os
import datetime
from bs4 import BeautifulSoup
# load config dictionary
config = json.load(open('./config.json'))
# load the old output, so that we can keep track of things
old_output = json.load(open(config['json_output']))
output = old_output
# reset success and timestamp for current output
output['success'] = True
output['timestamp_utc'] = None
# scrape number of attendees from techup.ch
try:
content = urllib2.urlopen(config['techupurl']).read()
soup = BeautifulSoup(content)
attendees_title_dom_els = soup.find_all(attrs={"class":"attendees_title"})
if len(attendees_title_dom_els) > 0:
attendees_title= soup.find_all(attrs={"class":"attendees_title"})[0].contents[0]
pattern = re.compile("^\s*(\d+)\s*people")
output['registrations_techupch'] = int(pattern.findall(attendees_title)[0])
else:
output['registrations_techupch'] = 0
except:
# if there was a failure, start counting the failures
if output['registrations_techupch'] < 0:
output['registrations_techupch'] -= 1
else:
output['registrations_techupch'] = -1
output['success'] = False
# get number of email registrations
try:
# Login with your Google account
gc = gspread.login(config['user'], config['password'])
# Open a worksheet from spreadsheet with one shot
wks = gc.open(config['spreadsheet']).worksheet(config['worksheet'])
# get number of mail registrations (= number of rows minus first 2 lines)
output['registrations_mail'] = len(wks.get_all_values())-2
except:
# if there was a failure, start counting the failures
if output['registrations_mail'] < 0:
output['registrations_mail'] -= 1
else:
output['registrations_mail'] = -1
output['success'] = False
# write results to a json file
output['timestamp_utc'] = str(datetime.datetime.utcnow())
old_output = json.load(open(config['json_output']))
json.dump(output, open(config['json_output'],"w"))
# if I call this via a CGI script, let's make sure that it doesn't
# throw an error
if os.environ.has_key('REQUEST_METHOD'):
print "Content-Type: text/plain"
print
# fn to send notifications with pushover
def send_push_notification(message):
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": config["pushover_token"],
"user": config["pushover_user"],
"message": message,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
# Let's see if we need to send a notification
if not output['success']:
# if there was more than 5 failures, send out a push notification.
if output['registrations_mail'] <= -5 or output['registrations_techupch'] <= -5:
send_push_notification("Whoops, couldn't update registration counter repeatedly: %s" % output)
else:
pass # Cross your fingers that the error was only temporary
else:
if (output['registrations_techupch'] != old_output['registrations_techupch'] or output['registrations_mail'] != old_output['registrations_mail']):
send_push_notification("New registration! %s" % output)
else:
pass # nothing happend, so we don't need to send anything
# finally, print out a JSON object for the website
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment