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)