pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by Willow on Tue 31st Dec 22:24
download | new post

  1. #!/usr/bin/python
  2.  
  3. import urllib
  4. import collections
  5. import json
  6.  
  7. TableEntry = collections.namedtuple("TableEntry", "Item_ID Item_Name Maximum_Buy Minimum_Sell Flippable Profit")
  8.  
  9. def getItemData(itemID, flipMargin = 0.85):
  10.     #Accepts an itemID and queries the gw2spidy API
  11.     #Returns a json response as a named tuple
  12.     #http://docs.python.org/2/library/collections.html#collections.namedtuple
  13.     gw2spidy = "http://www.gw2spidy.com/api/v0.9/json/item/"
  14.     f = urllib.urlopen(gw2spidy+str(itemID))
  15.     jsonResponse = f.read()
  16.     f.close()
  17.     response = json.loads(jsonResponse)
  18.     #Extract sell and buy order information from the response + convert to silver from copper.
  19.     minSell = response["result"]["min_sale_unit_price"]/100.0
  20.     maxBuy =  response["result"]["max_offer_unit_price"]/100.0
  21.  
  22.     if (minSell*flipMargin) < maxBuy:
  23.         flip = "No"
  24.     else: flip = "Yes"
  25.  
  26.     profit = (minSell*flipMargin) - maxBuy
  27.    
  28.     itemData = TableEntry((itemID),
  29.                           (response["result"]["name"]),
  30.                           (maxBuy),
  31.                           (minSell),
  32.                           (flip),
  33.                           (profit))
  34.     return itemData
  35.  
  36. def buildTable(itemIDs):
  37.     items = []
  38.     for itemID in itemIDs:
  39.         items.append(getItemData(itemID))
  40.     return items
  41.  
  42. def outputTable(name,table):
  43.  
  44.     width = len(table[0])
  45.     output = ['<table><tr><th colspan=',str(width),'>',name,'</th></tr>\n']
  46.     output.append('<tr>')
  47.     for field in table[0]._fields:
  48.         output.append('<th>' + field.replace('_',' ') + '</th>')
  49.     output.append('</tr>\n')
  50.     for row in table:
  51.         output.append('<tr>')
  52.         for element in row:
  53.             output.append('<td>' + str(element) + '</td>')
  54.         output.append('<tr>\n')
  55.     output.append('</table>\n<br />\n')
  56.     return "".join(output)
  57.  
  58. def outputSection(name,section):
  59.     output = ['<div class="section"><h1 class="sectionHeading">' , name , '</h1>\n']
  60.     for table in section:
  61.         output.append(outputTable(table,section[table]))
  62.     output.append('</div>\n')
  63.     return "".join(output)
  64.  
  65. def outputAll(damnThing):
  66.     f = open('gw2TP.html', 'w')
  67.     f.write('<html>\n<head></head>\n<body>\n')
  68.    
  69.     output = ['<html>\n<head></head>\n<body>\n']
  70.     for section in damnThing:
  71.         output.append(outputSection(section,damnThing[section]))
  72.     f.write("".join(output))       
  73.     f.write("</body>\n</html>")
  74.     f.close()
  75.    
  76.     return "".join(output)
  77.  
  78.  
  79. if __name__ == "__main__":
  80.  
  81.     #Build Common Materials section
  82.     ore     = buildTable( [19697, 19703, 19699, 19698, 19702, 19700, 19701 ] )   
  83.     wood    = buildTable( [19723, 19726, 19727, 19724, 19722, 19725 ] )
  84.     cloth   = buildTable( [19718, 19739, 19741, 19743, 19748, 19745 ] )
  85.     leather = buildTable( [19719, 19728, 19730, 19731, 19729, 19732 ] )
  86.     common = {"Ore": ore, "Wood": wood, "Cloth": cloth, "Leather": leather}
  87.     #Build Fine Materials section
  88.     blood   = buildTable( [24290, 24291, 24292, 24293, 24294, 24295] )
  89.     bone    = buildTable( [24343, 24343, 24344, 24345, 24341, 24358] )
  90.     claw    = buildTable( [24346, 24347, 24348, 24349, 24350, 24351] )
  91.     dust    = buildTable( [24272, 24273, 24274, 24275, 24276, 24277] )
  92.     fang    = buildTable( [24352, 24353, 24354, 24355, 24356, 24357] )
  93.     scale   = buildTable( [24284, 24285, 24286, 24287, 24288, 24289] )
  94.     totem   = buildTable( [24296, 24297, 24298, 24363, 24299, 24300] )
  95.     sac     = buildTable( [24278, 24279, 24280, 24281, 24282, 24283] )
  96.     fine = {"Blood": blood, "Bone": bone, "Claw": claw, "Dust": dust,
  97.             "Fang": fang, "Scale": scale, "Totem": totem, "Venom Sac": sac}
  98.     #Build Rare Materials section
  99.     crystal   = buildTable( [24326, 24327, 24328, 24329, 24330] )
  100.     corrupted = buildTable( [24336, 24337, 24338, 24339, 24340] )
  101.     destroyer = buildTable( [24321, 24322, 24323, 24324, 24325] )
  102.     essence   = buildTable( [24331, 24332, 24333, 24334, 24335] )
  103.     glacial   = buildTable( [24316, 24317, 24318, 24319, 24319] )
  104.     molten    = buildTable( [24311, 24312, 24313, 24314, 24315] )
  105.     onyx      = buildTable( [24306, 24307, 24308, 24309, 24310] )
  106.     charged   = buildTable( [24301, 24302, 24303, 24304, 24305] )
  107.     rare = {"Crystal" : crystal, "Corrupted" : corrupted,
  108.             "Destroyer" : destroyer, "Essence" : essence,
  109.             "Glacial" :glacial, "Molten" : molten,
  110.             "Onyx" : onyx, "Charged" : charged}
  111.    
  112.    
  113.     wholeDamnThing = { "Common Materials" : common,
  114.                        "Fine Materials" : fine,
  115.                        "Rare Materials" : rare}
  116.     outputAll(wholeDamnThing)
  117.     #print outputAll(wholeDamnThing),

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings