/src/pyechonest/pyechonest/artist.py

http://echo-nest-remix.googlecode.com/ · Python · 1015 lines · 942 code · 13 blank · 60 comment · 14 complexity · ea5d8ec2dd9a41b96341c3183b882e8f MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. Copyright (c) 2010 The Echo Nest. All rights reserved.
  5. Created by Tyler Williams on 2010-04-25.
  6. The Artist module loosely covers http://developer.echonest.com/docs/v4/artist.html
  7. Refer to the official api documentation if you are unsure about something.
  8. """
  9. import util
  10. from proxies import ArtistProxy, ResultList
  11. from song import Song
  12. class Artist(ArtistProxy):
  13. """
  14. An Artist object
  15. Attributes:
  16. id (str): Echo Nest Artist ID
  17. name (str): Artist Name
  18. audio (list): Artist audio
  19. biographies (list): Artist biographies
  20. blogs (list): Artist blogs
  21. familiarity (float): Artist familiarity
  22. hotttnesss (float): Artist hotttnesss
  23. images (list): Artist images
  24. news (list): Artist news
  25. reviews (list): Artist reviews
  26. similar (list): Similar Artists
  27. songs (list): A list of song objects
  28. terms (list): Terms for an artist
  29. urls (list): Artist urls
  30. video (list): Artist video
  31. years_active (list): A list of dictionaries containing start and stop years
  32. You create an artist object like this:
  33. >>> a = artist.Artist('ARH6W4X1187B99274F')
  34. >>> a = artist.Artist('the national')
  35. >>> a = artist.Artist('musicbrainz:artist:a74b1b7f-71a5-4011-9441-d0b5e4122711')
  36. """
  37. def __init__(self, id, **kwargs):
  38. """
  39. Artist class
  40. Args:
  41. id (str): an artistw ID
  42. Returns:
  43. An artist object
  44. Example:
  45. >>> a = artist.Artist('ARH6W4X1187B99274F', buckets=['hotttnesss'])
  46. >>> a.hotttnesss
  47. 0.80098515900997658
  48. >>>
  49. """
  50. super(Artist, self).__init__(id, **kwargs)
  51. def __repr__(self):
  52. return "<%s - %s>" % (self._object_type.encode('utf-8'), self.name.encode('utf-8'))
  53. def __str__(self):
  54. return self.name.encode('utf-8')
  55. def __cmp__(self, other):
  56. return cmp(self.id, other.id)
  57. def get_audio(self, results=15, start=0, cache=True):
  58. """Get a list of audio documents found on the web related to an artist
  59. Args:
  60. Kwargs:
  61. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  62. results (int): An integer number of results to return
  63. start (int): An integer starting value for the result set
  64. Returns:
  65. A list of audio document dicts; list contains additional attributes 'start' and 'total'
  66. Example:
  67. >>> a = artist.Artist('alphabeat')
  68. >>> a.get_audio()[0]
  69. {u'artist': u'Alphabeat',
  70. u'date': u'2010-04-28T01:40:45',
  71. u'id': u'70be4373fa57ac2eee8c7f30b0580899',
  72. u'length': 210.0,
  73. u'link': u'http://iamthecrime.com',
  74. u'release': u'The Beat Is...',
  75. u'title': u'DJ',
  76. u'url': u'http://iamthecrime.com/wp-content/uploads/2010/04/03_DJ_iatc.mp3'}
  77. >>>
  78. """
  79. if cache and ('audio' in self.cache) and results==15 and start==0:
  80. return self.cache['audio']
  81. else:
  82. response = self.get_attribute('audio', results=results, start=start)
  83. if results==15 and start==0:
  84. self.cache['audio'] = ResultList(response['audio'], 0, response['total'])
  85. return ResultList(response['audio'], start, response['total'])
  86. audio = property(get_audio)
  87. def get_biographies(self, results=15, start=0, license=None, cache=True):
  88. """Get a list of artist biographies
  89. Args:
  90. Kwargs:
  91. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  92. results (int): An integer number of results to return
  93. start (int): An integer starting value for the result set
  94. license (str): A string specifying the desired license type
  95. Returns:
  96. A list of biography document dicts; list contains additional attributes 'start' and 'total'
  97. Example:
  98. >>> a = artist.Artist('britney spears')
  99. >>> bio = a.get_biographies(results=1)[0]
  100. >>> bio['url']
  101. u'http://www.mtvmusic.com/spears_britney'
  102. >>>
  103. """
  104. if cache and ('biographies' in self.cache) and results==15 and start==0 and license==None:
  105. return self.cache['biographies']
  106. else:
  107. response = self.get_attribute('biographies', results=results, start=start, license=license)
  108. if results==15 and start==0 and license==None:
  109. self.cache['biographies'] = ResultList(response['biographies'], 0, response['total'])
  110. return ResultList(response['biographies'], start, response['total'])
  111. biographies = property(get_biographies)
  112. def get_blogs(self, results=15, start=0, cache=True, high_relevance=False):
  113. """Get a list of blog articles related to an artist
  114. Args:
  115. Kwargs:
  116. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  117. results (int): An integer number of results to return
  118. start (int): An ingteger starting value for the result set
  119. Returns:
  120. A list of blog document dicts; list contains additional attributes 'start' and 'total'
  121. Example:
  122. >>> a = artist.Artist('bob marley')
  123. >>> blogs = a.get_blogs(results=1,start=4)
  124. >>> blogs.total
  125. 4068
  126. >>> blogs[0]['summary']
  127. But the Kenyans I know relate to music about the same way Americans do. They like their Congolese afropop,
  128. and I've known some to be big fans of international acts like <span>Bob</span> <span>Marley</span> and Dolly Parton.
  129. They rarely talk about music that's indigenous in the way a South African or Malian or Zimbabwean would, and it's
  130. even rarer to actually hear such indigenous music. I do sometimes hear ceremonial chanting from the Maasai, but only
  131. when they're dancing for tourists. If East Africa isn't the most musical part ... "
  132. >>>
  133. """
  134. if cache and ('blogs' in self.cache) and results==15 and start==0 and not high_relevance:
  135. return self.cache['blogs']
  136. else:
  137. high_relevance = 'true' if high_relevance else 'false'
  138. response = self.get_attribute('blogs', results=results, start=start, high_relevance=high_relevance)
  139. if results==15 and start==0:
  140. self.cache['blogs'] = ResultList(response['blogs'], 0, response['total'])
  141. return ResultList(response['blogs'], start, response['total'])
  142. blogs = property(get_blogs)
  143. def get_familiarity(self, cache=True):
  144. """Get our numerical estimation of how familiar an artist currently is to the world
  145. Args:
  146. Kwargs:
  147. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  148. Returns:
  149. A float representing familiarity.
  150. Example:
  151. >>> a = artist.Artist('frank sinatra')
  152. >>> a.get_familiarity()
  153. 0.65142555825947457
  154. >>> a.familiarity
  155. 0.65142555825947457
  156. >>>
  157. """
  158. if not (cache and ('familiarity' in self.cache)):
  159. response = self.get_attribute('familiarity')
  160. self.cache['familiarity'] = response['artist']['familiarity']
  161. return self.cache['familiarity']
  162. familiarity = property(get_familiarity)
  163. def get_foreign_id(self, idspace='musicbrainz', cache=True):
  164. """Get the foreign id for this artist for a specific id space
  165. Args:
  166. Kwargs:
  167. idspace (str): A string indicating the idspace to fetch a foreign id for.
  168. Returns:
  169. A foreign ID string
  170. Example:
  171. >>> a = artist.Artist('fabulous')
  172. >>> a.get_foreign_id('7digital')
  173. u'7digital:artist:186042'
  174. >>>
  175. """
  176. if not (cache and ('foreign_ids' in self.cache) and filter(lambda d: d.get('catalog') == idspace, self.cache['foreign_ids'])):
  177. response = self.get_attribute('profile', bucket=['id:'+idspace])
  178. foreign_ids = response['artist'].get("foreign_ids", [])
  179. self.cache['foreign_ids'] = self.cache.get('foreign_ids', []) + foreign_ids
  180. cval = filter(lambda d: d.get('catalog') == idspace, self.cache.get('foreign_ids'))
  181. return cval[0].get('foreign_id') if cval else None
  182. def get_hotttnesss(self, cache=True):
  183. """Get our numerical description of how hottt an artist currently is
  184. Args:
  185. Kwargs:
  186. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  187. Returns:
  188. float: the hotttnesss value
  189. Example:
  190. >>> a = artist.Artist('hannah montana')
  191. >>> a.get_hotttnesss()
  192. 0.59906022155998995
  193. >>> a.hotttnesss
  194. 0.59906022155998995
  195. >>>
  196. """
  197. if not (cache and ('hotttnesss' in self.cache)):
  198. response = self.get_attribute('hotttnesss')
  199. self.cache['hotttnesss'] = response['artist']['hotttnesss']
  200. return self.cache['hotttnesss']
  201. hotttnesss = property(get_hotttnesss)
  202. def get_images(self, results=15, start=0, license=None, cache=True):
  203. """Get a list of artist images
  204. Args:
  205. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  206. results (int): An integer number of results to return
  207. start (int): An integer starting value for the result set
  208. license (str): A string specifying the desired license type
  209. Returns:
  210. A list of image document dicts; list contains additional attributes 'start' and 'total'
  211. Example:
  212. >>> a = artist.Artist('Captain Beefheart')
  213. >>> images = a.get_images(results=1)
  214. >>> images.total
  215. 49
  216. >>> images[0]['url']
  217. u'http://c4.ac-images.myspacecdn.com/images01/5/l_e1a329cdfdb16a848288edc6d578730f.jpg'
  218. >>>
  219. """
  220. if cache and ('images' in self.cache) and results==15 and start==0 and license==None:
  221. return self.cache['images']
  222. else:
  223. response = self.get_attribute('images', results=results, start=start, license=license)
  224. if results==15 and start==0 and license==None:
  225. self.cache['images'] = ResultList(response['images'], 0, response['total'])
  226. return ResultList(response['images'], start, response['total'])
  227. images = property(get_images)
  228. def get_news(self, results=15, start=0, cache=True, high_relevance=False):
  229. """Get a list of news articles found on the web related to an artist
  230. Args:
  231. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  232. results (int): An integer number of results to return
  233. start (int): An integer starting value for the result set
  234. Returns:
  235. A list of news document dicts; list contains additional attributes 'start' and 'total'
  236. Example:
  237. >>> a = artist.Artist('Henry Threadgill')
  238. >>> news = a.news
  239. >>> news.total
  240. 41
  241. >>> news[0]['name']
  242. u'Jazz Journalists Association Announces 2010 Jazz Award Winners'
  243. >>>
  244. """
  245. if cache and ('news' in self.cache) and results==15 and start==0 and not high_relevance:
  246. return self.cache['news']
  247. else:
  248. high_relevance = 'true' if high_relevance else 'false'
  249. response = self.get_attribute('news', results=results, start=start, high_relevance=high_relevance)
  250. if results==15 and start==0:
  251. self.cache['news'] = ResultList(response['news'], 0, response['total'])
  252. return ResultList(response['news'], start, response['total'])
  253. news = property(get_news)
  254. def get_reviews(self, results=15, start=0, cache=True):
  255. """Get reviews related to an artist's work
  256. Args:
  257. Kwargs:
  258. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  259. results (int): An integer number of results to return
  260. start (int): An integer starting value for the result set
  261. Returns:
  262. A list of review document dicts; list contains additional attributes 'start' and 'total'
  263. Example:
  264. >>> a = artist.Artist('Ennio Morricone')
  265. >>> reviews = a.reviews
  266. >>> reviews.total
  267. 17
  268. >>> reviews[0]['release']
  269. u'For A Few Dollars More'
  270. >>>
  271. """
  272. if cache and ('reviews' in self.cache) and results==15 and start==0:
  273. return self.cache['reviews']
  274. else:
  275. response = self.get_attribute('reviews', results=results, start=start)
  276. if results==15 and start==0:
  277. self.cache['reviews'] = ResultList(response['reviews'], 0, response['total'])
  278. return ResultList(response['reviews'], start, response['total'])
  279. reviews = property(get_reviews)
  280. def get_similar(self, results=15, start=0, buckets=None, limit=False, cache=True, max_familiarity=None, min_familiarity=None, \
  281. max_hotttnesss=None, min_hotttnesss=None, min_results=None, reverse=False, artist_start_year_before=None, \
  282. artist_start_year_after=None,artist_end_year_before=None,artist_end_year_after=None):
  283. """Return similar artists to this one
  284. Args:
  285. Kwargs:
  286. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  287. results (int): An integer number of results to return
  288. start (int): An integer starting value for the result set
  289. max_familiarity (float): A float specifying the max familiarity of artists to search for
  290. min_familiarity (float): A float specifying the min familiarity of artists to search for
  291. max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  292. min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  293. reverse (bool): A boolean indicating whether or not to return dissimilar artists (wrecommender). Defaults to False.
  294. Returns:
  295. A list of similar Artist objects
  296. Example:
  297. >>> a = artist.Artist('Sleater Kinney')
  298. >>> similars = a.similar[:5]
  299. >>> similars
  300. [<artist - Bikini Kill>, <artist - Pretty Girls Make Graves>, <artist - Huggy Bear>, <artist - Bratmobile>, <artist - Team Dresch>]
  301. >>>
  302. """
  303. buckets = buckets or []
  304. kwargs = {}
  305. if max_familiarity:
  306. kwargs['max_familiarity'] = max_familiarity
  307. if min_familiarity:
  308. kwargs['min_familiarity'] = min_familiarity
  309. if max_hotttnesss:
  310. kwargs['max_hotttnesss'] = max_hotttnesss
  311. if min_hotttnesss:
  312. kwargs['min_hotttnesss'] = min_hotttnesss
  313. if min_results:
  314. kwargs['min_results'] = min_results
  315. if buckets:
  316. kwargs['bucket'] = buckets
  317. if limit:
  318. kwargs['limit'] = 'true'
  319. if reverse:
  320. kwargs['reverse'] = 'true'
  321. if artist_start_year_before:
  322. kwargs['artist_start_year_before'] = artist_start_year_before
  323. if artist_start_year_after:
  324. kwargs['artist_start_year_after'] = artist_start_year_after
  325. if artist_end_year_before:
  326. kwargs['artist_end_year_before'] = artist_end_year_before
  327. if artist_end_year_after:
  328. kwargs['artist_end_year_after'] = artist_end_year_after
  329. if cache and ('similar' in self.cache) and results==15 and start==0 and (not kwargs):
  330. return [Artist(**util.fix(a)) for a in self.cache['similar']]
  331. else:
  332. response = self.get_attribute('similar', results=results, start=start, **kwargs)
  333. if results==15 and start==0 and (not kwargs):
  334. self.cache['similar'] = response['artists']
  335. return [Artist(**util.fix(a)) for a in response['artists']]
  336. similar = property(get_similar)
  337. def get_songs(self, cache=True, results=15, start=0):
  338. """Get the songs associated with an artist
  339. Args:
  340. Kwargs:
  341. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  342. results (int): An integer number of results to return
  343. start (int): An integer starting value for the result set
  344. Results:
  345. A list of Song objects; list contains additional attributes 'start' and 'total'
  346. Example:
  347. >>> a = artist.Artist('Strokes')
  348. >>> a.get_songs(results=5)
  349. [<song - Fear Of Sleep>, <song - Red Light>, <song - Ize Of The World>, <song - Evening Sun>, <song - Juicebox>]
  350. >>>
  351. """
  352. if cache and ('songs' in self.cache) and results==15 and start==0:
  353. if not isinstance(self.cache['songs'][0], Song):
  354. song_objects = []
  355. for s in self.cache["songs"]:
  356. song_objects.append(Song(id=s['id'],
  357. title=s['title'],
  358. artist_name=self.name,
  359. artist_id=self.id))
  360. self.cache['songs'] = song_objects
  361. return self.cache['songs']
  362. else:
  363. response = self.get_attribute('songs', results=results, start=start)
  364. for s in response['songs']:
  365. s.update({'artist_id':self.id, 'artist_name':self.name})
  366. songs = [Song(**util.fix(s)) for s in response['songs']]
  367. if results==15 and start==0:
  368. self.cache['songs'] = ResultList(songs, 0, response['total'])
  369. return ResultList(songs, start, response['total'])
  370. songs = property(get_songs)
  371. def get_terms(self, sort='weight', cache=True):
  372. """Get the terms associated with an artist
  373. Args:
  374. Kwargs:
  375. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  376. sort (str): A string specifying the desired sorting type (weight or frequency)
  377. Results:
  378. A list of term document dicts
  379. Example:
  380. >>> a = artist.Artist('tom petty')
  381. >>> a.terms
  382. [{u'frequency': 1.0, u'name': u'heartland rock', u'weight': 1.0},
  383. {u'frequency': 0.88569401860168606,
  384. u'name': u'jam band',
  385. u'weight': 0.9116501862732439},
  386. {u'frequency': 0.9656145118557401,
  387. u'name': u'pop rock',
  388. u'weight': 0.89777934440040685},
  389. {u'frequency': 0.8414744288140491,
  390. u'name': u'southern rock',
  391. u'weight': 0.8698567153186606},
  392. {u'frequency': 0.9656145118557401,
  393. u'name': u'hard rock',
  394. u'weight': 0.85738022655218893},
  395. {u'frequency': 0.88569401860168606,
  396. u'name': u'singer-songwriter',
  397. u'weight': 0.77427243392312772},
  398. {u'frequency': 0.88569401860168606,
  399. u'name': u'rock',
  400. u'weight': 0.71158718989399083},
  401. {u'frequency': 0.60874110500110956,
  402. u'name': u'album rock',
  403. u'weight': 0.69758668733499629},
  404. {u'frequency': 0.74350792060935744,
  405. u'name': u'psychedelic',
  406. u'weight': 0.68457367494207944},
  407. {u'frequency': 0.77213698386292873,
  408. u'name': u'pop',
  409. u'weight': 0.65039556639337293},
  410. {u'frequency': 0.41747136183050298,
  411. u'name': u'bar band',
  412. u'weight': 0.54974975024767025}]
  413. >>>
  414. """
  415. if cache and ('terms' in self.cache) and sort=='weight':
  416. return self.cache['terms']
  417. else:
  418. response = self.get_attribute('terms', sort=sort)
  419. if sort=='weight':
  420. self.cache['terms'] = response['terms']
  421. return response['terms']
  422. terms = property(get_terms)
  423. def get_urls(self, cache=True):
  424. """Get the urls for an artist
  425. Args:
  426. Kwargs:
  427. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  428. Results:
  429. A url document dict
  430. Example:
  431. >>> a = artist.Artist('the unicorns')
  432. >>> a.get_urls()
  433. {u'amazon_url': u'http://www.amazon.com/gp/search?ie=UTF8&keywords=The Unicorns&tag=httpechonecom-20&index=music',
  434. u'aolmusic_url': u'http://music.aol.com/artist/the-unicorns',
  435. u'itunes_url': u'http://itunes.com/TheUnicorns',
  436. u'lastfm_url': u'http://www.last.fm/music/The+Unicorns',
  437. u'mb_url': u'http://musicbrainz.org/artist/603c5f9f-492a-4f21-9d6f-1642a5dbea2d.html',
  438. u'myspace_url': u'http://www.myspace.com/iwasbornunicorn'}
  439. >>>
  440. """
  441. if not (cache and ('urls' in self.cache)):
  442. response = self.get_attribute('urls')
  443. self.cache['urls'] = response['urls']
  444. return self.cache['urls']
  445. urls = property(get_urls)
  446. def get_video(self, results=15, start=0, cache=True):
  447. """Get a list of video documents found on the web related to an artist
  448. Args:
  449. Kwargs:
  450. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  451. results (int): An integer number of results to return
  452. start (int): An integer starting value for the result set
  453. Returns:
  454. A list of video document dicts; list contains additional attributes 'start' and 'total'
  455. Example:
  456. >>> a = artist.Artist('the vapors')
  457. >>> a.get_video(results=1, start=2)
  458. [{u'date_found': u'2009-12-28T08:27:48',
  459. u'id': u'd02f9e6dc7904f70402d4676516286b9',
  460. u'image_url': u'http://i1.ytimg.com/vi/p6c0wOFL3Us/default.jpg',
  461. u'site': u'youtube',
  462. u'title': u'The Vapors-Turning Japanese (rectangular white vinyl promo)',
  463. u'url': u'http://youtube.com/watch?v=p6c0wOFL3Us'}]
  464. >>>
  465. """
  466. if cache and ('video' in self.cache) and results==15 and start==0:
  467. return self.cache['video']
  468. else:
  469. response = self.get_attribute('video', results=results, start=start)
  470. if results==15 and start==0:
  471. self.cache['video'] = ResultList(response['video'], 0, response['total'])
  472. return ResultList(response['video'], start, response['total'])
  473. video = property(get_video)
  474. def get_years_active(self, cache=True):
  475. """Get a list of years active dictionaries for an artist
  476. Args:
  477. Kwargs:
  478. cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.
  479. Returns:
  480. A list of years active dictionaries; list contains additional attributes 'start' and 'total'
  481. Example:
  482. >>> a = artist.Artist('yelle')
  483. >>> a.get_years_active()
  484. [{ start: 2005 }]
  485. >>>
  486. """
  487. if cache and ('years_active' in self.cache):
  488. return self.cache['years_active']
  489. else:
  490. response = self.get_attribute('profile', bucket=['years_active'])
  491. self.cache['years_active'] = response['artist']['years_active']
  492. return response['artist']['years_active']
  493. years_active = property(get_years_active)
  494. def get_doc_counts(self, cache=True):
  495. """
  496. Get the number of related documents of various types for the artist.
  497. The types include audio, biographies, blogs, images, news, reviews, songs, videos.
  498. Note that these documents can be retrieved by calling artist.<document type>, for example,
  499. artist.biographies.
  500. Args:
  501. Kwargs:
  502. cache (bool): A boolean indicating whether or not the cached value should be used (if available).
  503. Defaults to True.
  504. Returns:
  505. A dictionary with one key for each document type, mapped to an integer count of documents.
  506. Example:
  507. >>> a = artist.Artist("The Kinks")
  508. >>> a.get_doc_counts()
  509. {u'audio': 194,
  510. u'biographies': 9,
  511. u'blogs': 379,
  512. u'images': 177,
  513. u'news': 84,
  514. u'reviews': 110,
  515. u'songs': 499,
  516. u'videos': 340}
  517. >>>
  518. """
  519. if not cache or not ('doc_counts' in self.cache):
  520. response = self.get_attribute("profile", bucket='doc_counts')
  521. self.cache['doc_counts'] = response['artist']['doc_counts']
  522. return self.cache['doc_counts']
  523. doc_counts = property(get_doc_counts)
  524. def search(name=None, description=None, style=None, mood=None, start=0, \
  525. results=15, buckets=None, limit=False, \
  526. fuzzy_match=False, sort=None, max_familiarity=None, min_familiarity=None, \
  527. max_hotttnesss=None, min_hotttnesss=None, test_new_things=None, rank_type=None, \
  528. artist_start_year_after=None, artist_start_year_before=None,artist_end_year_after=None,artist_end_year_before=None):
  529. """Search for artists by name, description, or constraint.
  530. Args:
  531. Kwargs:
  532. name (str): the name of an artist
  533. description (str): A string describing the artist
  534. style (str): A string describing the style/genre of the artist
  535. mood (str): A string describing the mood of the artist
  536. start (int): An integer starting value for the result set
  537. results (int): An integer number of results to return
  538. buckets (list): A list of strings specifying which buckets to retrieve
  539. limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
  540. fuzzy_match (bool): A boolean indicating whether or not to search for similar sounding matches (only works with name)
  541. max_familiarity (float): A float specifying the max familiarity of artists to search for
  542. min_familiarity (float): A float specifying the min familiarity of artists to search for
  543. max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  544. min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  545. artist_start_year_before (int): Returned artists will have started recording music before this year.
  546. artist_start_year_after (int): Returned artists will have started recording music after this year.
  547. artist_end_year_before (int): Returned artists will have stopped recording music before this year.
  548. artist_end_year_after (int): Returned artists will have stopped recording music after this year.
  549. rank_type (str): A string denoting the desired ranking for description searches, either 'relevance' or 'familiarity'
  550. Returns:
  551. A list of Artist objects
  552. Example:
  553. >>> results = artist.search(name='t-pain')
  554. >>> results
  555. [<artist - T-Pain>, <artist - T-Pain & Lil Wayne>, <artist - T Pain & 2 Pistols>, <artist - Roscoe Dash & T-Pain>, <artist - Tony Moxberg & T-Pain>, <artist - Flo-Rida (feat. T-Pain)>, <artist - Shortyo/Too Short/T-Pain>]
  556. >>>
  557. """
  558. limit = str(limit).lower()
  559. fuzzy_match = str(fuzzy_match).lower()
  560. kwargs = locals()
  561. kwargs['bucket'] = buckets or []
  562. del kwargs['buckets']
  563. """Search for artists"""
  564. result = util.callm("%s/%s" % ('artist', 'search'), kwargs)
  565. return [Artist(**util.fix(a_dict)) for a_dict in result['response']['artists']]
  566. def top_hottt(start=0, results=15, buckets = None, limit=False):
  567. """Get the top hotttest artists, according to The Echo Nest
  568. Args:
  569. Kwargs:
  570. results (int): An integer number of results to return
  571. start (int): An integer starting value for the result set
  572. buckets (list): A list of strings specifying which buckets to retrieve
  573. limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
  574. Returns:
  575. A list of hottt Artist objects
  576. Example:
  577. >>> hot_stuff = artist.top_hottt()
  578. >>> hot_stuff
  579. [<artist - Deerhunter>, <artist - Sufjan Stevens>, <artist - Belle and Sebastian>, <artist - Glee Cast>, <artist - Linkin Park>, <artist - Neil Young>, <artist - Jimmy Eat World>, <artist - Kanye West>, <artist - Katy Perry>, <artist - Bruno Mars>, <artist - Lady Gaga>, <artist - Rihanna>, <artist - Lil Wayne>, <artist - Jason Mraz>, <artist - Green Day>]
  580. >>>
  581. """
  582. buckets = buckets or []
  583. kwargs = {}
  584. if start:
  585. kwargs['start'] = start
  586. if results:
  587. kwargs['results'] = results
  588. if buckets:
  589. kwargs['bucket'] = buckets
  590. if limit:
  591. kwargs['limit'] = 'true'
  592. """Get top hottt artists"""
  593. result = util.callm("%s/%s" % ('artist', 'top_hottt'), kwargs)
  594. return [Artist(**util.fix(a_dict)) for a_dict in result['response']['artists']]
  595. def top_terms(results=15):
  596. """Get a list of the top overall terms
  597. Args:
  598. Kwargs:
  599. results (int): An integer number of results to return
  600. Returns:
  601. A list of term document dicts
  602. Example:
  603. >>> terms = artist.top_terms(results=5)
  604. >>> terms
  605. [{u'frequency': 1.0, u'name': u'rock'},
  606. {u'frequency': 0.99054710039307992, u'name': u'electronic'},
  607. {u'frequency': 0.96131624654034398, u'name': u'hip hop'},
  608. {u'frequency': 0.94358477322411127, u'name': u'jazz'},
  609. {u'frequency': 0.94023302416455468, u'name': u'pop rock'}]
  610. >>>
  611. """
  612. kwargs = {}
  613. if results:
  614. kwargs['results'] = results
  615. """Get top terms"""
  616. result = util.callm("%s/%s" % ('artist', 'top_terms'), kwargs)
  617. return result['response']['terms']
  618. def list_terms(type):
  619. """Get a list of best terms to use with search
  620. Args:
  621. Kwargs:
  622. type (str): the type of term to return, either 'mood' or 'style'
  623. Example:
  624. >>> best_terms = artist.list_terms('mood')
  625. >>> best_terms
  626. [{u'name': u'aggressive'},
  627. {u'name': u'ambient'},
  628. {u'name': u'angry'},
  629. {u'name': u'angst-ridden'},
  630. {u'name': u'bouncy'},
  631. {u'name': u'calming'},
  632. {u'name': u'carefree'}, etc.]
  633. """
  634. kwargs = {'type': type}
  635. result = util.callm("%s/%s" % ('artist', 'list_terms'), kwargs)
  636. return result['response']['terms']
  637. def similar(names=None, ids=None, start=0, results=15, buckets=None, limit=False, max_familiarity=None, min_familiarity=None,
  638. max_hotttnesss=None, min_hotttnesss=None, seed_catalog=None,artist_start_year_before=None, \
  639. artist_start_year_after=None,artist_end_year_before=None,artist_end_year_after=None):
  640. """Return similar artists to this one
  641. Args:
  642. Kwargs:
  643. ids (str/list): An artist id or list of ids
  644. names (str/list): An artist name or list of names
  645. results (int): An integer number of results to return
  646. buckets (list): A list of strings specifying which buckets to retrieve
  647. limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
  648. start (int): An integer starting value for the result set
  649. max_familiarity (float): A float specifying the max familiarity of artists to search for
  650. min_familiarity (float): A float specifying the min familiarity of artists to search for
  651. max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  652. min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  653. seed_catalog (str): A string specifying the catalog similar artists are restricted to
  654. Returns:
  655. A list of similar Artist objects
  656. Example:
  657. >>> some_dudes = [artist.Artist('weezer'), artist.Artist('radiohead')]
  658. >>> some_dudes
  659. [<artist - Weezer>, <artist - Radiohead>]
  660. >>> sims = artist.similar(ids=[art.id for art in some_dudes], results=5)
  661. >>> sims
  662. [<artist - The Smashing Pumpkins>, <artist - Biffy Clyro>, <artist - Death Cab for Cutie>, <artist - Jimmy Eat World>, <artist - Nerf Herder>]
  663. >>>
  664. """
  665. buckets = buckets or []
  666. kwargs = {}
  667. if ids:
  668. if not isinstance(ids, list):
  669. ids = [ids]
  670. kwargs['id'] = ids
  671. if names:
  672. if not isinstance(names, list):
  673. names = [names]
  674. kwargs['name'] = names
  675. if max_familiarity is not None:
  676. kwargs['max_familiarity'] = max_familiarity
  677. if min_familiarity is not None:
  678. kwargs['min_familiarity'] = min_familiarity
  679. if max_hotttnesss is not None:
  680. kwargs['max_hotttnesss'] = max_hotttnesss
  681. if min_hotttnesss is not None:
  682. kwargs['min_hotttnesss'] = min_hotttnesss
  683. if seed_catalog is not None:
  684. kwargs['seed_catalog'] = seed_catalog
  685. if start:
  686. kwargs['start'] = start
  687. if results:
  688. kwargs['results'] = results
  689. if buckets:
  690. kwargs['bucket'] = buckets
  691. if limit:
  692. kwargs['limit'] = 'true'
  693. if artist_start_year_before:
  694. kwargs['artist_start_year_before'] = artist_start_year_before
  695. if artist_start_year_after:
  696. kwargs['artist_start_year_after'] = artist_start_year_after
  697. if artist_end_year_before:
  698. kwargs['artist_end_year_before'] = artist_end_year_before
  699. if artist_end_year_after:
  700. kwargs['artist_end_year_after'] = artist_end_year_after
  701. result = util.callm("%s/%s" % ('artist', 'similar'), kwargs)
  702. return [Artist(**util.fix(a_dict)) for a_dict in result['response']['artists']]
  703. def extract(text='', start=0, results=15, buckets=None, limit=False, max_familiarity=None, min_familiarity=None,
  704. max_hotttnesss=None, min_hotttnesss=None):
  705. """Extract artist names from a block of text.
  706. Args:
  707. Kwargs:
  708. text (str): The text to extract artists from
  709. start (int): An integer starting value for the result set
  710. results (int): An integer number of results to return
  711. buckets (list): A list of strings specifying which buckets to retrieve
  712. limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
  713. max_familiarity (float): A float specifying the max familiarity of artists to search for
  714. min_familiarity (float): A float specifying the min familiarity of artists to search for
  715. max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  716. min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
  717. Returns:
  718. A list of Artist objects
  719. Example:
  720. >>> results = artist.extract(text='i saw beyonce at burger king, she was eatin, she was eatin')
  721. >>> results
  722. >>>
  723. """
  724. buckets = buckets or []
  725. kwargs = {}
  726. kwargs['text'] = text
  727. if max_familiarity is not None:
  728. kwargs['max_familiarity'] = max_familiarity
  729. if min_familiarity is not None:
  730. kwargs['min_familiarity'] = min_familiarity
  731. if max_hotttnesss is not None:
  732. kwargs['max_hotttnesss'] = max_hotttnesss
  733. if min_hotttnesss is not None:
  734. kwargs['min_hotttnesss'] = min_hotttnesss
  735. if start:
  736. kwargs['start'] = start
  737. if results:
  738. kwargs['results'] = results
  739. if buckets:
  740. kwargs['bucket'] = buckets
  741. if limit:
  742. kwargs['limit'] = 'true'
  743. result = util.callm("%s/%s" % ('artist', 'extract'), kwargs)
  744. return [Artist(**util.fix(a_dict)) for a_dict in result['response']['artists']]