manufacturing.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. # metaGroupID's:
  26. # NULL or 1: Tech I
  27. # 2: Tech II
  28. # 14: Tech III
  29. # filtering for these excludes storyline (3), faction (4), officer (5), and deadspace (6)
  30. curs.execute('''
  31. SELECT b.productTypeID, t.typeName FROM invBlueprintTypes as b
  32. INNER JOIN invTypes AS t ON b.productTypeID = t.typeID
  33. LEFT JOIN invMetaTypes AS m ON m.typeID = b.productTypeID
  34. WHERE m.metaGroupID IS NULL or m.metaGroupID in (1,2,14)
  35. ''')
  36. while True:
  37. r = curs.fetchone()
  38. if r is None:
  39. break
  40. yield r
  41. finally:
  42. curs.close()
  43. def get_mats(typeid):
  44. curs = conn.cursor()
  45. try:
  46. # base mats
  47. curs.execute('''
  48. SELECT t.typeID, m.quantity
  49. FROM invTypeMaterials AS m
  50. INNER JOIN invTypes AS t ON m.materialTypeID = t.typeID
  51. WHERE m.typeID = ?;
  52. ''', (typeid,))
  53. mats = dict(curs.fetchall())
  54. # extra mats
  55. curs.execute('''
  56. SELECT blueprintTypeID FROM invBlueprintTypes
  57. WHERE productTypeID = ?
  58. ''', (typeid,))
  59. blueprint_typeid = curs.fetchall()[0][0]
  60. curs.execute('''
  61. SELECT t.typeID, r.quantity
  62. FROM ramTypeRequirements AS r
  63. INNER JOIN invTypes AS t ON r.requiredTypeID = t.typeID
  64. INNER JOIN invGroups AS g ON t.groupID = g.groupID
  65. WHERE r.typeID = ? AND r.activityID = 1 AND g.categoryID != 16;
  66. ''', (blueprint_typeid,))
  67. while True:
  68. r = curs.fetchone()
  69. if r is None:
  70. break
  71. mat_typeid, quantity = r
  72. try:
  73. mats[mat_typeid] += quantity
  74. except KeyError:
  75. mats[mat_typeid] = quantity
  76. return mats
  77. finally:
  78. curs.close()
  79. systemid = get_systemid(system_name)
  80. prices = {}
  81. def get_prices(typeid):
  82. global prices
  83. if typeid in prices:
  84. return prices[typeid]
  85. # &regionlimit=%d
  86. url = 'http://api.eve-central.com/api/marketstat?typeid=%d&usesystem=%d' % (typeid, systemid)
  87. xml = minidom.parseString(rs.get(url).text)
  88. buy = xml.getElementsByTagName('buy')[0]
  89. buy_max = buy.getElementsByTagName('max')[0]
  90. bid = float(buy_max.childNodes[0].data)
  91. sell = xml.getElementsByTagName('sell')[0]
  92. sell_min = sell.getElementsByTagName('min')[0]
  93. ask = float(sell_min.childNodes[0].data)
  94. prices[typeid] = (bid, ask)
  95. return bid, ask
  96. def calc_margin(typeid):
  97. mats = get_mats(typeid)
  98. if mats is None:
  99. return
  100. cost = 0
  101. for mtypeid, quantity in mats.iteritems():
  102. cost += get_prices(mtypeid)[1] * quantity
  103. if cost == 0:
  104. return
  105. sell_value = get_prices(typeid)[0]
  106. profit = sell_value - cost
  107. margin = profit / cost
  108. return margin
  109. with open(system_name, 'w') as f:
  110. for typeid, name in iter_blueprints():
  111. try:
  112. margin = calc_margin(typeid)
  113. except xml.parsers.expat.ExpatError:
  114. pass
  115. else:
  116. if margin is not None:
  117. line = '%0.2f\t%s' % (margin, name)
  118. print line
  119. f.write(line + '\n')