]>
Commit | Line | Data |
---|---|---|
fabfe17d PH |
1 | from __future__ import unicode_literals |
2 | ||
1ac4004f | 3 | from .common import InfoExtractor |
15707c7e | 4 | from ..compat import compat_urllib_parse_urlencode |
1ac4004f | 5 | from ..utils import ( |
f3003531 | 6 | ExtractorError, |
02fb9804 | 7 | int_or_none, |
8 | qualities, | |
1ac4004f PH |
9 | ) |
10 | ||
11 | ||
12 | class 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', | |
cb1fa588 | 27 | 'uploader_url': 'https://www.flickr.com/photos/forestwander-nature-pictures/', |
02fb9804 | 28 | 'comment_count': int, |
5b95419c | 29 | 'view_count': int, |
30 | 'tags': list, | |
3fd6332c | 31 | 'license': 'Attribution-ShareAlike', |
6f5ac90c PH |
32 | } |
33 | } | |
02fb9804 | 34 | _API_BASE_URL = 'https://api.flickr.com/services/rest?' |
3fd6332c | 35 | # https://help.yahoo.com/kb/flickr/SLN25525.html |
36 | _LICENSES = { | |
37 | '0': 'All Rights Reserved', | |
38 | '1': 'Attribution-NonCommercial-ShareAlike', | |
39 | '2': 'Attribution-NonCommercial', | |
40 | '3': 'Attribution-NonCommercial-NoDerivs', | |
41 | '4': 'Attribution', | |
42 | '5': 'Attribution-ShareAlike', | |
43 | '6': 'Attribution-NoDerivs', | |
44 | '7': 'No known copyright restrictions', | |
45 | '8': 'United States government work', | |
46 | '9': 'Public Domain Dedication (CC0)', | |
47 | '10': 'Public Domain Work', | |
48 | } | |
f8e51f60 | 49 | |
14667225 | 50 | def _call_api(self, method, video_id, api_key, note, secret=None): |
02fb9804 | 51 | query = { |
52 | 'photo_id': video_id, | |
53 | 'method': 'flickr.%s' % method, | |
14667225 | 54 | 'api_key': api_key, |
02fb9804 | 55 | 'format': 'json', |
56 | 'nojsoncallback': 1, | |
57 | } | |
58 | if secret: | |
59 | query['secret'] = secret | |
15707c7e | 60 | data = self._download_json(self._API_BASE_URL + compat_urllib_parse_urlencode(query), video_id, note) |
f3003531 | 61 | if data['stat'] != 'ok': |
62 | raise ExtractorError(data['message']) | |
63 | return data | |
1ac4004f | 64 | |
02fb9804 | 65 | def _real_extract(self, url): |
66 | video_id = self._match_id(url) | |
1ac4004f | 67 | |
eed30fea | 68 | api_key = self._download_json( |
69 | 'https://www.flickr.com/hermes_error_beacon.gne', video_id, | |
70 | 'Downloading api key')['site_key'] | |
14667225 | 71 | |
eed30fea | 72 | video_info = self._call_api( |
73 | 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo'] | |
02fb9804 | 74 | if video_info['media'] == 'video': |
eed30fea | 75 | streams = self._call_api( |
76 | 'video.getStreamInfo', video_id, api_key, | |
77 | 'Downloading streams info', video_info['secret'])['streams'] | |
1ac4004f | 78 | |
eed30fea | 79 | preference = qualities( |
80 | ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig']) | |
1ac4004f | 81 | |
02fb9804 | 82 | formats = [] |
83 | for stream in streams['stream']: | |
84 | stream_type = str(stream.get('type')) | |
85 | formats.append({ | |
86 | 'format_id': stream_type, | |
87 | 'url': stream['_content'], | |
88 | 'preference': preference(stream_type), | |
89 | }) | |
90 | self._sort_formats(formats) | |
1ac4004f | 91 | |
02fb9804 | 92 | owner = video_info.get('owner', {}) |
cb1fa588 S |
93 | uploader_id = owner.get('nsid') |
94 | uploader_path = owner.get('path_alias') or uploader_id | |
95 | uploader_url = 'https://www.flickr.com/photos/%s/' % uploader_path if uploader_path else None | |
1ac4004f | 96 | |
02fb9804 | 97 | return { |
98 | 'id': video_id, | |
99 | 'title': video_info['title']['_content'], | |
100 | 'description': video_info.get('description', {}).get('_content'), | |
101 | 'formats': formats, | |
102 | 'timestamp': int_or_none(video_info.get('dateuploaded')), | |
103 | 'duration': int_or_none(video_info.get('video', {}).get('duration')), | |
cb1fa588 | 104 | 'uploader_id': uploader_id, |
02fb9804 | 105 | 'uploader': owner.get('realname'), |
cb1fa588 | 106 | 'uploader_url': uploader_url, |
02fb9804 | 107 | 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')), |
5b95419c | 108 | 'view_count': int_or_none(video_info.get('views')), |
3fd6332c | 109 | 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])], |
110 | 'license': self._LICENSES.get(video_info.get('license')), | |
02fb9804 | 111 | } |
967c9076 | 112 | else: |
113 | raise ExtractorError('not a video', expected=True) |