Merge branch 'googledrive' of github.com:remitamine/youtube-dl into remitamine-google...
[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     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         }
31     }
32
33     _API_BASE_URL = 'https://api.flickr.com/services/rest?'
34
35     def _call_api(self, method, video_id, api_key, note, secret=None):
36         query = {
37             'photo_id': video_id,
38             'method': 'flickr.%s' % method,
39             'api_key': api_key,
40             'format': 'json',
41             'nojsoncallback': 1,
42         }
43         if secret:
44             query['secret'] = secret
45         data = self._download_json(self._API_BASE_URL + compat_urllib_parse.urlencode(query), video_id, note)
46         if data['stat'] != 'ok':
47             raise ExtractorError(data['message'])
48         return data
49
50     def _real_extract(self, url):
51         video_id = self._match_id(url)
52
53         api_key = self._download_json('https://www.flickr.com/hermes_error_beacon.gne', video_id, 'Downloading api key',)['site_key']
54
55         video_info = self._call_api('photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
56         if video_info['media'] == 'video':
57             streams = self._call_api('video.getStreamInfo', video_id, api_key, 'Downloading streams info', video_info['secret'])['streams']
58
59             preference = qualities(['iphone_wifi', '700', 'appletv', 'orig'])
60
61             formats = []
62             for stream in streams['stream']:
63                 stream_type = str(stream.get('type'))
64                 formats.append({
65                     'format_id': stream_type,
66                     'url': stream['_content'],
67                     'preference': preference(stream_type),
68                 })
69             self._sort_formats(formats)
70
71             owner = video_info.get('owner', {})
72
73             return {
74                 'id': video_id,
75                 'title': video_info['title']['_content'],
76                 'description': video_info.get('description', {}).get('_content'),
77                 'formats': formats,
78                 'timestamp': int_or_none(video_info.get('dateuploaded')),
79                 'duration': int_or_none(video_info.get('video', {}).get('duration')),
80                 'uploader_id': owner.get('nsid'),
81                 'uploader': owner.get('realname'),
82                 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
83                 'view_count': int_or_none(video_info.get('views')),
84                 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])]
85             }
86         else:
87             raise ExtractorError('not a video', expected=True)