You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_request,
  7. )
  8. from ..utils import (
  9. clean_html,
  10. ExtractorError,
  11. float_or_none,
  12. parse_iso8601,
  13. )
  14. class TapelyIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?tape\.ly/(?P<id>[A-Za-z0-9\-_]+)(?:/(?P<songnr>\d+))?'
  16. _API_URL = 'http://tape.ly/showtape?id={0:}'
  17. _S3_SONG_URL = 'http://mytape.s3.amazonaws.com/{0:}'
  18. _SOUNDCLOUD_SONG_URL = 'http://api.soundcloud.com{0:}'
  19. _TESTS = [
  20. {
  21. 'url': 'http://tape.ly/my-grief-as-told-by-water',
  22. 'info_dict': {
  23. 'id': 23952,
  24. 'title': 'my grief as told by water',
  25. 'thumbnail': 're:^https?://.*\.png$',
  26. 'uploader_id': 16484,
  27. 'timestamp': 1411848286,
  28. 'description': 'For Robin and Ponkers, whom the tides of life have taken out to sea.',
  29. },
  30. 'playlist_count': 13,
  31. },
  32. {
  33. 'url': 'http://tape.ly/my-grief-as-told-by-water/1',
  34. 'md5': '79031f459fdec6530663b854cbc5715c',
  35. 'info_dict': {
  36. 'id': 258464,
  37. 'title': 'Dreaming Awake (My Brightest Diamond)',
  38. 'ext': 'm4a',
  39. },
  40. },
  41. ]
  42. def _real_extract(self, url):
  43. mobj = re.match(self._VALID_URL, url)
  44. display_id = mobj.group('id')
  45. playlist_url = self._API_URL.format(display_id)
  46. request = compat_urllib_request.Request(playlist_url)
  47. request.add_header('X-Requested-With', 'XMLHttpRequest')
  48. request.add_header('Accept', 'application/json')
  49. request.add_header('Referer', url)
  50. playlist = self._download_json(request, display_id)
  51. tape = playlist['tape']
  52. entries = []
  53. for s in tape['songs']:
  54. song = s['song']
  55. entry = {
  56. 'id': song['id'],
  57. 'duration': float_or_none(song.get('songduration'), 1000),
  58. 'title': song['title'],
  59. }
  60. if song['source'] == 'S3':
  61. entry.update({
  62. 'url': self._S3_SONG_URL.format(song['filename']),
  63. })
  64. entries.append(entry)
  65. elif song['source'] == 'YT':
  66. self.to_screen('YouTube video detected')
  67. yt_id = song['filename'].replace('/youtube/', '')
  68. entry.update(self.url_result(yt_id, 'Youtube', video_id=yt_id))
  69. entries.append(entry)
  70. elif song['source'] == 'SC':
  71. self.to_screen('SoundCloud song detected')
  72. sc_url = self._SOUNDCLOUD_SONG_URL.format(song['filename'])
  73. entry.update(self.url_result(sc_url, 'Soundcloud'))
  74. entries.append(entry)
  75. else:
  76. self.report_warning('Unknown song source: %s' % song['source'])
  77. if mobj.group('songnr'):
  78. songnr = int(mobj.group('songnr')) - 1
  79. try:
  80. return entries[songnr]
  81. except IndexError:
  82. raise ExtractorError(
  83. 'No song with index: %s' % mobj.group('songnr'),
  84. expected=True)
  85. return {
  86. '_type': 'playlist',
  87. 'id': tape['id'],
  88. 'display_id': display_id,
  89. 'title': tape['name'],
  90. 'entries': entries,
  91. 'thumbnail': tape.get('image_url'),
  92. 'description': clean_html(tape.get('subtext')),
  93. 'like_count': tape.get('likescount'),
  94. 'uploader_id': tape.get('user_id'),
  95. 'timestamp': parse_iso8601(tape.get('published_at')),
  96. }