]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/mixch.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / mixch.py
index 7eedbc7520096603b62aafbd7d00d1084020245a..9b7c7b89b958372737cf617700185fc51b7b6a7f 100644 (file)
@@ -1,8 +1,13 @@
 from .common import InfoExtractor
+from ..networking.exceptions import HTTPError
 from ..utils import (
     ExtractorError,
-    traverse_obj,
+    UserNotLive,
+    int_or_none,
+    str_or_none,
+    url_or_none,
 )
+from ..utils.traversal import traverse_obj
 
 
 class MixchIE(InfoExtractor):
@@ -10,17 +15,20 @@ class MixchIE(InfoExtractor):
     _VALID_URL = r'https?://(?:www\.)?mixch\.tv/u/(?P<id>\d+)'
 
     _TESTS = [{
-        'url': 'https://mixch.tv/u/16236849/live',
+        'url': 'https://mixch.tv/u/16943797/live',
         'skip': 'don\'t know if this live persists',
         'info_dict': {
-            'id': '16236849',
-            'title': '24配信シェア⭕️投票🙏💦',
-            'comment_count': 13145,
-            'view_count': 28348,
-            'timestamp': 1636189377,
-            'uploader': '🦥伊咲👶🏻#フレアワ',
-            'uploader_id': '16236849',
-        }
+            'id': '16943797',
+            'ext': 'mp4',
+            'title': '#EntView #カリナ #セブチ 2024-05-05 06:58',
+            'comment_count': int,
+            'view_count': int,
+            'timestamp': 1714726805,
+            'uploader': 'Ent.View K-news🎶💕',
+            'uploader_id': '16943797',
+            'live_status': 'is_live',
+            'upload_date': '20240503',
+        },
     }, {
         'url': 'https://mixch.tv/u/16137876/live',
         'only_matching': True,
@@ -28,32 +36,41 @@ class MixchIE(InfoExtractor):
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
-        webpage = self._download_webpage(f'https://mixch.tv/u/{video_id}/live', video_id)
-
-        initial_js_state = self._parse_json(self._search_regex(
-            r'(?m)^\s*window\.__INITIAL_JS_STATE__\s*=\s*(\{.+?\});\s*$', webpage, 'initial JS state'), video_id)
-
-        is_live = initial_js_state.get('liveInfo')
-        if not is_live:
-            self.raise_no_formats('Livestream has ended or has not started', expected=True)
+        data = self._download_json(f'https://mixch.tv/api-web/users/{video_id}/live', video_id)
+        if not traverse_obj(data, ('liveInfo', {dict})):
+            raise UserNotLive(video_id=video_id)
 
         return {
             'id': video_id,
-            'title': traverse_obj(initial_js_state, ('liveInfo', 'title')),
-            'comment_count': traverse_obj(initial_js_state, ('liveInfo', 'comments')),
-            'view_count': traverse_obj(initial_js_state, ('liveInfo', 'visitor')),
-            'timestamp': traverse_obj(initial_js_state, ('liveInfo', 'created')),
-            'uploader': traverse_obj(initial_js_state, ('broadcasterInfo', 'name')),
             'uploader_id': video_id,
+            **traverse_obj(data, {
+                'title': ('liveInfo', 'title', {str}),
+                'comment_count': ('liveInfo', 'comments', {int_or_none}),
+                'view_count': ('liveInfo', 'visitor', {int_or_none}),
+                'timestamp': ('liveInfo', 'created', {int_or_none}),
+                'uploader': ('broadcasterInfo', 'name', {str}),
+            }),
             'formats': [{
                 'format_id': 'hls',
-                'url': traverse_obj(initial_js_state, ('liveInfo', 'hls')) or 'https://d1hd0ww6piyb43.cloudfront.net/hls/torte_%s.m3u8' % video_id,
+                'url': data['liveInfo']['hls'],
                 'ext': 'mp4',
                 'protocol': 'm3u8',
-            }] if is_live else [],
-            'live_status': 'is_live' if is_live else 'is_upcoming',
+            }],
+            'is_live': True,
+            '__post_extractor': self.extract_comments(video_id),
         }
 
+    def _get_comments(self, video_id):
+        yield from traverse_obj(self._download_json(
+            f'https://mixch.tv/api-web/lives/{video_id}/messages', video_id,
+            note='Downloading comments', errnote='Failed to download comments'), (..., {
+                'author': ('name', {str}),
+                'author_id': ('user_id', {str_or_none}),
+                'id': ('message_id', {str}, {lambda x: x or None}),
+                'text': ('body', {str}),
+                'timestamp': ('created', {int}),
+            }))
+
 
 class MixchArchiveIE(InfoExtractor):
     IE_NAME = 'mixch:archive'
@@ -64,22 +81,38 @@ class MixchArchiveIE(InfoExtractor):
         'skip': 'paid video, no DRM. expires at Jan 23',
         'info_dict': {
             'id': '421',
+            'ext': 'mp4',
             'title': '96NEKO SHOW TIME',
-        }
+        },
+    }, {
+        'url': 'https://mixch.tv/archive/1213',
+        'skip': 'paid video, no DRM. expires at Dec 31, 2023',
+        'info_dict': {
+            'id': '1213',
+            'ext': 'mp4',
+            'title': '【特別トーク番組アーカイブス】Merm4id×燐舞曲 2nd LIVE「VERSUS」',
+            'release_date': '20231201',
+            'thumbnail': str,
+        },
+    }, {
+        'url': 'https://mixch.tv/archive/1214',
+        'only_matching': True,
     }]
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
-        webpage = self._download_webpage(url, video_id)
 
-        html5_videos = self._parse_html5_media_entries(
-            url, webpage.replace('video-js', 'video'), video_id, 'hls')
-        if not html5_videos:
-            self.raise_login_required(method='cookies')
-        infodict = html5_videos[0]
-        infodict.update({
-            'id': video_id,
-            'title': self._html_search_regex(r'class="archive-title">(.+?)</', webpage, 'title')
-        })
+        try:
+            info_json = self._download_json(
+                f'https://mixch.tv/api-web/archive/{video_id}', video_id)['archive']
+        except ExtractorError as e:
+            if isinstance(e.cause, HTTPError) and e.cause.status == 401:
+                self.raise_login_required()
+            raise
 
-        return infodict
+        return {
+            'id': video_id,
+            'title': traverse_obj(info_json, ('title', {str})),
+            'formats': self._extract_m3u8_formats(info_json['archiveURL'], video_id),
+            'thumbnail': traverse_obj(info_json, ('thumbnailURL', {url_or_none})),
+        }