[flickr] extract fresh api key and remove duplication in test
[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
5 from ..utils import (
6     int_or_none,
7     qualities,
8 )
9
10
11 class FlickrIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/[\w\-_@]+/(?P<id>\d+)'
13     _TEST = {
14         'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
15         'md5': '164fe3fa6c22e18d448d4d5af2330f31',
16         'info_dict': {
17             'id': '5645318632',
18             'ext': 'mpg',
19             '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.',
20             'title': 'Dark Hollow Waterfalls',
21             'duration': 19,
22             'timestamp': 1303528740,
23             'upload_date': '20110423',
24             'uploader_id': '10922353@N03',
25             'uploader': 'Forest Wander',
26             'comment_count': int,
27         }
28     }
29
30     _API_BASE_URL = 'https://api.flickr.com/services/rest?'
31
32     def _call_api(self, method, video_id, api_key, note, secret=None):
33         query = {
34             'photo_id': video_id,
35             'method': 'flickr.%s' % method,
36             'api_key': api_key,
37             'format': 'json',
38             'nojsoncallback': 1,
39         }
40         if secret:
41             query['secret'] = secret
42         return self._download_json(self._API_BASE_URL + compat_urllib_parse.urlencode(query), video_id, note)
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46
47         api_key = self._download_json('https://www.flickr.com/hermes_error_beacon.gne', video_id, 'Downloading api key',)['site_key']
48
49         video_info = self._call_api('photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
50         if video_info['media'] == 'video':
51             streams = self._call_api('video.getStreamInfo', video_id, api_key, 'Downloading streams info', video_info['secret'])['streams']
52
53             preference = qualities(['iphone_wifi', '700', 'appletv', 'orig'])
54
55             formats = []
56             for stream in streams['stream']:
57                 stream_type = str(stream.get('type'))
58                 formats.append({
59                     'format_id': stream_type,
60                     'url': stream['_content'],
61                     'preference': preference(stream_type),
62                 })
63             self._sort_formats(formats)
64
65             owner = video_info.get('owner', {})
66
67             return {
68                 'id': video_id,
69                 'title': video_info['title']['_content'],
70                 'description': video_info.get('description', {}).get('_content'),
71                 'formats': formats,
72                 'timestamp': int_or_none(video_info.get('dateuploaded')),
73                 'duration': int_or_none(video_info.get('video', {}).get('duration')),
74                 'uploader_id': owner.get('nsid'),
75                 'uploader': owner.get('realname'),
76                 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
77             }