twentytwotracks.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. # 22Tracks regularly replace the audio tracks that can be streamed on their
  5. # site. The tracks usually expire after 1 months, so we can't add tests.
  6. class TwentyTwoTracksIE(InfoExtractor):
  7. _VALID_URL = r'http://22tracks\.com/([a-z]+)/([a-z]+[2]*)/(\d+)'
  8. IE_NAME = 'TwentyTwoTracks:Tracks'
  9. def _extract_info(self, city, genre, track=''):
  10. self._base_url = "http://22tracks.com/api/"
  11. if track == '':
  12. itemid = genre
  13. else:
  14. itemid = track
  15. cities = self._download_json(
  16. self._base_url + 'cities', itemid,
  17. 'Downloading city info', 'Cannot download city info')
  18. city_id = [x['id'] for x in cities if x['slug'] == city]
  19. genres = self._download_json(
  20. self._base_url + 'genres/' + str(city_id[0]), itemid,
  21. 'Downloading genre info', 'Cannot download genre info')
  22. genre_id = [x['id'] for x in genres if x['slug'] == genre]
  23. tracks = self._download_json(
  24. self._base_url + 'tracks/' + str(genre_id[0]),
  25. itemid, 'Downloading track info', 'Cannot download track info')
  26. if track == '':
  27. return [[x['title'] for x in genres if x['slug'] == genre][0],
  28. tracks]
  29. else:
  30. return [x for x in tracks if x['id'] == itemid][0]
  31. def _get_token(self, filename, track_id):
  32. token = self._download_json(
  33. 'http://22tracks.com/token.php?desktop=true&u=%2F128%2f{0}'.format(
  34. filename), track_id, 'Finding download link...')
  35. down_url = 'http://audio.22tracks.com{0}?st={1}&e={2}'.format(
  36. token['filename'],
  37. token['st'],
  38. token['e'])
  39. return down_url
  40. def _real_extract(self, url):
  41. mobj = re.match(self._VALID_URL, url)
  42. city_id = mobj.group(1)
  43. genre_id = mobj.group(2)
  44. track_id = mobj.group(3)
  45. self.to_screen(':: Track ID found! - Downloading single track')
  46. track_info = self._extract_info(city_id, genre_id, track_id)
  47. download_url = self._get_token(track_info['filename'], track_id)
  48. title = '{0}-{1}'.format(
  49. track_info['artist'].strip(), track_info['title'].strip())
  50. return {
  51. 'id': track_id,
  52. 'url': download_url,
  53. 'ext': 'mp3',
  54. 'title': title,
  55. 'duration': track_info['duration']
  56. }
  57. class TwentyTwoTracksGenreIE(TwentyTwoTracksIE):
  58. _VALID_URL = r'http://22tracks\.com/([a-z]+)/([a-z]+[2]*)/?'
  59. IE_NAME = 'TwentyTwoTracks:Genre'
  60. def _real_extract(self, url):
  61. mobj = re.match(self._VALID_URL, url)
  62. city_id = mobj.group(1)
  63. genre_id = mobj.group(2)
  64. self.to_screen(':: Track ID not found! - Downloading entire genre')
  65. playlist_info = self._extract_info(city_id, genre_id)
  66. entries = []
  67. for track in playlist_info[1]:
  68. title = '{0}-{1}'.format(
  69. track['artist'].strip(), track['title'].strip())
  70. entries.append({
  71. 'id': track['id'],
  72. 'url': self._get_token(track['filename'], track['id']),
  73. 'ext': 'mp3',
  74. 'title': title
  75. })
  76. self.to_screen(':: Links found - Downloading Playlist')
  77. return {
  78. '_type': 'playlist',
  79. 'id': genre_id,
  80. 'title': playlist_info[0],
  81. 'entries': entries
  82. }