]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/flickr.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / flickr.py
CommitLineData
1ac4004f 1from .common import InfoExtractor
15aeeb11
S
2from ..compat import (
3 compat_str,
4 compat_urllib_parse_urlencode,
5)
1ac4004f 6from ..utils import (
f3003531 7 ExtractorError,
e0ddbd02 8 format_field,
02fb9804 9 int_or_none,
10 qualities,
1ac4004f
PH
11)
12
13
14class FlickrIE(InfoExtractor):
02fb9804 15 _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/[\w\-_@]+/(?P<id>\d+)'
6f5ac90c 16 _TEST = {
fabfe17d 17 'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
02fb9804 18 'md5': '164fe3fa6c22e18d448d4d5af2330f31',
fabfe17d 19 'info_dict': {
cc746841 20 'id': '5645318632',
02fb9804 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.',
02fb9804 23 'title': 'Dark Hollow Waterfalls',
24 'duration': 19,
25 'timestamp': 1303528740,
26 'upload_date': '20110423',
27 'uploader_id': '10922353@N03',
28 'uploader': 'Forest Wander',
cb1fa588 29 'uploader_url': 'https://www.flickr.com/photos/forestwander-nature-pictures/',
02fb9804 30 'comment_count': int,
5b95419c 31 'view_count': int,
32 'tags': list,
3fd6332c 33 'license': 'Attribution-ShareAlike',
6f5ac90c
PH
34 }
35 }
02fb9804 36 _API_BASE_URL = 'https://api.flickr.com/services/rest?'
3fd6332c 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 }
f8e51f60 51
14667225 52 def _call_api(self, method, video_id, api_key, note, secret=None):
02fb9804 53 query = {
54 'photo_id': video_id,
55 'method': 'flickr.%s' % method,
14667225 56 'api_key': api_key,
02fb9804 57 'format': 'json',
58 'nojsoncallback': 1,
59 }
60 if secret:
61 query['secret'] = secret
15707c7e 62 data = self._download_json(self._API_BASE_URL + compat_urllib_parse_urlencode(query), video_id, note)
f3003531 63 if data['stat'] != 'ok':
64 raise ExtractorError(data['message'])
65 return data
1ac4004f 66
02fb9804 67 def _real_extract(self, url):
68 video_id = self._match_id(url)
1ac4004f 69
eed30fea 70 api_key = self._download_json(
71 'https://www.flickr.com/hermes_error_beacon.gne', video_id,
72 'Downloading api key')['site_key']
14667225 73
eed30fea 74 video_info = self._call_api(
75 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
02fb9804 76 if video_info['media'] == 'video':
eed30fea 77 streams = self._call_api(
78 'video.getStreamInfo', video_id, api_key,
79 'Downloading streams info', video_info['secret'])['streams']
1ac4004f 80
eed30fea 81 preference = qualities(
82 ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig'])
1ac4004f 83
02fb9804 84 formats = []
85 for stream in streams['stream']:
15aeeb11 86 stream_type = compat_str(stream.get('type'))
02fb9804 87 formats.append({
88 'format_id': stream_type,
89 'url': stream['_content'],
f983b875 90 'quality': preference(stream_type),
02fb9804 91 })
1ac4004f 92
02fb9804 93 owner = video_info.get('owner', {})
cb1fa588
S
94 uploader_id = owner.get('nsid')
95 uploader_path = owner.get('path_alias') or uploader_id
a70635b8 96 uploader_url = format_field(uploader_path, None, 'https://www.flickr.com/photos/%s/')
1ac4004f 97
02fb9804 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')),
cb1fa588 105 'uploader_id': uploader_id,
02fb9804 106 'uploader': owner.get('realname'),
cb1fa588 107 'uploader_url': uploader_url,
02fb9804 108 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
5b95419c 109 'view_count': int_or_none(video_info.get('views')),
3fd6332c 110 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])],
111 'license': self._LICENSES.get(video_info.get('license')),
02fb9804 112 }
967c9076 113 else:
114 raise ExtractorError('not a video', expected=True)