streetvoice.py 3.5 KB

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