]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/flickr.py
Merge pull request #9400 from inondle/master
[yt-dlp.git] / youtube_dl / extractor / flickr.py
CommitLineData
fabfe17d
PH
1from __future__ import unicode_literals
2
1ac4004f 3from .common import InfoExtractor
15707c7e 4from ..compat import compat_urllib_parse_urlencode
1ac4004f 5from ..utils import (
f3003531 6 ExtractorError,
02fb9804 7 int_or_none,
8 qualities,
1ac4004f
PH
9)
10
11
12class FlickrIE(InfoExtractor):
02fb9804 13 _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/[\w\-_@]+/(?P<id>\d+)'
6f5ac90c 14 _TEST = {
fabfe17d 15 'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
02fb9804 16 'md5': '164fe3fa6c22e18d448d4d5af2330f31',
fabfe17d 17 'info_dict': {
cc746841 18 'id': '5645318632',
02fb9804 19 'ext': 'mpg',
20 '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 21 'title': 'Dark Hollow Waterfalls',
22 'duration': 19,
23 'timestamp': 1303528740,
24 'upload_date': '20110423',
25 'uploader_id': '10922353@N03',
26 'uploader': 'Forest Wander',
27 'comment_count': int,
5b95419c 28 'view_count': int,
29 'tags': list,
6f5ac90c
PH
30 }
31 }
1ac4004f 32
02fb9804 33 _API_BASE_URL = 'https://api.flickr.com/services/rest?'
f8e51f60 34
14667225 35 def _call_api(self, method, video_id, api_key, note, secret=None):
02fb9804 36 query = {
37 'photo_id': video_id,
38 'method': 'flickr.%s' % method,
14667225 39 'api_key': api_key,
02fb9804 40 'format': 'json',
41 'nojsoncallback': 1,
42 }
43 if secret:
44 query['secret'] = secret
15707c7e 45 data = self._download_json(self._API_BASE_URL + compat_urllib_parse_urlencode(query), video_id, note)
f3003531 46 if data['stat'] != 'ok':
47 raise ExtractorError(data['message'])
48 return data
1ac4004f 49
02fb9804 50 def _real_extract(self, url):
51 video_id = self._match_id(url)
1ac4004f 52
eed30fea 53 api_key = self._download_json(
54 'https://www.flickr.com/hermes_error_beacon.gne', video_id,
55 'Downloading api key')['site_key']
14667225 56
eed30fea 57 video_info = self._call_api(
58 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
02fb9804 59 if video_info['media'] == 'video':
eed30fea 60 streams = self._call_api(
61 'video.getStreamInfo', video_id, api_key,
62 'Downloading streams info', video_info['secret'])['streams']
1ac4004f 63
eed30fea 64 preference = qualities(
65 ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig'])
1ac4004f 66
02fb9804 67 formats = []
68 for stream in streams['stream']:
69 stream_type = str(stream.get('type'))
70 formats.append({
71 'format_id': stream_type,
72 'url': stream['_content'],
73 'preference': preference(stream_type),
74 })
75 self._sort_formats(formats)
1ac4004f 76
02fb9804 77 owner = video_info.get('owner', {})
1ac4004f 78
02fb9804 79 return {
80 'id': video_id,
81 'title': video_info['title']['_content'],
82 'description': video_info.get('description', {}).get('_content'),
83 'formats': formats,
84 'timestamp': int_or_none(video_info.get('dateuploaded')),
85 'duration': int_or_none(video_info.get('video', {}).get('duration')),
86 'uploader_id': owner.get('nsid'),
87 'uploader': owner.get('realname'),
88 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
5b95419c 89 'view_count': int_or_none(video_info.get('views')),
90 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])]
02fb9804 91 }
967c9076 92 else:
93 raise ExtractorError('not a video', expected=True)