pixivsketch.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. traverse_obj,
  5. unified_timestamp,
  6. )
  7. class PixivSketchBaseIE(InfoExtractor):
  8. def _call_api(self, video_id, path, referer, note='Downloading JSON metadata'):
  9. response = self._download_json(f'https://sketch.pixiv.net/api/{path}', video_id, note=note, headers={
  10. 'Referer': referer,
  11. 'X-Requested-With': referer,
  12. })
  13. errors = traverse_obj(response, ('errors', ..., 'message'))
  14. if errors:
  15. raise ExtractorError(' '.join(f'{e}.' for e in errors))
  16. return response.get('data') or {}
  17. class PixivSketchIE(PixivSketchBaseIE):
  18. IE_NAME = 'pixiv:sketch'
  19. _VALID_URL = r'https?://sketch\.pixiv\.net/@(?P<uploader_id>[a-zA-Z0-9_-]+)/lives/(?P<id>\d+)/?'
  20. _TESTS = [{
  21. 'url': 'https://sketch.pixiv.net/@nuhutya/lives/3654620468641830507',
  22. 'info_dict': {
  23. 'id': '7370666691623196569',
  24. 'title': 'まにあえクリスマス!',
  25. 'uploader': 'ぬふちゃ',
  26. 'uploader_id': 'nuhutya',
  27. 'channel_id': '9844815',
  28. 'age_limit': 0,
  29. 'timestamp': 1640351536,
  30. },
  31. 'skip': True,
  32. }, {
  33. # these two (age_limit > 0) requires you to login on website, but it's actually not required for download
  34. 'url': 'https://sketch.pixiv.net/@namahyou/lives/4393103321546851377',
  35. 'info_dict': {
  36. 'id': '4907995960957946943',
  37. 'title': 'クリスマスなんて知らん🖕',
  38. 'uploader': 'すゃもり',
  39. 'uploader_id': 'suya2mori2',
  40. 'channel_id': '31169300',
  41. 'age_limit': 15,
  42. 'timestamp': 1640347640,
  43. },
  44. 'skip': True,
  45. }, {
  46. 'url': 'https://sketch.pixiv.net/@8aki/lives/3553803162487249670',
  47. 'info_dict': {
  48. 'id': '1593420639479156945',
  49. 'title': 'おまけ本作業(リョナ有)',
  50. 'uploader': 'おぶい / Obui',
  51. 'uploader_id': 'oving',
  52. 'channel_id': '17606',
  53. 'age_limit': 18,
  54. 'timestamp': 1640330263,
  55. },
  56. 'skip': True,
  57. }]
  58. def _real_extract(self, url):
  59. video_id, uploader_id = self._match_valid_url(url).group('id', 'uploader_id')
  60. data = self._call_api(video_id, f'lives/{video_id}.json', url)
  61. if not traverse_obj(data, 'is_broadcasting'):
  62. raise ExtractorError(f'This live is offline. Use https://sketch.pixiv.net/@{uploader_id} for ongoing live.', expected=True)
  63. m3u8_url = traverse_obj(data, ('owner', 'hls_movie', 'url'))
  64. formats = self._extract_m3u8_formats(
  65. m3u8_url, video_id, ext='mp4',
  66. entry_protocol='m3u8_native', m3u8_id='hls')
  67. return {
  68. 'id': video_id,
  69. 'title': data.get('name'),
  70. 'formats': formats,
  71. 'uploader': traverse_obj(data, ('user', 'name'), ('owner', 'user', 'name')),
  72. 'uploader_id': traverse_obj(data, ('user', 'unique_name'), ('owner', 'user', 'unique_name')),
  73. 'channel_id': str(traverse_obj(data, ('user', 'pixiv_user_id'), ('owner', 'user', 'pixiv_user_id'))),
  74. 'age_limit': 18 if data.get('is_r18') else 15 if data.get('is_r15') else 0,
  75. 'timestamp': unified_timestamp(data.get('created_at')),
  76. 'is_live': True,
  77. }
  78. class PixivSketchUserIE(PixivSketchBaseIE):
  79. IE_NAME = 'pixiv:sketch:user'
  80. _VALID_URL = r'https?://sketch\.pixiv\.net/@(?P<id>[a-zA-Z0-9_-]+)/?'
  81. _TESTS = [{
  82. 'url': 'https://sketch.pixiv.net/@nuhutya',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'https://sketch.pixiv.net/@namahyou',
  86. 'only_matching': True,
  87. }, {
  88. 'url': 'https://sketch.pixiv.net/@8aki',
  89. 'only_matching': True,
  90. }]
  91. @classmethod
  92. def suitable(cls, url):
  93. return super().suitable(url) and not PixivSketchIE.suitable(url)
  94. def _real_extract(self, url):
  95. user_id = self._match_id(url)
  96. data = self._call_api(user_id, f'lives/users/@{user_id}.json', url)
  97. if not traverse_obj(data, 'is_broadcasting'):
  98. try:
  99. self._call_api(user_id, 'users/current.json', url, 'Investigating reason for request failure')
  100. except ExtractorError as ex:
  101. if ex.cause and ex.cause.code == 401:
  102. self.raise_login_required(f'Please log in, or use direct link like https://sketch.pixiv.net/@{user_id}/1234567890', method='cookies')
  103. raise ExtractorError('This user is offline', expected=True)
  104. return self.url_result(f'https://sketch.pixiv.net/@{user_id}/lives/{data["id"]}')