[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / pr0gramm.py
blobb0d6475fe4c79c1e168dec13dec5f62cb48564d0
1 import datetime as dt
2 import functools
3 import json
4 import urllib.parse
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 float_or_none,
10 int_or_none,
11 make_archive_id,
12 mimetype2ext,
13 str_or_none,
14 urljoin,
16 from ..utils.traversal import traverse_obj
19 class Pr0grammIE(InfoExtractor):
20 _VALID_URL = r'https?://pr0gramm\.com\/(?:[^/?#]+/)+(?P<id>[\d]+)(?:[/?#:]|$)'
21 _TESTS = [{
22 'url': 'https://pr0gramm.com/new/video/5466437',
23 'info_dict': {
24 'id': '5466437',
25 'ext': 'mp4',
26 'title': 'pr0gramm-5466437 by g11st',
27 'tags': ['Neon Genesis Evangelion', 'Touhou Project', 'Fly me to the Moon', 'Marisad', 'Marisa Kirisame', 'video', 'sound', 'Marisa', 'Anime'],
28 'uploader': 'g11st',
29 'uploader_id': '394718',
30 'timestamp': 1671590240,
31 'upload_date': '20221221',
32 'like_count': int,
33 'dislike_count': int,
34 'age_limit': 0,
35 'thumbnail': r're:^https://thumb\.pr0gramm\.com/.*\.jpg',
36 '_old_archive_ids': ['pr0grammstatic 5466437'],
38 }, {
39 'url': 'https://pr0gramm.com/new/3052805:comment28391322',
40 'info_dict': {
41 'id': '3052805',
42 'ext': 'mp4',
43 'title': 'pr0gramm-3052805 by Hansking1',
44 'tags': 'count:15',
45 'uploader': 'Hansking1',
46 'uploader_id': '385563',
47 'timestamp': 1552930408,
48 'upload_date': '20190318',
49 'like_count': int,
50 'dislike_count': int,
51 'age_limit': 0,
52 'thumbnail': r're:^https://thumb\.pr0gramm\.com/.*\.jpg',
53 '_old_archive_ids': ['pr0grammstatic 3052805'],
55 }, {
56 # Requires verified account
57 'url': 'https://pr0gramm.com/new/Gianna%20Michaels/5848332',
58 'info_dict': {
59 'id': '5848332',
60 'ext': 'mp4',
61 'title': 'pr0gramm-5848332 by erd0pfel',
62 'tags': 'count:18',
63 'uploader': 'erd0pfel',
64 'uploader_id': '349094',
65 'timestamp': 1694489652,
66 'upload_date': '20230912',
67 'like_count': int,
68 'dislike_count': int,
69 'age_limit': 18,
70 'thumbnail': r're:^https://thumb\.pr0gramm\.com/.*\.jpg',
71 '_old_archive_ids': ['pr0grammstatic 5848332'],
73 }, {
74 'url': 'https://pr0gramm.com/top/5895149',
75 'info_dict': {
76 'id': '5895149',
77 'ext': 'mp4',
78 'title': 'pr0gramm-5895149 by algoholigSeeManThrower',
79 'tags': 'count:19',
80 'uploader': 'algoholigSeeManThrower',
81 'uploader_id': '457556',
82 'timestamp': 1697580902,
83 'upload_date': '20231018',
84 'like_count': int,
85 'dislike_count': int,
86 'age_limit': 0,
87 'thumbnail': 'https://thumb.pr0gramm.com/2023/10/18/db47bb3db5e1a1b3.jpg',
88 '_old_archive_ids': ['pr0grammstatic 5895149'],
90 }, {
91 'url': 'https://pr0gramm.com/static/5466437',
92 'only_matching': True,
93 }, {
94 'url': 'https://pr0gramm.com/new/rowan%20atkinson%20herr%20bohne/3052805',
95 'only_matching': True,
96 }, {
97 'url': 'https://pr0gramm.com/user/froschler/dafur-ist-man-hier/5091290',
98 'only_matching': True,
101 BASE_URL = 'https://pr0gramm.com'
103 @functools.cached_property
104 def _is_logged_in(self):
105 return 'pp' in self._get_cookies(self.BASE_URL)
107 @functools.cached_property
108 def _maximum_flags(self):
109 # We need to guess the flags for the content otherwise the api will raise an error
110 # We can guess the maximum allowed flags for the account from the cookies
111 # Bitflags are (msbf): pol, nsfp, nsfl, nsfw, sfw
112 flags = 0b10001
113 if self._is_logged_in:
114 flags |= 0b01000
115 cookies = self._get_cookies(self.BASE_URL)
116 if 'me' not in cookies:
117 self._download_webpage(self.BASE_URL, None, 'Refreshing verification information')
118 if traverse_obj(cookies, ('me', {lambda x: x.value}, {urllib.parse.unquote}, {json.loads}, 'verified')):
119 flags |= 0b00110
121 return flags
123 def _call_api(self, endpoint, video_id, query={}, note='Downloading API json'):
124 data = self._download_json(
125 f'https://pr0gramm.com/api/items/{endpoint}',
126 video_id, note, query=query, expected_status=403)
128 error = traverse_obj(data, ('error', {str}))
129 if error in ('nsfwRequired', 'nsflRequired', 'nsfpRequired', 'verificationRequired'):
130 if not self._is_logged_in:
131 self.raise_login_required()
132 raise ExtractorError(f'Unverified account cannot access NSFW/NSFL ({error})', expected=True)
133 elif error:
134 message = traverse_obj(data, ('msg', {str})) or error
135 raise ExtractorError(f'API returned error: {message}', expected=True)
137 return data
139 @staticmethod
140 def _create_source_url(path):
141 return urljoin('https://img.pr0gramm.com', path)
143 def _real_extract(self, url):
144 video_id = self._match_id(url)
145 video_info = traverse_obj(
146 self._call_api('get', video_id, {'id': video_id, 'flags': self._maximum_flags}),
147 ('items', 0, {dict}))
149 source = video_info.get('image')
150 if not source or not source.endswith('mp4'):
151 self.raise_no_formats('Could not extract a video', expected=bool(source), video_id=video_id)
153 metadata = self._call_api('info', video_id, {'itemId': video_id}, note='Downloading tags')
154 tags = traverse_obj(metadata, ('tags', ..., 'tag', {str}))
155 # Sorted by "confidence", higher confidence = earlier in list
156 confidences = traverse_obj(metadata, ('tags', ..., 'confidence', ({int}, {float})))
157 if confidences:
158 tags = [tag for _, tag in sorted(zip(confidences, tags), reverse=True)]
160 formats = traverse_obj(video_info, ('variants', ..., {
161 'format_id': ('name', {str}),
162 'url': ('path', {self._create_source_url}),
163 'ext': ('mimeType', {mimetype2ext}),
164 'vcodec': ('codec', {str}),
165 'width': ('width', {int_or_none}),
166 'height': ('height', {int_or_none}),
167 'bitrate': ('bitRate', {float_or_none}),
168 'filesize': ('fileSize', {int_or_none}),
169 })) if video_info.get('variants') else [{
170 'ext': 'mp4',
171 'format_id': 'source',
172 **traverse_obj(video_info, {
173 'url': ('image', {self._create_source_url}),
174 'width': ('width', {int_or_none}),
175 'height': ('height', {int_or_none}),
179 subtitles = {}
180 for subtitle in traverse_obj(video_info, ('subtitles', lambda _, v: v['language'])):
181 subtitles.setdefault(subtitle['language'], []).append(traverse_obj(subtitle, {
182 'url': ('path', {self._create_source_url}),
183 'note': ('label', {str}),
186 return {
187 'id': video_id,
188 'title': f'pr0gramm-{video_id} by {video_info.get("user")}',
189 'tags': tags,
190 'formats': formats,
191 'subtitles': subtitles,
192 'age_limit': 18 if traverse_obj(video_info, ('flags', {0b110.__and__})) else 0,
193 '_old_archive_ids': [make_archive_id('Pr0grammStatic', video_id)],
194 **traverse_obj(video_info, {
195 'uploader': ('user', {str}),
196 'uploader_id': ('userId', {str_or_none}),
197 'like_count': ('up', {int}),
198 'dislike_count': ('down', {int}),
199 'timestamp': ('created', {int}),
200 'upload_date': ('created', {int}, {dt.date.fromtimestamp}, {lambda x: x.strftime('%Y%m%d')}),
201 'thumbnail': ('thumb', {lambda x: urljoin('https://thumb.pr0gramm.com', x)}),