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.

111 lines
3.8 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|tapely\.com)/(?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. 'url': 'https://tapely.com/my-grief-as-told-by-water',
  43. 'only_matching': True,
  44. },
  45. ]
  46. def _real_extract(self, url):
  47. mobj = re.match(self._VALID_URL, url)
  48. display_id = mobj.group('id')
  49. playlist_url = self._API_URL.format(display_id)
  50. request = compat_urllib_request.Request(playlist_url)
  51. request.add_header('X-Requested-With', 'XMLHttpRequest')
  52. request.add_header('Accept', 'application/json')
  53. request.add_header('Referer', url)
  54. playlist = self._download_json(request, display_id)
  55. tape = playlist['tape']
  56. entries = []
  57. for s in tape['songs']:
  58. song = s['song']
  59. entry = {
  60. 'id': song['id'],
  61. 'duration': float_or_none(song.get('songduration'), 1000),
  62. 'title': song['title'],
  63. }
  64. if song['source'] == 'S3':
  65. entry.update({
  66. 'url': self._S3_SONG_URL.format(song['filename']),
  67. })
  68. entries.append(entry)
  69. elif song['source'] == 'YT':
  70. self.to_screen('YouTube video detected')
  71. yt_id = song['filename'].replace('/youtube/', '')
  72. entry.update(self.url_result(yt_id, 'Youtube', video_id=yt_id))
  73. entries.append(entry)
  74. elif song['source'] == 'SC':
  75. self.to_screen('SoundCloud song detected')
  76. sc_url = self._SOUNDCLOUD_SONG_URL.format(song['filename'])
  77. entry.update(self.url_result(sc_url, 'Soundcloud'))
  78. entries.append(entry)
  79. else:
  80. self.report_warning('Unknown song source: %s' % song['source'])
  81. if mobj.group('songnr'):
  82. songnr = int(mobj.group('songnr')) - 1
  83. try:
  84. return entries[songnr]
  85. except IndexError:
  86. raise ExtractorError(
  87. 'No song with index: %s' % mobj.group('songnr'),
  88. expected=True)
  89. return {
  90. '_type': 'playlist',
  91. 'id': tape['id'],
  92. 'display_id': display_id,
  93. 'title': tape['name'],
  94. 'entries': entries,
  95. 'thumbnail': tape.get('image_url'),
  96. 'description': clean_html(tape.get('subtext')),
  97. 'like_count': tape.get('likescount'),
  98. 'uploader_id': tape.get('user_id'),
  99. 'timestamp': parse_iso8601(tape.get('published_at')),
  100. }