Created
January 12, 2012 14:55
-
-
Save derpston/1600961 to your computer and use it in GitHub Desktop.
SparkFun Free Day / Pachube / Metricfire data scraper
This file contains hidden or 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
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