Skip to content

Instantly share code, notes, and snippets.

@derpston
Created January 12, 2012 14:55
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 derpston/1600961 to your computer and use it in GitHub Desktop.
Save derpston/1600961 to your computer and use it in GitHub Desktop.
SparkFun Free Day / Pachube / Metricfire data scraper
import urllib
import time
import metricfire
from BeautifulSoup import BeautifulSoup
metricfire.init("[API key redacted]")
previous_values = None
while True:
# Fetch page from Pachube.
content = urllib.urlopen("https://pachube.com/feeds/43969").read()
# Find the current winning and losing attempt values.
soup = BeautifulSoup(content)
values = {}
for datastream in soup.findAll("span", attrs = {"class": "datastream-id"}):
key = datastream.getText()
value = int(datastream.findParent().find("span", attrs = {"class": "datastream-value"}).getText())
if key == 'Losing_Attempts':
values['lose'] = value
elif key == 'Winning_Attempts':
values['win'] = value
# Send the lose/win totals off to Metricfire.
metricfire.send("lose", values['lose'])
metricfire.send("win", values['win'])
# Calculate the percentage chance of one entry winning.
if previous_values is not None:
dxwin = values['win'] - previous_values['win']
dxlose = values['lose'] - previous_values['lose']
try:
metricfire.send("winpc", float(dxwin) / dxlose * 100)
except ZeroDivisionError, ex:
pass
previous_values = values
# Be polite to Pachube.
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment