flickr.py 3.3 KB

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