Package lastfm :: Module geo
[hide private]
[frames] | no frames]

Source Code for Module lastfm.geo

  1  #!/usr/bin/env python 
  2   
  3  __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>" 
  4  __version__ = "0.2" 
  5  __license__ = "GNU Lesser General Public License" 
  6   
  7  from lastfm.base import LastfmBase 
  8  from lastfm.mixins import Cacheable 
  9  from lastfm.lazylist import lazylist 
10 11 -class Geo(object):
12 """A class representing an geographic location.""" 13 @staticmethod
14 - def get_events(api, 15 location, 16 latitude = None, 17 longitude = None, 18 distance = None):
19 params = {'method': 'geo.getEvents', 'location': location} 20 if distance is not None: 21 params.update({'distance': distance}) 22 23 if latitude is not None and longitude is not None: 24 params.update({'latitude': latitude, 'longitude': longitude}) 25 26 @lazylist 27 def gen(lst): 28 data = api._fetch_data(params).find('events') 29 total_pages = int(data.attrib['totalpages']) 30 31 @lazylist 32 def gen2(lst, data): 33 for e in data.findall('event'): 34 yield Event.create_from_data(api, e)
35 36 for e in gen2(data): 37 yield e 38 39 for page in xrange(2, total_pages+1): 40 params.update({'page': page}) 41 data = api._fetch_data(params).find('events') 42 for e in gen2(data): 43 yield e
44 return gen() 45 46 @staticmethod
47 - def get_top_artists(api, country):
48 params = {'method': 'geo.getTopArtists', 'country': country} 49 data = api._fetch_data(params).find('topartists') 50 return [ 51 Artist( 52 api, 53 name = a.findtext('name'), 54 mbid = a.findtext('mbid'), 55 stats = Stats( 56 subject = a.findtext('name'), 57 rank = int(a.attrib['rank']), 58 playcount = int(a.findtext('playcount')) 59 ), 60 url = 'http://' + a.findtext('url'), 61 image = {'large': a.findtext('image')} 62 ) 63 for a in data.findall('artist') 64 ]
65 66 @staticmethod
67 - def get_top_tracks(api, country, location = None):
68 params = {'method': 'geo.getTopTracks', 'country': country} 69 if location is not None: 70 params.update({'location': location}) 71 72 data = api._fetch_data(params).find('toptracks') 73 return [ 74 Track( 75 api, 76 name = t.findtext('name'), 77 mbid = t.findtext('mbid'), 78 artist = Artist( 79 api, 80 name = t.findtext('artist/name'), 81 mbid = t.findtext('artist/mbid'), 82 url = t.findtext('artist/url') 83 ), 84 stats = Stats( 85 subject = t.findtext('name'), 86 rank = int(t.attrib['rank']), 87 playcount = int(t.findtext('playcount')) 88 ), 89 streamable = (t.findtext('streamable') == '1'), 90 full_track = (t.find('streamable').attrib['fulltrack'] == '1'), 91 url = 'http://' + t.findtext('url'), 92 image = {'large': t.findtext('image')} 93 ) 94 for t in data.findall('track') 95 ]
96
97 -class Location(LastfmBase, Cacheable):
98 """A class representing a location of an event""" 99 XMLNS = "http://www.w3.org/2003/01/geo/wgs84_pos#" 100
101 - def init(self, 102 api, 103 city = None, 104 country = None, 105 street = None, 106 postal_code = None, 107 latitude = None, 108 longitude = None, 109 timezone = None):
110 if not isinstance(api, Api): 111 raise InvalidParametersError("api reference must be supplied as an argument") 112 self._api = api 113 self._city = city 114 self._country = country 115 self._street = street 116 self._postal_code = postal_code 117 self._latitude = latitude 118 self._longitude = longitude 119 self._timezone = timezone
120 121 @property
122 - def city(self):
123 """city in which the location is situated""" 124 return self._city
125 126 @property
127 - def country(self):
128 """country in which the location is situated""" 129 return self._country
130 131 @property
132 - def street(self):
133 """street in which the location is situated""" 134 return self._street
135 136 @property
137 - def postal_code(self):
138 """postal code of the location""" 139 return self._postal_code
140 141 @property
142 - def latitude(self):
143 """latitude of the location""" 144 return self._latitude
145 146 @property
147 - def longitude(self):
148 """longitude of the location""" 149 return self._longitude
150 151 @property
152 - def timezone(self):
153 """timezone in which the location is situated""" 154 return self._timezone
155 156 @LastfmBase.cached_property
157 - def top_tracks(self):
158 """top tracks of the location""" 159 if self.country is None or self.city is None: 160 raise InvalidParametersError("country and city of this location are required for calling this method") 161 return Geo.get_top_tracks(self._api, self.country.name, self.city)
162 163 @LastfmBase.top_property("top_tracks")
164 - def top_track(self):
165 """top track of the location""" 166 pass
167
168 - def get_events(self, 169 distance = None):
170 return Geo.get_events(self._api, 171 self.city, 172 self.latitude, 173 self.longitude, 174 distance)
175 176 @LastfmBase.cached_property
177 - def events(self):
178 """events taking place at/around the location""" 179 return self.get_events()
180 181 @staticmethod
182 - def _hash_func(*args, **kwds):
183 try: 184 return hash("latlong%s%s" % (kwds['latitude'], kwds['longitude'])) 185 except KeyError: 186 try: 187 return hash("name%s" % kwds['city']) 188 except KeyError: 189 raise InvalidParametersError("either latitude and longitude or city has to be provided for hashing")
190
191 - def __hash__(self):
192 if not self.city: 193 return self.__class__._hash_func( 194 latitude = self.latitude, 195 longitude = self.longitude) 196 else: 197 return self.__class__._hash_func(name = self.city)
198
199 - def __eq__(self, other):
200 return self.latitude == other.latitude and self.longitude == other.longitude
201
202 - def __lt__(self, other):
203 if self.country != other.country: 204 return self.country < other.country 205 else: 206 return self.city < other.city
207
208 - def __repr__(self):
209 if self.city is None: 210 return "<lastfm.geo.Location: (%s, %s)>" % (self.latitude, self.longitude) 211 else: 212 return "<lastfm.geo.Location: %s>" % self.city
213
214 -class Country(LastfmBase, Cacheable):
215 """A class representing a country.""" 216 ISO_CODES = { 217 'AD': 'Andorra', 218 'AE': 'United Arab Emirates', 219 'AF': 'Afghanistan', 220 'AG': 'Antigua and Barbuda', 221 'AI': 'Anguilla', 222 'AL': 'Albania', 223 'AM': 'Armenia', 224 'AN': 'Netherlands Antilles', 225 'AO': 'Angola', 226 'AQ': 'Antarctica', 227 'AR': 'Argentina', 228 'AS': 'American Samoa', 229 'AT': 'Austria', 230 'AU': 'Australia', 231 'AW': 'Aruba', 232 'AX': 'land Islands', 233 'AZ': 'Azerbaijan', 234 'BA': 'Bosnia and Herzegovina', 235 'BB': 'Barbados', 236 'BD': 'Bangladesh', 237 'BE': 'Belgium', 238 'BF': 'Burkina Faso', 239 'BG': 'Bulgaria', 240 'BH': 'Bahrain', 241 'BI': 'Burundi', 242 'BJ': 'Benin', 243 'BL': 'Saint Barthlemy', 244 'BM': 'Bermuda', 245 'BN': 'Brunei Darussalam', 246 'BO': 'Bolivia', 247 'BR': 'Brazil', 248 'BS': 'Bahamas', 249 'BT': 'Bhutan', 250 'BV': 'Bouvet Island', 251 'BW': 'Botswana', 252 'BY': 'Belarus', 253 'BZ': 'Belize', 254 'CA': 'Canada', 255 'CC': 'Cocos (Keeling) Islands', 256 'CD': 'Congo, The Democratic Republic of the', 257 'CF': 'Central African Republic', 258 'CG': 'Congo', 259 'CH': 'Switzerland', 260 'CI': "Cte d'Ivoire", 261 'CK': 'Cook Islands', 262 'CL': 'Chile', 263 'CM': 'Cameroon', 264 'CN': 'China', 265 'CO': 'Colombia', 266 'CR': 'Costa Rica', 267 'CU': 'Cuba', 268 'CV': 'Cape Verde', 269 'CX': 'Christmas Island', 270 'CY': 'Cyprus', 271 'CZ': 'Czech Republic', 272 'DE': 'Germany', 273 'DJ': 'Djibouti', 274 'DK': 'Denmark', 275 'DM': 'Dominica', 276 'DO': 'Dominican Republic', 277 'DZ': 'Algeria', 278 'EC': 'Ecuador', 279 'EE': 'Estonia', 280 'EG': 'Egypt', 281 'EH': 'Western Sahara', 282 'ER': 'Eritrea', 283 'ES': 'Spain', 284 'ET': 'Ethiopia', 285 'FI': 'Finland', 286 'FJ': 'Fiji', 287 'FK': 'Falkland Islands (Malvinas)', 288 'FM': 'Micronesia, Federated States of', 289 'FO': 'Faroe Islands', 290 'FR': 'France', 291 'GA': 'Gabon', 292 'GB': 'United Kingdom', 293 'GD': 'Grenada', 294 'GE': 'Georgia', 295 'GF': 'French Guiana', 296 'GG': 'Guernsey', 297 'GH': 'Ghana', 298 'GI': 'Gibraltar', 299 'GL': 'Greenland', 300 'GM': 'Gambia', 301 'GN': 'Guinea', 302 'GP': 'Guadeloupe', 303 'GQ': 'Equatorial Guinea', 304 'GR': 'Greece', 305 'GS': 'South Georgia and the South Sandwich Islands', 306 'GT': 'Guatemala', 307 'GU': 'Guam', 308 'GW': 'Guinea-Bissau', 309 'GY': 'Guyana', 310 'HK': 'Hong Kong', 311 'HM': 'Heard Island and McDonald Islands', 312 'HN': 'Honduras', 313 'HR': 'Croatia', 314 'HT': 'Haiti', 315 'HU': 'Hungary', 316 'ID': 'Indonesia', 317 'IE': 'Ireland', 318 'IL': 'Israel', 319 'IM': 'Isle of Man', 320 'IN': 'India', 321 'IO': 'British Indian Ocean Territory', 322 'IQ': 'Iraq', 323 'IR': 'Iran, Islamic Republic of', 324 'IS': 'Iceland', 325 'IT': 'Italy', 326 'JE': 'Jersey', 327 'JM': 'Jamaica', 328 'JO': 'Jordan', 329 'JP': 'Japan', 330 'KE': 'Kenya', 331 'KG': 'Kyrgyzstan', 332 'KH': 'Cambodia', 333 'KI': 'Kiribati', 334 'KM': 'Comoros', 335 'KN': 'Saint Kitts and Nevis', 336 'KP': "Korea, Democratic People's Republic of", 337 'KR': 'Korea, Republic of', 338 'KW': 'Kuwait', 339 'KY': 'Cayman Islands', 340 'KZ': 'Kazakhstan', 341 'LA': "Lao People's Democratic Republic", 342 'LB': 'Lebanon', 343 'LC': 'Saint Lucia', 344 'LI': 'Liechtenstein', 345 'LK': 'Sri Lanka', 346 'LR': 'Liberia', 347 'LS': 'Lesotho', 348 'LT': 'Lithuania', 349 'LU': 'Luxembourg', 350 'LV': 'Latvia', 351 'LY': 'Libyan Arab Jamahiriya', 352 'MA': 'Morocco', 353 'MC': 'Monaco', 354 'MD': 'Moldova', 355 'ME': 'Montenegro', 356 'MF': 'Saint Martin', 357 'MG': 'Madagascar', 358 'MH': 'Marshall Islands', 359 'MK': 'Macedonia, The Former Yugoslav Republic of', 360 'ML': 'Mali', 361 'MM': 'Myanmar', 362 'MN': 'Mongolia', 363 'MO': 'Macao', 364 'MP': 'Northern Mariana Islands', 365 'MQ': 'Martinique', 366 'MR': 'Mauritania', 367 'MS': 'Montserrat', 368 'MT': 'Malta', 369 'MU': 'Mauritius', 370 'MV': 'Maldives', 371 'MW': 'Malawi', 372 'MX': 'Mexico', 373 'MY': 'Malaysia', 374 'MZ': 'Mozambique', 375 'NA': 'Namibia', 376 'NC': 'New Caledonia', 377 'NE': 'Niger', 378 'NF': 'Norfolk Island', 379 'NG': 'Nigeria', 380 'NI': 'Nicaragua', 381 'NL': 'Netherlands', 382 'NO': 'Norway', 383 'NP': 'Nepal', 384 'NR': 'Nauru', 385 'NU': 'Niue', 386 'NZ': 'New Zealand', 387 'OM': 'Oman', 388 'PA': 'Panama', 389 'PE': 'Peru', 390 'PF': 'French Polynesia', 391 'PG': 'Papua New Guinea', 392 'PH': 'Philippines', 393 'PK': 'Pakistan', 394 'PL': 'Poland', 395 'PM': 'Saint Pierre and Miquelon', 396 'PN': 'Pitcairn', 397 'PR': 'Puerto Rico', 398 'PS': 'Palestinian Territory, Occupied', 399 'PT': 'Portugal', 400 'PW': 'Palau', 401 'PY': 'Paraguay', 402 'QA': 'Qatar', 403 'RE': 'Runion', 404 'RO': 'Romania', 405 'RS': 'Serbia', 406 'RU': 'Russian Federation', 407 'RW': 'Rwanda', 408 'SA': 'Saudi Arabia', 409 'SB': 'Solomon Islands', 410 'SC': 'Seychelles', 411 'SD': 'Sudan', 412 'SE': 'Sweden', 413 'SG': 'Singapore', 414 'SH': 'Saint Helena', 415 'SI': 'Slovenia', 416 'SJ': 'Svalbard and Jan Mayen', 417 'SK': 'Slovakia', 418 'SL': 'Sierra Leone', 419 'SM': 'San Marino', 420 'SN': 'Senegal', 421 'SO': 'Somalia', 422 'SR': 'Suriname', 423 'ST': 'Sao Tome and Principe', 424 'SV': 'El Salvador', 425 'SY': 'Syrian Arab Republic', 426 'SZ': 'Swaziland', 427 'TC': 'Turks and Caicos Islands', 428 'TD': 'Chad', 429 'TF': 'French Southern Territories', 430 'TG': 'Togo', 431 'TH': 'Thailand', 432 'TJ': 'Tajikistan', 433 'TK': 'Tokelau', 434 'TL': 'Timor-Leste', 435 'TM': 'Turkmenistan', 436 'TN': 'Tunisia', 437 'TO': 'Tonga', 438 'TR': 'Turkey', 439 'TT': 'Trinidad and Tobago', 440 'TV': 'Tuvalu', 441 'TW': 'Taiwan, Province of China', 442 'TZ': 'Tanzania, United Republic of', 443 'UA': 'Ukraine', 444 'UG': 'Uganda', 445 'UM': 'United States Minor Outlying Islands', 446 'US': 'United States', 447 'UY': 'Uruguay', 448 'UZ': 'Uzbekistan', 449 'VA': 'Holy See (Vatican City State)', 450 'VC': 'Saint Vincent and the Grenadines', 451 'VE': 'Venezuela', 452 'VG': 'Virgin Islands, British', 453 'VI': 'Virgin Islands, U.S.', 454 'VN': 'Viet Nam', 455 'VU': 'Vanuatu', 456 'WF': 'Wallis and Futuna', 457 'WS': 'Samoa', 458 'YE': 'Yemen', 459 'YT': 'Mayotte', 460 'ZA': 'South Africa', 461 'ZM': 'Zambia', 462 'ZW': 'Zimbabwe'}
463 - def init(self, 464 api, 465 name = None):
466 if not isinstance(api, Api): 467 raise InvalidParametersError("api reference must be supplied as an argument") 468 self._api = api 469 self._name = name
470 471 @property
472 - def name(self):
473 """name of the country""" 474 return self._name
475 476 @LastfmBase.cached_property
477 - def top_artists(self):
478 """top artists of the country""" 479 return Geo.get_top_artists(self._api, self.name)
480 481 @LastfmBase.top_property("top_artists")
482 - def top_artist(self):
483 """top artist of the country""" 484 pass
485
486 - def get_top_tracks(self, location = None):
487 return Geo.get_top_tracks(self._api, self.name, location)
488 489 @LastfmBase.cached_property
490 - def top_tracks(self):
491 """top tracks of the country""" 492 return self.get_top_tracks()
493 494 @LastfmBase.top_property("top_tracks")
495 - def top_track(self):
496 """top track of the country""" 497 pass
498 499 @LastfmBase.cached_property
500 - def events(self):
501 """events taking place at/around the location""" 502 return Geo.get_events(self._api, self.name)
503 504 @staticmethod
505 - def _hash_func(*args, **kwds):
506 try: 507 return hash(kwds['name'].lower()) 508 except KeyError: 509 raise InvalidParametersError("name has to be provided for hashing")
510
511 - def __hash__(self):
512 return self.__class__._hash_func(name = self.name)
513
514 - def __eq__(self, other):
515 return self.name.lower() == other.name.lower()
516
517 - def __lt__(self, other):
518 return self.name < other.name
519
520 - def __repr__(self):
521 return "<lastfm.geo.Country: %s>" % self.name
522 523 from lastfm.api import Api 524 from lastfm.artist import Artist 525 from lastfm.error import InvalidParametersError 526 from lastfm.event import Event 527 from lastfm.stats import Stats 528 from lastfm.track import Track 529