jacob614

tptapi stock fetching script

Sep 23rd, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import urllib
  2. import urllib2
  3. from cookielib import CookieJar
  4. import re
  5.  
  6. #
  7. # stock script
  8. #
  9.  
  10. cookies = {}
  11. def login(username, password):
  12.     cj = CookieJar()
  13.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  14.     opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1')]
  15.     urllib2.install_opener(opener)
  16.  
  17.     urllib2.urlopen(urllib2.Request(
  18.             "http://tptapi.com/login_proc.php?login",
  19.             urllib.urlencode({"Username":username, "Password":password})))
  20.  
  21.     for i in list(cj):
  22.         cookies[i.name] = i.value
  23.  
  24. def getPage(url):
  25.     return urllib2.urlopen(urllib2.Request(url, urllib.urlencode(cookies))).read()
  26.  
  27. results = {}
  28. def getStockInfo(page):
  29.     stocks = []
  30.     for i in page.splitlines():
  31.         stocks.extend(re.findall("<td width='\d+%'>([^<>]*)</td>", i))
  32.  
  33.     for i in range(0, len(stocks), 4):
  34.         results[stocks[i]] = {'name':stocks[i], 'value':stocks[i+1], 'shares':stocks[i+2], 'ceo':stocks[i+3]}
  35.  
  36. def printStockInfo(text, user, server, channel):
  37.     output = ""
  38.     for i in results:
  39.         output = output + i + " - $" + results[i]["value"] + "  08"
  40.         #print i + " - $" + results[i]["value"] + " " + results[i]["shares"] + " shares. CEO: " + results[i]["ceo"]
  41.     send(["msg " + channel + " " + output, server, channel])
  42.  
  43. def stockInfo(text, user, server, channel):
  44.     getStockInfo(getPage("http://tptapi.com/getstock.php"))
  45.     if text[1] in results:
  46.         send(["msg " + channel + " " + results[text[1]]["value"], server, channel])
  47.  
  48.  
  49. #
  50. #insert random hexchat related stuff here
  51. #somewhere in main:
  52. #
  53.     if command == "!!login":
  54.         login("jacob2", "<insert password here>") #cookies are saved to a pref file in my script
  55.     elif command == "!!stock":
  56.         stockInfo(text, user, server, channel)   #get info on just one stock
  57.     elif command == "!!print":
  58.         printStockInfo(text, user, server, channel) #print all info. doesn't get the info first though ...
Advertisement
Add Comment
Please, Sign In to add comment