]>
Commit | Line | Data |
---|---|---|
685e346e A |
1 | import feed |
2 | import utils | |
3 | from feed import get_json, HtmlFeed, XmlFeed | |
4 | from datetime import datetime | |
5 | from decimal import Decimal | |
6 | from urllib import quote | |
7 | from ..erepublik_utils import Object | |
8 | ||
9 | def from_id(id): | |
10 | return Citizen(XmlFeed('http://api.erepublik.com/v2/feeds/citizens/%d' % int(id))) | |
11 | ||
12 | def from_name(name): | |
13 | if isinstance(name, unicode): | |
14 | name = name.encode('utf-8') | |
15 | ||
16 | return Citizen(XmlFeed('http://api.erepublik.com/v2/feeds/citizen_by_name/xml/%s' % quote(name))) | |
17 | ||
18 | def from_dict(dict): | |
19 | return from_id(dict['id']) | |
20 | ||
21 | def from_heapy(s, by_id=False): | |
22 | c = get_json('http://api.1way.it/erep/citizen/%s/%s' % ('id' if by_id else 'name', s)) | |
23 | if 'error' in c: | |
24 | raise feed.FeedError(c['error']) | |
25 | ||
26 | c['strength'] = Decimal(c['strength']) | |
27 | c['rank_points'] = c['rank']['points'] | |
28 | c['rank'] = utils.get_rank_by_points(c['rank_points']) | |
29 | c['birth_date'] = datetime.strptime(c['birth_date'], '%Y-%m-%d %H:%M:%S') | |
30 | c['age'] = (datetime.utcnow() - c['birth_date']).days | |
31 | c['citizenship'] = c['citizenship']['country'] | |
32 | c['country'] = c['residence']['country'] | |
33 | c['region'] = c['residence']['region'] | |
34 | c['is_organization'] = False | |
35 | c['employer'] = None # who cares | |
36 | c['is_party_member'] = False | |
37 | c['party'] = None # todo (?) | |
38 | c['avatar'] = c['avatar_link'] | |
39 | return Object(**c) | |
40 | ||
41 | def from_html(s): | |
42 | page = HtmlFeed('http://www.erepublik.com/en/citizen/profile/%d' % s) | |
43 | cname = page.html().split('<title>', 1)[1].split('</title>')[0].rsplit(' -', 1)[0] | |
44 | get_json('http://api.1way.it/erep/citizen/add/%d' % s) | |
45 | return Object(**{'id': s, 'name': cname}) | |
46 | ||
47 | class Citizen: | |
48 | """A citizen of Erepublik""" | |
49 | ||
50 | def __init__(self, f): | |
51 | self.id = f.int('/citizen/id') | |
52 | self.name = f.text('/citizen/name') | |
53 | self.sex = f.text('/citizen/sex') | |
54 | self.avatar = f.text('/citizen/avatar-link') | |
55 | self.strength = f.decimal('/citizen/military-skills/military-skill[1]/points', Decimal('0')) | |
56 | self.rank_points = f.int('/citizen/military/rank-points', 0) | |
57 | self.stars = f.int('/citizen/military/stars', 0) | |
58 | self.rank = utils.get_rank_by_points(self.rank_points) | |
59 | self.damage = f.int('/citizen/military/total-damage', 0) | |
60 | self.fights = f.int('/citizen/military/fight-count', 0) | |
61 | self.level = f.int('/citizen/level') | |
62 | self.experience = f.int('/citizen/experience-points') | |
63 | self.wellness = f.decimal('/citizen/wellness') | |
64 | bd = f.text('/citizen/date-of-birth') | |
65 | self.birth_date = datetime.strptime(bd[:-1], '%Y-%m-%dT%H:%M:%S') | |
66 | self.age = (datetime.utcnow() - self.birth_date).days | |
67 | self.citizenship = { | |
68 | 'name': f.text('/citizen/citizenship/country/name'), | |
69 | 'id': f.int('/citizen/citizenship/country/id'), | |
70 | 'code': f.text('/citizen/citizenship/country/code')} | |
71 | self.country = { | |
72 | 'name': f.text('/citizen/residence/country/name'), | |
73 | 'id': f.int('/citizen/residence/country/id'), | |
74 | 'code': f.text('/citizen/residence/country/code')} | |
75 | self.region = { | |
76 | 'name': f.text('/citizen/residence/region/name'), | |
77 | 'id': f.int('/citizen/residence/region/id')} | |
78 | self.is_general_manager = f.bool('/citizen/is-general-manager') | |
79 | self.is_congressman = f.bool('/citizen/is-congressman') | |
80 | self.is_president = f.bool('/citizen/is-president') | |
81 | self.is_organization = f.bool('/citizen/is-organization') | |
82 | ||
83 | emp = f.text('/citizen/employer/name') | |
84 | ||
85 | if emp == None: | |
86 | self.employer = None | |
87 | else: | |
88 | self.employer = {'name': emp, 'id': f.int('/citizen/employer/id')} | |
89 | ||
90 | party = f.text('/citizen/party/name') | |
91 | ||
92 | if party == None: | |
93 | self.party = None | |
94 | self.is_party_member = False | |
95 | else: | |
96 | self.is_party_member = True | |
97 | self.party = {'name': party, 'id': f.int('/citizen/party/id'), 'president': f.bool('/citizen/party/president')} | |
98 | ||
99 | self.medals = dict( | |
100 | [medal.text('type'), medal.int('amount')] | |
101 | for medal in f.elements('/citizen/medals/medal') if medal.int('amount') > 0) | |
102 | ||
103 | def __str__(self): | |
104 | return '[%d] %s' % (self.id, self.name) |