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
10 return Citizen(XmlFeed('http://api.erepublik.com/v2/feeds/citizens/%d' % int(id)))
13 if isinstance(name
, unicode):
14 name
= name
.encode('utf-8')
16 return Citizen(XmlFeed('http://api.erepublik.com/v2/feeds/citizen_by_name/xml/%s' % quote(name
)))
19 return from_id(dict['id'])
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
))
24 raise feed
.FeedError(c
['error'])
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']
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}
)
48 """A citizen of Erepublik"""
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
68 'name': f
.text('/citizen/citizenship/country/name'),
69 'id': f
.int('/citizen/citizenship/country/id'),
70 'code': f
.text('/citizen/citizenship/country/code')}
72 'name': f
.text('/citizen/residence/country/name'),
73 'id': f
.int('/citizen/residence/country/id'),
74 'code': f
.text('/citizen/residence/country/code')}
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')
83 emp
= f
.text('/citizen/employer/name')
88 self
.employer
= {'name': emp, 'id': f.int('/citizen/employer/id')}
90 party
= f
.text('/citizen/party/name')
94 self
.is_party_member
= False
96 self
.is_party_member
= True
97 self
.party
= {'name': party, 'id': f.int('/citizen/party/id'), 'president': f.bool('/citizen/party/president')}
100 [medal
.text('type'), medal
.int('amount')]
101 for medal
in f
.elements('/citizen/medals/medal') if medal
.int('amount') > 0)
104 return '[%d] %s' % (self
.id, self
.name
)