]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/flickr.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / flickr.py
1 from .common import InfoExtractor
2 from ..compat import (
3 compat_str,
4 compat_urllib_parse_urlencode,
5 )
6 from ..utils import (
7 ExtractorError,
8 format_field,
9 int_or_none,
10 qualities,
11 )
12
13
14 class FlickrIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/[\w\-_@]+/(?P<id>\d+)'
16 _TEST = {
17 'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
18 'md5': '164fe3fa6c22e18d448d4d5af2330f31',
19 'info_dict': {
20 'id': '5645318632',
21 'ext': 'mpg',
22 'description': 'Waterfalls in the Springtime at Dark Hollow Waterfalls. These are located just off of Skyline Drive in Virginia. They are only about 6/10 of a mile hike but it is a pretty steep hill and a good climb back up.',
23 'title': 'Dark Hollow Waterfalls',
24 'duration': 19,
25 'timestamp': 1303528740,
26 'upload_date': '20110423',
27 'uploader_id': '10922353@N03',
28 'uploader': 'Forest Wander',
29 'uploader_url': 'https://www.flickr.com/photos/forestwander-nature-pictures/',
30 'comment_count': int,
31 'view_count': int,
32 'tags': list,
33 'license': 'Attribution-ShareAlike',
34 }
35 }
36 _API_BASE_URL = 'https://api.flickr.com/services/rest?'
37 # https://help.yahoo.com/kb/flickr/SLN25525.html
38 _LICENSES = {
39 '0': 'All Rights Reserved',
40 '1': 'Attribution-NonCommercial-ShareAlike',
41 '2': 'Attribution-NonCommercial',
42 '3': 'Attribution-NonCommercial-NoDerivs',
43 '4': 'Attribution',
44 '5': 'Attribution-ShareAlike',
45 '6': 'Attribution-NoDerivs',
46 '7': 'No known copyright restrictions',
47 '8': 'United States government work',
48 '9': 'Public Domain Dedication (CC0)',
49 '10': 'Public Domain Work',
50 }
51
52 def _call_api(self, method, video_id, api_key, note, secret=None):
53 query = {
54 'photo_id': video_id,
55 'method': 'flickr.%s' % method,
56 'api_key': api_key,
57 'format': 'json',
58 'nojsoncallback': 1,
59 }
60 if secret:
61 query['secret'] = secret
62 data = self._download_json(self._API_BASE_URL + compat_urllib_parse_urlencode(query), video_id, note)
63 if data['stat'] != 'ok':
64 raise ExtractorError(data['message'])
65 return data
66
67 def _real_extract(self, url):
68 video_id = self._match_id(url)
69
70 api_key = self._download_json(
71 'https://www.flickr.com/hermes_error_beacon.gne', video_id,
72 'Downloading api key')['site_key']
73
74 video_info = self._call_api(
75 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
76 if video_info['media'] == 'video':
77 streams = self._call_api(
78 'video.getStreamInfo', video_id, api_key,
79 'Downloading streams info', video_info['secret'])['streams']
80
81 preference = qualities(
82 ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig'])
83
84 formats = []
85 for stream in streams['stream']:
86 stream_type = compat_str(stream.get('type'))
87 formats.append({
88 'format_id': stream_type,
89 'url': stream['_content'],
90 'quality': preference(stream_type),
91 })
92
93 owner = video_info.get('owner', {})
94 uploader_id = owner.get('nsid')
95 uploader_path = owner.get('path_alias') or uploader_id
96 uploader_url = format_field(uploader_path, None, 'https://www.flickr.com/photos/%s/')
97
98 return {
99 'id': video_id,
100 'title': video_info['title']['_content'],
101 'description': video_info.get('description', {}).get('_content'),
102 'formats': formats,
103 'timestamp': int_or_none(video_info.get('dateuploaded')),
104 'duration': int_or_none(video_info.get('video', {}).get('duration')),
105 'uploader_id': uploader_id,
106 'uploader': owner.get('realname'),
107 'uploader_url': uploader_url,
108 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
109 'view_count': int_or_none(video_info.get('views')),
110 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])],
111 'license': self._LICENSES.get(video_info.get('license')),
112 }
113 else:
114 raise ExtractorError('not a video', expected=True)