summaryrefslogtreecommitdiffstats
path: root/manufacturing.py
blob: 894e5805a8efd43b3eb193135251a5295a1e8842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python

import oursql
import requests
import urllib2
from xml.dom import minidom

system_name = 'amarr'
#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:
		curs.execute('''
			SELECT b.productTypeID, t.typeName FROM invBlueprintTypes as b
			INNER JOIN invTypes AS t ON b.productTypeID = t.typeID
		''')
		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
			if mat_typeid not in minerals.keys():
				return
			try:
				mats[mat_typeid] += quantity
			except KeyError:
				mats[mat_typeid] = quantity
		return mats
	finally:
		curs.close()

def get_prices(typeid, systemid):
	# &regionlimit=%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)

	return bid, ask

def calc_margin(typeid, systemid):
	mats = get_mats(typeid)
	if mats is None:
		return
	cost = 0
	for mtypeid, quantity in mats.iteritems():
		try:
			cost += prices[mtypeid][1] * quantity
		except KeyError:
			return
	if cost == 0:
		return
	sell_value = get_prices(typeid, systemid)[0]
	profit = sell_value - cost
	margin = profit / cost
	return margin

minerals = {
		34: 'Tritanium',
		35: 'Pyerite',
		36: 'Mexallon',
		40: 'Megacyte',
		37: 'Isogen',
		38: 'Nocxium',
		39: 'Zydrine',
	}
prices = {}

systemid = get_systemid(system_name)
for typeid in minerals.iterkeys():
	prices[typeid] = get_prices(typeid, systemid)
with open(system_name, 'w') as f:
	for typeid, name in iter_blueprints():
		try:
			margin = calc_margin(typeid, systemid)
		except Exception as e:
			print repr(e)
		else:
			if margin is not None:
				line = '%0.2f\t%s' % (margin, name)
				print line
				f.write(line + '\n')