]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/redgifs.py
[redgifs] Add extractor (#1631)
[yt-dlp.git] / yt_dlp / extractor / redgifs.py
CommitLineData
4e4ba1d7 1# coding: utf-8
2
3from .common import InfoExtractor
4from ..utils import (
5 ExtractorError,
6 int_or_none,
7 qualities,
8 try_get,
9)
10
11
12class RedGifsIE(InfoExtractor):
13 _VALID_URL = r'https?://(?:(?:www|thumbs2?)\.)?redgifs\.com/(?:watch/)?(?P<id>[^-/?#\.]+)'
14 _FORMATS = {
15 'gif': 250,
16 'sd': 480,
17 'hd': None,
18 }
19 _TESTS = [{
20 'url': 'https://www.redgifs.com/watch/squeakyhelplesswisent',
21 'info_dict': {
22 'id': 'squeakyhelplesswisent',
23 'ext': 'mp4',
24 'title': 'Hotwife Legs Thick',
25 'timestamp': 1636287915,
26 'upload_date': '20211107',
27 'uploader': 'ignored52',
28 'duration': 16,
29 'view_count': int,
30 'like_count': int,
31 'categories': list,
32 'age_limit': 18,
33 }
34 }, {
35 'url': 'https://thumbs2.redgifs.com/SqueakyHelplessWisent-mobile.mp4#t=0',
36 'info_dict': {
37 'id': 'squeakyhelplesswisent',
38 'ext': 'mp4',
39 'title': 'Hotwife Legs Thick',
40 'timestamp': 1636287915,
41 'upload_date': '20211107',
42 'uploader': 'ignored52',
43 'duration': 16,
44 'view_count': int,
45 'like_count': int,
46 'categories': list,
47 'age_limit': 18,
48 }
49 }]
50
51 def _real_extract(self, url):
52 video_id = self._match_id(url).lower()
53
54 video_info = self._download_json(
55 'https://api.redgifs.com/v2/gifs/%s' % video_id,
56 video_id, 'Downloading video info')
57 if 'error' in video_info:
58 raise ExtractorError(f'RedGifs said: {video_info["error"]}', expected=True)
59
60 gif = video_info['gif']
61 urls = gif['urls']
62
63 quality = qualities(tuple(self._FORMATS.keys()))
64
65 orig_height = int_or_none(gif.get('height'))
66 aspect_ratio = try_get(gif, lambda x: orig_height / x['width'])
67
68 formats = []
69 for format_id, height in self._FORMATS.items():
70 video_url = urls.get(format_id)
71 if not video_url:
72 continue
73 height = min(orig_height, height or orig_height)
74 formats.append({
75 'url': video_url,
76 'format_id': format_id,
77 'width': height * aspect_ratio if aspect_ratio else None,
78 'height': height,
79 'quality': quality(format_id),
80 })
81 self._sort_formats(formats)
82
83 return {
84 'id': video_id,
85 'title': ' '.join(gif.get('tags') or []) or 'RedGifs',
86 'timestamp': int_or_none(gif.get('createDate')),
87 'uploader': gif.get('userName'),
88 'duration': int_or_none(gif.get('duration')),
89 'view_count': int_or_none(gif.get('views')),
90 'like_count': int_or_none(gif.get('likes')),
91 'categories': gif.get('tags') or [],
92 'age_limit': 18,
93 'formats': formats,
94 }