]>
jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/erepublik/api/map.py
1 from feed
import XmlFeed
, get_json
11 if cached_map
== None:
12 cached_map
= get_map()
16 def get_country_name(s
):
17 map = get_cached_map()
19 if isinstance(s
, int):
24 for c
in map.countries
:
25 if s
== c
['name'].lower() or s
== c
['code'].lower() or s
== str(c
['id']):
30 #def get_country_link(name):
31 # country_feed = country.get_cached_countries()
33 # countries = country_feed.elements('/countries/country')
37 # if name == c.text('name').lower() or name == c.text('code').lower():
38 # return c.text('permalink')
42 def get_country_id(name
):
43 map = get_cached_map()
46 for c
in map.countries
:
47 if name
== c
['name'].lower() or name
== c
['code'].lower():
52 def get_country_currency(val
):
53 map = get_cached_map()
54 countries
= map.countries
56 if isinstance(val
, str) or isinstance(val
, unicode):
59 if val
== c
['name'].lower() or val
== c
['code'].lower():
61 elif isinstance(val
, int):
65 elif isinstance(val
, dict):
67 if val
['id'] == c
['id']:
72 def get_region_name(region_id
):
73 map = get_cached_map()
75 if r
['id'] == int(region_id
):
80 def get_region_id(region_name
):
81 map = get_cached_map()
84 if r
['name'].lower().startswith(region_name
.lower()):
89 def get_region(search
):
90 map = get_cached_map()
92 search
= search
.lower()
94 if str(r
['id']) == search
or r
['name'].lower().startswith(search
):
99 def get_region_zone(region_name
):
100 map = get_cached_map()
101 for region
in map.regions
:
102 if region
['name'].lower().startswith(region_name
.lower()):
103 return region
['zone']
106 def get_region_distance(start
, end
):
107 x1
, x2
= start
.lower()
111 dist_x
= abs(ord(x1
) - ord(y1
))
114 dist_y
= abs(x2
- y2
)
119 return dist_x
+ dist_y
+ 1
121 def get_battles(searchstr
):
122 searchstr
= searchstr
.lower()
123 f
= XmlFeed('http://api.erepublik.com/map/data/')
126 for battle
in f
.elements('/countries/country/regions/region/battles/battle'):
127 battle_id
= int(battle
.text('@b_id'))
128 attacker_id
= int(battle
.text('@c_id'))
129 attacker
= get_country_name(attacker_id
)
130 region_id
= battle
.text('../../@r_id')
131 region
= get_region_name(region_id
)
132 defender_id
= int(battle
.text('../../../../@c_id'))
133 defender
= get_country_name(defender_id
)
134 if searchstr
== '' or searchstr
== attacker
.lower() or searchstr
== region
.lower() or searchstr
== defender
.lower():
136 'battle_id': battle_id
,
137 'attacker_id': attacker_id
,
138 'attacker': attacker
,
139 'region_id': region_id
,
141 'defender_id': defender_id
,
147 def get_mpp_list(country_id
):
148 map = XmlFeed('http://api.erepublik.com/map/data/')
151 # for mpp in map.elements('/countries/country[@c_id = "%d"]/mpps/mpp' % country_id): bug with pypy 1.5.0-alpha0 / python 2.7.1
152 for country
in map.elements('/countries/country'):
153 if country
.int('@c_id') == int(country_id
):
154 for mpp
in country
.elements('mpps/mpp'):
156 'id': int(mpp
.text('@c_id')), 'country': get_country_name(int(mpp
.text('@c_id'))),
157 'expiration': mpp
.text('@expires')})
163 data
= get_json('http://api.1way.it/erep/map')
164 self
.countries
= data
['countries']
165 self
.regions
= data
['regions']