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