TankorSmash

Steam Purchase Hist.

Jul 25th, 2012
1,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1.  
  2. import BeautifulSoup as bs
  3.  
  4. from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
  5.  
  6.  
  7.  
  8. #buyhist.html is the source code for your page at https://store.steampowered.com/account/
  9.  
  10. #  you could probably grab it with requests, but I wanted to play it safe and quickly
  11.  
  12. f = open('buyhist3.html')
  13.  
  14. data = f.read()
  15.  
  16.  
  17.  
  18. soup = BeautifulSoup(data, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
  19.  
  20.  
  21.  
  22. #find all the sales, there are two lists: the first 6 or so, and the rest:
  23.  
  24. # first, get the big list of sales
  25.  
  26. store_trans = soup.findAll('div', attrs={'class': 'block'})[1]
  27.  
  28. # then, get the visible ones
  29.  
  30. visible_trans = store_trans.findAll('div', attrs={'class': 'transactions'})[0]
  31.  
  32. # then, the hidden ones:
  33.  
  34. hidden_trans = store_trans.findAll('div', attrs={'class': 'hidden_transactions'})[0]
  35.  
  36.  
  37.  
  38. #create a list to catch all the sales
  39.  
  40. sales = []
  41.  
  42.  
  43.  
  44. #loop over all the transactions, looking for each sale object:
  45.  
  46. # first the visible
  47.  
  48. for sale in  visible_trans.findAll('div', attrs={'class': lambda x : x.startswith('transactionRow ')}):
  49.  
  50.  
  51.  
  52.     sales.append(sale)
  53.  
  54. # then the hidden
  55.  
  56. for sale in  hidden_trans.findAll('div', attrs={'class': lambda x : x.startswith('transactionRow ')}):
  57.  
  58.     sales.append(sale)
  59.  
  60.  
  61.  
  62. #now we should have a total number of sales:
  63.  
  64. print 'total number of sales found:', len(sales)
  65.  
  66.  
  67.  
  68. #lets just find the total number we were looking for in the html, under transaction_footer_element
  69.  
  70. total_sales_tag = soup.findAll('div', attrs={'class': 'transaction_footer_element'})[0]
  71.  
  72. # then take the last string in the group as our total:
  73.  
  74. total_sales = total_sales_tag.text.split()[-1]
  75.  
  76. print 'compared to our expected total:', total_sales
  77.  
  78.  
  79.  
  80. #Now we parse the sale entries.
  81.  
  82. allowed_fields = []
  83.  
  84. for field in ['Date', 'Price', 'Items']:
  85.  
  86.     allowed_fields.append('transactionRow'+field)
  87.  
  88.  
  89.  
  90. #new way with saving vars to dicts
  91.  
  92. sales_list = []
  93.  
  94. for sale in sales:
  95.  
  96.     #print 'Date\t\tPrice\t\tTitle'
  97.  
  98.     sale_dict = {}
  99.  
  100.     for tag in sale:    
  101.  
  102.         try:
  103.  
  104.  
  105.  
  106.             if tag['class'] in allowed_fields:
  107.  
  108.                 if tag.findChildren('div', attrs={'class' : "transactionRowTitle"}):
  109.  
  110.                     #sale_dict['title'] = tag.findChildren('div', attrs={'class' : "transactionRowTitle"})[0].renderContents()  
  111.  
  112.                     title = '\t-->' + tag.findChildren('div', attrs={'class' : "transactionRowTitle"})[0].renderContents()  
  113.  
  114.                     sale_dict['title'] = '\n\t -->'.join(title.split(',')) + '\n'
  115.  
  116.                 elif tag['class'] == 'transactionRowDate':
  117.  
  118.                     sale_dict['date'] = tag.renderContents()
  119.  
  120.                 elif tag['class'] == 'transactionRowPrice':
  121.  
  122.                     sale_dict['price'] = tag.renderContents()
  123.  
  124.                 else:
  125.  
  126.                     print 'bah'                                  
  127.  
  128.         except TypeError as e:
  129.  
  130.             pass
  131.  
  132.     sales_list.append(sale_dict)
  133.  
  134.  
  135.  
  136. print 'success!'
  137.  
  138.  
  139.  
  140. #printing nicely
  141.  
  142. maxT, maxD, maxP = 0, 0, 0
  143.  
  144. for sale in sales_list:
  145.  
  146.     maxT = max(maxT, len(sale['title']))
  147.  
  148.     maxD=max(maxD, len(sale['date']))
  149.  
  150.     maxP = max(maxP, len(sale['price']))
  151.  
  152.  
  153.  
  154. for sale in sales_list:
  155.  
  156.     #pad the columns out. In this version it's not really needed anymore, as I
  157.  
  158.     # now split the columns up a bit.
  159.  
  160.     print "Date: %-*s\t Price: %*s\n %s" % (maxD, sale['date'],
  161.  
  162.                                                 maxP, sale['price'],
  163.  
  164.                                                sale['title'])
  165.  
  166.    
  167.  
  168. costs = []
  169.  
  170. count = 0
  171.  
  172. for sale in sales_list:
  173.  
  174.     try:
  175.  
  176.        costs.append(float(sale['price'].strip('$')))
  177.  
  178.     except ValueError:
  179.  
  180.         count += 1
  181.  
  182. print '\nTotal costs from your pocket:', sum(costs), 'with probably', count, 'free games'
Advertisement
Add Comment
Please, Sign In to add comment