logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

streetvoice.py (3624B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. parse_iso8601,
  7. str_or_none,
  8. strip_or_none,
  9. try_get,
  10. urljoin,
  11. )
  12. class StreetVoiceIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:.+?\.)?streetvoice\.com/[^/]+/songs/(?P<id>[0-9]+)'
  14. _TESTS = [{
  15. 'url': 'https://streetvoice.com/skippylu/songs/123688/',
  16. 'md5': '0eb535970629a5195685355f3ed60bfd',
  17. 'info_dict': {
  18. 'id': '123688',
  19. 'ext': 'mp3',
  20. 'title': '流浪',
  21. 'description': 'md5:8eb0bfcc9dcd8aa82bd6efca66e3fea6',
  22. 'thumbnail': r're:^https?://.*\.jpg',
  23. 'duration': 270,
  24. 'upload_date': '20100923',
  25. 'uploader': 'Crispy脆樂團',
  26. 'uploader_id': '627810',
  27. 'uploader_url': 're:^https?://streetvoice.com/skippylu/',
  28. 'timestamp': 1285261661,
  29. 'view_count': int,
  30. 'like_count': int,
  31. 'comment_count': int,
  32. 'repost_count': int,
  33. 'track': '流浪',
  34. 'track_id': '123688',
  35. 'album': '2010',
  36. }
  37. }, {
  38. 'url': 'http://tw.streetvoice.com/skippylu/songs/94440/',
  39. 'only_matching': True,
  40. }]
  41. def _real_extract(self, url):
  42. song_id = self._match_id(url)
  43. base_url = 'https://streetvoice.com/api/v4/song/%s/' % song_id
  44. song = self._download_json(base_url, song_id, query={
  45. 'fields': 'album,comments_count,created_at,id,image,length,likes_count,name,nickname,plays_count,profile,share_count,synopsis,user,username',
  46. })
  47. title = song['name']
  48. formats = []
  49. for suffix, format_id in [('hls/file', 'hls'), ('file', 'http'), ('file/original', 'original')]:
  50. f_url = (self._download_json(
  51. base_url + suffix + '/', song_id,
  52. 'Downloading %s format URL' % format_id,
  53. data=b'', fatal=False) or {}).get('file')
  54. if not f_url:
  55. continue
  56. f = {
  57. 'ext': 'mp3',
  58. 'format_id': format_id,
  59. 'url': f_url,
  60. 'vcodec': 'none',
  61. }
  62. if format_id == 'hls':
  63. f['protocol'] = 'm3u8_native'
  64. abr = self._search_regex(r'\.mp3\.(\d+)k', f_url, 'bitrate', default=None)
  65. if abr:
  66. abr = int(abr)
  67. f.update({
  68. 'abr': abr,
  69. 'tbr': abr,
  70. })
  71. formats.append(f)
  72. user = song.get('user') or {}
  73. username = user.get('username')
  74. get_count = lambda x: int_or_none(song.get(x + '_count'))
  75. return {
  76. 'id': song_id,
  77. 'formats': formats,
  78. 'title': title,
  79. 'description': strip_or_none(song.get('synopsis')),
  80. 'thumbnail': song.get('image'),
  81. 'duration': int_or_none(song.get('length')),
  82. 'timestamp': parse_iso8601(song.get('created_at')),
  83. 'uploader': try_get(user, lambda x: x['profile']['nickname']),
  84. 'uploader_id': str_or_none(user.get('id')),
  85. 'uploader_url': urljoin(url, '/%s/' % username) if username else None,
  86. 'view_count': get_count('plays'),
  87. 'like_count': get_count('likes'),
  88. 'comment_count': get_count('comments'),
  89. 'repost_count': get_count('share'),
  90. 'track': title,
  91. 'track_id': song_id,
  92. 'album': try_get(song, lambda x: x['album']['name']),
  93. }