|
| 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