Skip to content

Commit 1e89148

Browse files
committed
Reimplement the 'fix names' task, oops
1 parent 530f901 commit 1e89148

3 files changed

Lines changed: 90 additions & 20 deletions

File tree

evething/settings.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,13 @@
243243
'args': (),
244244
},
245245

246-
# # update unknown Characters every hour
247-
# 'fix-unknown-characters': {
248-
# 'task': 'thing.tasks.fix_unknown_characters',
249-
# 'schedule': timedelta(hours=1),
250-
# 'options': {
251-
# 'expires': 59 * 60,
252-
# },
253-
# 'args': (),
254-
# },
255-
256-
# # generate hourly task summaries at xx:01
257-
# 'task-summaries': {
258-
# 'task': 'thing.tasks.task_summaries',
259-
# 'schedule': crontab(minute=1),
260-
# 'options': {
261-
# 'expires': 9,
262-
# 'queue': 'et_high',
263-
# },
264-
# 'args': (),
265-
# },
246+
# update unknown character/corporation names every hour
247+
'fix-names': {
248+
'task': 'thing.fix_names',
249+
'schedule': timedelta(hours=1),
250+
'options': {
251+
'expires': 59 * 60,
252+
},
253+
'args': (),
254+
},
266255
}

thing/tasks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
from serverstatus import ServerStatus
3030

3131
# Periodic tasks
32+
from fixnames import FixNames
3233
from historyupdater import HistoryUpdater
3334
from priceupdater import PriceUpdater

thing/tasks/fixnames.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from .apitask import APITask
2+
3+
from thing.models import Character, Corporation
4+
5+
# ---------------------------------------------------------------------------
6+
# Periodic task to try to fix *UNKNOWN* Character objects
7+
CHAR_NAME_URL = '/eve/CharacterName.xml.aspx'
8+
CORP_SHEET_URL = '/corp/CorporationSheet.xml.aspx'
9+
10+
class FixNames(APITask):
11+
name = 'thing.fix_names'
12+
13+
def run(self):
14+
self.init()
15+
16+
# Fetch all unknown Character objects
17+
char_map = {}
18+
for char in Character.objects.filter(name='*UNKNOWN*'):
19+
char_map[char.id] = char
20+
21+
# Fetch all unknown Corporation objects
22+
corp_map = {}
23+
for corp in Corporation.objects.filter(name='*UNKNOWN*'):
24+
corp_map[corp.id] = corp
25+
26+
ids = list(set(char_map.keys()) | set(corp_map.keys()))
27+
if len(ids) == 0:
28+
return
29+
30+
# Go fetch names for them
31+
name_map = {}
32+
for i in range(0, len(ids), 250):
33+
params = { 'ids': ','.join(map(str, ids[i:i+250])) }
34+
35+
if self.fetch_api(CHAR_NAME_URL, params, use_auth=False) is False or self.root is None:
36+
return False
37+
38+
# <row name="Tazuki Falorn" characterID="1759080617"/>
39+
for row in self.root.findall('result/rowset/row'):
40+
name_map[int(row.attrib['characterID'])] = row.attrib['name']
41+
42+
if len(name_map) == 0:
43+
return
44+
45+
# Fix corporation names first
46+
for id, corp in corp_map.items():
47+
corp_name = name_map.get(id)
48+
if corp_name is not None:
49+
corp.name = corp_name
50+
corp.save()
51+
del name_map[id]
52+
53+
# Ugh, now go look up all of the damn names just in case they're corporations
54+
new_corps = []
55+
for id, name in name_map.items():
56+
params = { 'corporationID': id }
57+
58+
# Not a corporation, update the Character object
59+
if self.fetch_api(CORP_SHEET_URL, params, use_auth=False) is False or self.root is None:
60+
char = char_map.get(id)
61+
char.name = name
62+
char.save()
63+
else:
64+
new_corps.append(Corporation(
65+
id=id,
66+
name=name,
67+
ticker=self.root.find('result/ticker').text,
68+
))
69+
70+
# Now we can create the new corporation objects
71+
corp_map = Corporation.objects.in_bulk([c.id for c in new_corps])
72+
new_corps = [c for c in new_corps if c.id not in corp_map]
73+
Corporation.objects.bulk_create(new_corps)
74+
75+
# And finally delete any characters that have equivalent corporations now
76+
cursor = self.get_cursor()
77+
cursor.execute('DELETE FROM thing_character WHERE id IN (SELECT id FROM thing_corporation)')
78+
cursor.close()
79+
80+
return True

0 commit comments

Comments
 (0)