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