flickr.py 3.5 KB

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