| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #!/usr/bin/env python
- import oursql
- import requests
- import urllib2
- from xml.dom import minidom
- import xml.parsers.expat
- system_name = 'jita'
- conn = oursql.connect(db='eve', user='eve', passwd='eve')
- rs = requests.session(headers={'User-Agent': 'http://git.raylu.net/eve/'})
- def get_systemid(name):
- curs = conn.cursor()
- try:
- #SELECT regionID FROM mapRegions WHERE regionName
- curs.execute('''
- SELECT solarSystemID
- FROM mapSolarSystems
- WHERE solarSystemName LIKE ?;
- ''', (name,))
- return curs.fetchone()[0]
- finally:
- curs.close()
- def iter_blueprints():
- curs = conn.cursor()
- try:
- # metaGroupID's:
- # NULL or 1: Tech I
- # 2: Tech II
- # 14: Tech III
- # filtering for these excludes storyline (3), faction (4), officer (5), and deadspace (6)
- curs.execute('''
- SELECT b.productTypeID, t.typeName FROM invBlueprintTypes as b
- INNER JOIN invTypes AS t ON b.productTypeID = t.typeID
- LEFT JOIN invMetaTypes AS m ON m.typeID = b.productTypeID
- WHERE m.metaGroupID IS NULL or m.metaGroupID in (1,2,14)
- ''')
- while True:
- r = curs.fetchone()
- if r is None:
- break
- yield r
- finally:
- curs.close()
- def get_mats(typeid):
- curs = conn.cursor()
- try:
- # base mats
- curs.execute('''
- SELECT t.typeID, m.quantity
- FROM invTypeMaterials AS m
- INNER JOIN invTypes AS t ON m.materialTypeID = t.typeID
- WHERE m.typeID = ?;
- ''', (typeid,))
- mats = dict(curs.fetchall())
- # extra mats
- curs.execute('''
- SELECT blueprintTypeID FROM invBlueprintTypes
- WHERE productTypeID = ?
- ''', (typeid,))
- blueprint_typeid = curs.fetchall()[0][0]
- curs.execute('''
- SELECT t.typeID, r.quantity
- FROM ramTypeRequirements AS r
- INNER JOIN invTypes AS t ON r.requiredTypeID = t.typeID
- INNER JOIN invGroups AS g ON t.groupID = g.groupID
- WHERE r.typeID = ? AND r.activityID = 1 AND g.categoryID != 16;
- ''', (blueprint_typeid,))
- while True:
- r = curs.fetchone()
- if r is None:
- break
- mat_typeid, quantity = r
- try:
- mats[mat_typeid] += quantity
- except KeyError:
- mats[mat_typeid] = quantity
- return mats
- finally:
- curs.close()
- systemid = get_systemid(system_name)
- prices = {}
- def get_prices(typeid):
- global prices
- if typeid in prices:
- return prices[typeid]
- # ®ionlimit=%d
- url = 'http://api.eve-central.com/api/marketstat?typeid=%d&usesystem=%d' % (typeid, systemid)
- xml = minidom.parseString(rs.get(url).text)
- buy = xml.getElementsByTagName('buy')[0]
- buy_max = buy.getElementsByTagName('max')[0]
- bid = float(buy_max.childNodes[0].data)
- sell = xml.getElementsByTagName('sell')[0]
- sell_min = sell.getElementsByTagName('min')[0]
- ask = float(sell_min.childNodes[0].data)
- prices[typeid] = (bid, ask)
- return bid, ask
- def calc_margin(typeid):
- mats = get_mats(typeid)
- if mats is None:
- return
- cost = 0
- for mtypeid, quantity in mats.iteritems():
- cost += get_prices(mtypeid)[1] * quantity
- if cost == 0:
- return
- sell_value = get_prices(typeid)[0]
- profit = sell_value - cost
- margin = profit / cost
- return margin
- with open(system_name, 'w') as f:
- for typeid, name in iter_blueprints():
- try:
- margin = calc_margin(typeid)
- except xml.parsers.expat.ExpatError:
- pass
- else:
- if margin is not None:
- line = '%0.2f\t%s' % (margin, name)
- print line
- f.write(line + '\n')
|