trading.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. from xml.etree import ElementTree
  3. import oursql
  4. import requests
  5. conn = oursql.connect(db='eve', user='eve', passwd='eve')
  6. rs = requests.session()
  7. def get_systemid(name):
  8. with conn as c:
  9. c.execute('SELECT solarSystemID FROM mapSolarSystems WHERE solarSystemName LIKE ?',
  10. (name,))
  11. return c.fetchone()[0]
  12. def iter_items():
  13. with conn as c:
  14. # metaGroupID's:
  15. # NULL or 1: Tech I
  16. # 2: Tech II
  17. # 3: storyline
  18. # 4: faction
  19. # 5: officer
  20. # 6: deadspace
  21. # 14: Tech III
  22. c.execute('''
  23. SELECT invTypes.typeID, typeName FROM invTypes
  24. LEFT JOIN invMetaTypes ON invMetaTypes.typeID = invTypes.typeID
  25. WHERE marketGroupID IS NOT NULL AND metaGroupID != 5
  26. ''')
  27. while True:
  28. r = c.fetchone()
  29. if r is None:
  30. break
  31. yield r
  32. def get_prices(typeid, systemid):
  33. url = 'http://api.eve-central.com/api/marketstat?typeid=%d&usesystem=%d' % (typeid, systemid)
  34. response = rs.get(url, stream=True)
  35. xml = ElementTree.fromstring(response.text)
  36. parent = xml.find('marketstat').find('type')
  37. buy = parent.findall('buy')[0].find('max')
  38. bid = float(buy.text)
  39. sell = parent.find('sell').find('min')
  40. ask = float(sell.text)
  41. volume = parent.find('all').find('volume')
  42. vol = int(volume.text)
  43. return bid, ask, vol
  44. def main():
  45. for system in ['amarr', 'rens', 'dodixie', 'hek']:
  46. systemid = get_systemid(system)
  47. with open(system, 'w') as f:
  48. for typeid, name in iter_items():
  49. bid, ask, vol = get_prices(typeid, systemid)
  50. score = (ask - bid) * vol
  51. f.write('%f\t%f\t%f\t%d\t%s\n' % (score, ask, bid, vol, name))
  52. if __name__ == '__main__':
  53. main()