manufacturing.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. import oursql
  3. import requests
  4. import urllib2
  5. from xml.dom import minidom
  6. import xml.parsers.expat
  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. try:
  66. mats[mat_typeid] += quantity
  67. except KeyError:
  68. mats[mat_typeid] = quantity
  69. return mats
  70. finally:
  71. curs.close()
  72. systemid = get_systemid(system_name)
  73. prices = {}
  74. def get_prices(typeid):
  75. global prices
  76. if typeid in prices:
  77. return prices[typeid]
  78. # &regionlimit=%d
  79. url = 'http://api.eve-central.com/api/marketstat?typeid=%d&usesystem=%d' % (typeid, systemid)
  80. xml = minidom.parseString(rs.get(url).text)
  81. buy = xml.getElementsByTagName('buy')[0]
  82. buy_max = buy.getElementsByTagName('max')[0]
  83. bid = float(buy_max.childNodes[0].data)
  84. sell = xml.getElementsByTagName('sell')[0]
  85. sell_min = sell.getElementsByTagName('min')[0]
  86. ask = float(sell_min.childNodes[0].data)
  87. prices[typeid] = (bid, ask)
  88. return bid, ask
  89. def calc_margin(typeid):
  90. mats = get_mats(typeid)
  91. if mats is None:
  92. return
  93. cost = 0
  94. for mtypeid, quantity in mats.iteritems():
  95. cost += get_prices(mtypeid)[1] * quantity
  96. if cost == 0:
  97. return
  98. sell_value = get_prices(typeid)[0]
  99. profit = sell_value - cost
  100. margin = profit / cost
  101. return margin
  102. with open(system_name, 'w') as f:
  103. for typeid, name in iter_blueprints():
  104. try:
  105. margin = calc_margin(typeid)
  106. except xml.parsers.expat.ExpatError:
  107. pass
  108. else:
  109. if margin is not None:
  110. line = '%0.2f\t%s' % (margin, name)
  111. print line
  112. f.write(line + '\n')