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