Advertisement
Guest User

updater.py

a guest
Jun 17th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import json
  2. import urllib.request
  3. import sys
  4. from bs4 import BeautifulSoup
  5.  
  6. username = 'BUXFER USERNAME'
  7. password = 'BUXFER PASSWORD'
  8.  
  9. def checkError(response):
  10.     result = json.loads(response)
  11.     response = result['response']
  12.     if response['status'] != "OK":
  13.         print("An error occured: " + response['status'].replace('ERROR: ', ''))
  14.         sys.exit(1)
  15.     return response
  16.  
  17. # BUXFER ACCOUNT POLLING #
  18. base = "https://www.buxfer.com/api";
  19. url  = base + "/login?userid=" + username + "&password=" + password;
  20.  
  21. # Confirm API request and obtain token
  22. req = urllib.request.Request(url=url)
  23. response = checkError(urllib.request.urlopen(req).read().decode('utf-8'))
  24. token = response['token']
  25.  
  26. # Obtain accounts
  27. url  = base + "/accounts?token=" + token;
  28. req = urllib.request.Request(url=url)
  29. response = checkError(urllib.request.urlopen(req).read().decode('utf-8'))
  30.  
  31. # PCPARTPICKER TOTAL POLLING #
  32. url = 'PCPARTPICKER BUILD URL'
  33. req = urllib.request.Request(url=url)
  34. req_html = urllib.request.urlopen(req).read().decode('utf-8')
  35. table = [[cell.text for cell in row("td")] for row in BeautifulSoup(req_html, "html.parser")("tr")]
  36. money = 0
  37. for entry in table:
  38.     if len(entry) > 2:
  39.         if entry[0] == "Base Total:":
  40.             money = float(entry[1][1:])
  41.  
  42. # Parse balance from first account and write to a text file
  43. balance = response['accounts'][0]['balance']
  44. f = open(r'PATH_TO_@RESOURCES', 'w')
  45. f.write('[Variables]\n\nBalance='+str(balance)+'\nGoal='+str(money))
  46. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement