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.

105 lines
3.6 KiB

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