manufacturing.py 2.9 KB

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