]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/pr0gramm.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / pr0gramm.py
CommitLineData
c305a25c 1import datetime as dt
b532556d 2import json
e3a3ed8a 3import urllib.parse
45b2ee6f 4
5from .common import InfoExtractor
b532556d 6from ..compat import functools
f98a3305
SS
7from ..utils import (
8 ExtractorError,
9 float_or_none,
10 int_or_none,
11 make_archive_id,
12 mimetype2ext,
f4f9f6d0 13 str_or_none,
f98a3305
SS
14 urljoin,
15)
b532556d 16from ..utils.traversal import traverse_obj
45b2ee6f 17
18
b532556d
SS
19class Pr0grammIE(InfoExtractor):
20 _VALID_URL = r'https?://pr0gramm\.com\/(?:[^/?#]+/)+(?P<id>[\d]+)(?:[/?#:]|$)'
21 _TESTS = [{
b532556d 22 'url': 'https://pr0gramm.com/new/video/5466437',
45b2ee6f 23 'info_dict': {
24 'id': '5466437',
25 'ext': 'mp4',
26 'title': 'pr0gramm-5466437 by g11st',
b532556d 27 'tags': ['Neon Genesis Evangelion', 'Touhou Project', 'Fly me to the Moon', 'Marisad', 'Marisa Kirisame', 'video', 'sound', 'Marisa', 'Anime'],
45b2ee6f 28 'uploader': 'g11st',
f4f9f6d0 29 'uploader_id': '394718',
30 'timestamp': 1671590240,
45b2ee6f 31 'upload_date': '20221221',
b532556d
SS
32 'like_count': int,
33 'dislike_count': int,
34 'age_limit': 0,
35 'thumbnail': r're:^https://thumb\.pr0gramm\.com/.*\.jpg',
f98a3305 36 '_old_archive_ids': ['pr0grammstatic 5466437'],
b532556d
SS
37 },
38 }, {
b532556d
SS
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',
f4f9f6d0 46 'uploader_id': '385563',
47 'timestamp': 1552930408,
b532556d
SS
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',
f98a3305 53 '_old_archive_ids': ['pr0grammstatic 3052805'],
b532556d
SS
54 },
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',
f4f9f6d0 64 'uploader_id': '349094',
65 'timestamp': 1694489652,
b532556d
SS
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',
f98a3305 71 '_old_archive_ids': ['pr0grammstatic 5848332'],
b532556d 72 },
5f25f348
SS
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',
f4f9f6d0 81 'uploader_id': '457556',
82 'timestamp': 1697580902,
5f25f348
SS
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'],
89 },
b532556d
SS
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,
99 }]
45b2ee6f 100
b532556d 101 BASE_URL = 'https://pr0gramm.com'
45b2ee6f 102
b532556d
SS
103 @functools.cached_property
104 def _is_logged_in(self):
105 return 'pp' in self._get_cookies(self.BASE_URL)
45b2ee6f 106
b532556d
SS
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
5f25f348
SS
111 # Bitflags are (msbf): pol, nsfp, nsfl, nsfw, sfw
112 flags = 0b10001
b532556d 113 if self._is_logged_in:
5f25f348 114 flags |= 0b01000
b532556d
SS
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')
e3a3ed8a 118 if traverse_obj(cookies, ('me', {lambda x: x.value}, {urllib.parse.unquote}, {json.loads}, 'verified')):
5f25f348 119 flags |= 0b00110
45b2ee6f 120
b532556d 121 return flags
45b2ee6f 122
b532556d
SS
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)
45b2ee6f 127
b532556d
SS
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)
45b2ee6f 136
b532556d 137 return data
45b2ee6f 138
f98a3305
SS
139 @staticmethod
140 def _create_source_url(path):
141 return urljoin('https://img.pr0gramm.com', path)
142
45b2ee6f 143 def _real_extract(self, url):
144 video_id = self._match_id(url)
b532556d
SS
145 video_info = traverse_obj(
146 self._call_api('get', video_id, {'id': video_id, 'flags': self._maximum_flags}),
147 ('items', 0, {dict}))
148
f98a3305 149 source = video_info.get('image')
b532556d
SS
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)
45b2ee6f 152
5f25f348
SS
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)]
b532556d 159
f98a3305
SS
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}),
176 }),
177 }]
178
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}),
184 }))
185
b532556d
SS
186 return {
187 'id': video_id,
188 'title': f'pr0gramm-{video_id} by {video_info.get("user")}',
b532556d 189 'tags': tags,
f98a3305
SS
190 'formats': formats,
191 'subtitles': subtitles,
b532556d
SS
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}),
f4f9f6d0 196 'uploader_id': ('userId', {str_or_none}),
b532556d
SS
197 'like_count': ('up', {int}),
198 'dislike_count': ('down', {int}),
f4f9f6d0 199 'timestamp': ('created', {int}),
c305a25c 200 'upload_date': ('created', {int}, {dt.date.fromtimestamp}, {lambda x: x.strftime('%Y%m%d')}),
add96eb9 201 'thumbnail': ('thumb', {lambda x: urljoin('https://thumb.pr0gramm.com', x)}),
b532556d
SS
202 }),
203 }