manufacturing.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python
  2. import oursql
  3. import requests
  4. import urllib2
  5. from xml.dom import minidom
  6. system_name = 'amarr'
  7. #system_name = 'jita'
  8. conn = oursql.connect(db='eve', user='eve', passwd='eve')
  9. rs = requests.session(headers={'User-Agent': 'http://git.raylu.net/eve/'})
  10. def get_systemid(name):
  11. curs = conn.cursor()
  12. try:
  13. #SELECT regionID FROM mapRegions WHERE regionName
  14. curs.execute('''
  15. SELECT solarSystemID
  16. FROM mapSolarSystems
  17. WHERE solarSystemName LIKE ?;
  18. ''', (name,))
  19. return curs.fetchone()[0]
  20. finally:
  21. curs.close()
  22. def iter_blueprints():
  23. curs = conn.cursor()
  24. try:
  25. curs.execute('''
  26. SELECT b.productTypeID, t.typeName FROM invBlueprintTypes as b
  27. INNER JOIN invTypes AS t ON b.productTypeID = t.typeID
  28. ''')
  29. while True:
  30. r = curs.fetchone()
  31. if r is None:
  32. break
  33. yield r
  34. finally:
  35. curs.close()
  36. def get_mats(typeid):
  37. curs = conn.cursor()
  38. try:
  39. # base mats
  40. curs.execute('''
  41. SELECT t.typeID, m.quantity
  42. FROM invTypeMaterials AS m
  43. INNER JOIN invTypes AS t ON m.materialTypeID = t.typeID
  44. WHERE m.typeID = ?;
  45. ''', (typeid,))
  46. mats = dict(curs.fetchall())
  47. # extra mats
  48. curs.execute('''
  49. SELECT blueprintTypeID FROM invBlueprintTypes
  50. WHERE productTypeID = ?
  51. ''', (typeid,))
  52. blueprint_typeid = curs.fetchall()[0][0]
  53. curs.execute('''
  54. SELECT t.typeID, r.quantity
  55. FROM ramTypeRequirements AS r
  56. INNER JOIN invTypes AS t ON r.requiredTypeID = t.typeID
  57. INNER JOIN invGroups AS g ON t.groupID = g.groupID
  58. WHERE r.typeID = ? AND r.activityID = 1 AND g.categoryID != 16;
  59. ''', (blueprint_typeid,))
  60. while True:
  61. r = curs.fetchone()
  62. if r is None:
  63. break
  64. mat_typeid, quantity = r
  65. if mat_typeid not in minerals.keys():
  66. return
  67. try:
  68. mats[mat_typeid] += quantity
  69. except KeyError:
  70. mats[mat_typeid] = quantity
  71. return mats
  72. finally:
  73. curs.close()
  74. def get_prices(typeid, systemid):
  75. # &regionlimit=%d
  76. url = 'http://api.eve-central.com/api/marketstat?typeid=%d&usesystem=%d' % (typeid, systemid)
  77. xml = minidom.parseString(rs.get(url).text)
  78. buy = xml.getElementsByTagName('buy')[0]
  79. buy_max = buy.getElementsByTagName('max')[0]
  80. bid = float(buy_max.childNodes[0].data)
  81. sell = xml.getElementsByTagName('sell')[0]
  82. sell_min = sell.getElementsByTagName('min')[0]
  83. ask = float(sell_min.childNodes[0].data)
  84. return bid, ask
  85. def calc_margin(typeid, systemid):
  86. mats = get_mats(typeid)
  87. if mats is None:
  88. return
  89. cost = 0
  90. for mtypeid, quantity in mats.iteritems():
  91. try:
  92. cost += prices[mtypeid][1] * quantity
  93. except KeyError:
  94. return
  95. if cost == 0:
  96. return
  97. sell_value = get_prices(typeid, systemid)[0]
  98. profit = sell_value - cost
  99. margin = profit / cost
  100. return margin
  101. minerals = {
  102. 34: 'Tritanium',
  103. 35: 'Pyerite',
  104. 36: 'Mexallon',
  105. 40: 'Megacyte',
  106. 37: 'Isogen',
  107. 38: 'Nocxium',
  108. 39: 'Zydrine',
  109. }
  110. prices = {}
  111. systemid = get_systemid(system_name)
  112. for typeid in minerals.iterkeys():
  113. prices[typeid] = get_prices(typeid, systemid)
  114. with open(system_name, 'w') as f:
  115. for typeid, name in iter_blueprints():
  116. try:
  117. margin = calc_margin(typeid, systemid)
  118. except Exception as e:
  119. print repr(e)
  120. else:
  121. if margin is not None:
  122. line = '%0.2f\t%s' % (margin, name)
  123. print line
  124. f.write(line + '\n')