]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/rutube.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / rutube.py
index 2f753b41f2f0acf81bcec0c1d862ccd940126d74..eb12f32faf9bc33e9af7b4db30c898a290d7c0db 100644 (file)
@@ -1,7 +1,3 @@
-# coding: utf-8
-from __future__ import unicode_literals
-
-import re
 import itertools
 
 from .common import InfoExtractor
@@ -9,8 +5,8 @@
     compat_str,
 )
 from ..utils import (
-    determine_ext,
     bool_or_none,
+    determine_ext,
     int_or_none,
     parse_qs,
     try_get,
@@ -29,8 +25,7 @@ def _download_api_info(self, video_id, query=None):
             video_id, 'Downloading video JSON',
             'Unable to download video JSON', query=query)
 
-    @staticmethod
-    def _extract_info(video, video_id=None, require_title=True):
+    def _extract_info(self, video, video_id=None, require_title=True):
         title = video['title'] if require_title else video.get('title')
 
         age_limit = video.get('is_adult')
@@ -39,21 +34,24 @@ def _extract_info(video, video_id=None, require_title=True):
 
         uploader_id = try_get(video, lambda x: x['author']['id'])
         category = try_get(video, lambda x: x['category']['name'])
+        description = video.get('description')
+        duration = int_or_none(video.get('duration'))
 
         return {
             'id': video.get('id') or video_id if video_id else video['id'],
             'title': title,
-            'description': video.get('description'),
+            'description': description,
             'thumbnail': video.get('thumbnail_url'),
-            'duration': int_or_none(video.get('duration')),
+            'duration': duration,
             'uploader': try_get(video, lambda x: x['author']['name']),
             'uploader_id': compat_str(uploader_id) if uploader_id else None,
             'timestamp': unified_timestamp(video.get('created_ts')),
-            'category': [category] if category else None,
+            'categories': [category] if category else None,
             'age_limit': age_limit,
             'view_count': int_or_none(video.get('hits')),
             'comment_count': int_or_none(video.get('comments_count')),
             'is_live': bool_or_none(video.get('is_livestream')),
+            'chapters': self._extract_chapters_from_description(description, duration),
         }
 
     def _download_and_extract_info(self, video_id, query=None):
@@ -85,7 +83,6 @@ def _extract_formats(self, options, video_id):
                     'url': format_url,
                     'format_id': format_id,
                 })
-        self._sort_formats(formats)
         return formats
 
     def _download_and_extract_formats(self, video_id, query=None):
@@ -96,11 +93,12 @@ def _download_and_extract_formats(self, video_id, query=None):
 class RutubeIE(RutubeBaseIE):
     IE_NAME = 'rutube'
     IE_DESC = 'Rutube videos'
-    _VALID_URL = r'https?://rutube\.ru/(?:video|(?:play/)?embed)/(?P<id>[\da-z]{32})'
+    _VALID_URL = r'https?://rutube\.ru/(?:video(?:/private)?|(?:play/)?embed)/(?P<id>[\da-z]{32})'
+    _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/(?:play/)?embed/[\da-z]{32}.*?)\1']
 
     _TESTS = [{
         'url': 'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
-        'md5': '1d24f180fac7a02f3900712e5a5764d6',
+        'md5': 'e33ac625efca66aba86cbec9851f2692',
         'info_dict': {
             'id': '3eac3b4561676c17df9132a9a1e62e3e',
             'ext': 'mp4',
@@ -112,7 +110,12 @@ class RutubeIE(RutubeBaseIE):
             'timestamp': 1381943602,
             'upload_date': '20131016',
             'age_limit': 0,
+            'view_count': int,
+            'thumbnail': 'http://pic.rutubelist.ru/video/d2/a0/d2a0aec998494a396deafc7ba2c82add.jpg',
+            'categories': ['Новости и СМИ'],
+            'chapters': [],
         },
+        'expected_warnings': ['Unable to download f4m'],
     }, {
         'url': 'http://rutube.ru/play/embed/a10e53b86e8f349080f718582ce4c661',
         'only_matching': True,
@@ -125,22 +128,56 @@ class RutubeIE(RutubeBaseIE):
     }, {
         'url': 'https://rutube.ru/video/10b3a03fc01d5bbcc632a2f3514e8aab/?pl_type=source',
         'only_matching': True,
+    }, {
+        'url': 'https://rutube.ru/video/private/884fb55f07a97ab673c7d654553e0f48/?p=x2QojCumHTS3rsKHWXN8Lg',
+        'md5': 'd106225f15d625538fe22971158e896f',
+        'info_dict': {
+            'id': '884fb55f07a97ab673c7d654553e0f48',
+            'ext': 'mp4',
+            'title': 'Яцуноками, Nioh2',
+            'description': 'Nioh2: финал сражения с боссом Яцуноками',
+            'duration': 15,
+            'uploader': 'mexus',
+            'uploader_id': '24222106',
+            'timestamp': 1670646232,
+            'upload_date': '20221210',
+            'age_limit': 0,
+            'view_count': int,
+            'thumbnail': 'http://pic.rutubelist.ru/video/f2/d4/f2d42b54be0a6e69c1c22539e3152156.jpg',
+            'categories': ['Видеоигры'],
+            'chapters': [],
+        },
+        'expected_warnings': ['Unable to download f4m'],
+    }, {
+        'url': 'https://rutube.ru/video/c65b465ad0c98c89f3b25cb03dcc87c6/',
+        'info_dict': {
+            'id': 'c65b465ad0c98c89f3b25cb03dcc87c6',
+            'ext': 'mp4',
+            'chapters': 'count:4',
+            'categories': ['Бизнес и предпринимательство'],
+            'description': 'md5:252feac1305257d8c1bab215cedde75d',
+            'thumbnail': 'http://pic.rutubelist.ru/video/71/8f/718f27425ea9706073eb80883dd3787b.png',
+            'duration': 782,
+            'age_limit': 0,
+            'uploader_id': '23491359',
+            'timestamp': 1677153329,
+            'view_count': int,
+            'upload_date': '20230223',
+            'title': 'Бизнес с нуля: найм сотрудников. Интервью с директором строительной компании',
+            'uploader': 'Стас Быков',
+        },
+        'expected_warnings': ['Unable to download f4m'],
     }]
 
     @classmethod
     def suitable(cls, url):
         return False if RutubePlaylistIE.suitable(url) else super(RutubeIE, cls).suitable(url)
 
-    @staticmethod
-    def _extract_urls(webpage):
-        return [mobj.group('url') for mobj in re.finditer(
-            r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//rutube\.ru/embed/[\da-z]{32}.*?)\1',
-            webpage)]
-
     def _real_extract(self, url):
         video_id = self._match_id(url)
-        info = self._download_and_extract_info(video_id)
-        info['formats'] = self._download_and_extract_formats(video_id)
+        query = parse_qs(url)
+        info = self._download_and_extract_info(video_id, query)
+        info['formats'] = self._download_and_extract_formats(video_id, query)
         return info
 
 
@@ -249,7 +286,6 @@ class RutubeMovieIE(RutubePlaylistBaseIE):
     IE_NAME = 'rutube:movie'
     IE_DESC = 'Rutube movies'
     _VALID_URL = r'https?://rutube\.ru/metainfo/tv/(?P<id>\d+)'
-    _TESTS = []
 
     _MOVIE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/?format=json'
     _PAGE_TEMPLATE = 'http://rutube.ru/api/metainfo/tv/%s/video?page=%s&format=json'