forked from madcowfred/evething
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory_updater.py
executable file
·91 lines (75 loc) · 2.43 KB
/
history_updater.py
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
#!/usr/local/bin/python
import os
import psycopg2
import time
import urllib2
import xml.etree.ElementTree as ET
from decimal import *
# Aurgh
from django.core.management import setup_environ
from evething import settings
setup_environ(settings)
from django.db import connection
from thing.models import *
HISTORY_URL = 'http://goonmetrics.com/api/price_history/?region_id=10000002&type_id=%s'
def main():
cursor = connection.cursor()
# Get a list of all item_ids
item_ids = []
cursor.execute("""
SELECT item_id
FROM thing_marketorder
UNION
SELECT bp.item_id
FROM thing_blueprint bp, thing_blueprintinstance bpi
WHERE bp.id = bpi.blueprint_id
UNION
SELECT item_id
FROM thing_blueprintcomponent
WHERE blueprint_id IN (
SELECT blueprint_id
FROM thing_blueprintinstance
)
""")
for row in cursor:
item_ids.append(row[0])
item_ids = list(item_ids)
item_ids.sort()
# Collect data
for i in range(0, len(item_ids), 50):
# Fetch the XML
url = HISTORY_URL % (','.join(str(z) for z in item_ids[i:i+50]))
f = urllib2.urlopen(url)
data = f.read()
f.close()
root = ET.fromstring(data)
# Do stuff
for t in root.findall('price_history/type'):
type_id = t.attrib['id']
data = {}
for h in t.findall('history'):
date = h.attrib['date']
minPrice = h.attrib['minPrice']
maxPrice = h.attrib['maxPrice']
avgPrice = h.attrib['avgPrice']
movement = h.attrib['movement']
numOrders = h.attrib['numOrders']
data[date] = (minPrice, maxPrice, avgPrice, movement, numOrders)
# Query that shit
for ph in PriceHistory.objects.filter(region=10000002, item=type_id, date__in=data.keys()):
del data[str(ph.date)]
# Add new ones
for date, row in data.items():
ph = PriceHistory(
region_id=10000002,
item_id=type_id,
date=date,
minimum=row[0],
maximum=row[1],
average=row[2],
movement=row[3],
orders=row[4],
)
ph.save()
if __name__ == '__main__':
main()