You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.6 KiB

  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(
  47. 'https://www.flickr.com/hermes_error_beacon.gne', video_id,
  48. 'Downloading api key')['site_key']
  49. video_info = self._call_api(
  50. 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
  51. if video_info['media'] == 'video':
  52. streams = self._call_api(
  53. 'video.getStreamInfo', video_id, api_key,
  54. 'Downloading streams info', video_info['secret'])['streams']
  55. preference = qualities(
  56. ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig'])
  57. formats = []
  58. for stream in streams['stream']:
  59. stream_type = str(stream.get('type'))
  60. formats.append({
  61. 'format_id': stream_type,
  62. 'url': stream['_content'],
  63. 'preference': preference(stream_type),
  64. })
  65. self._sort_formats(formats)
  66. owner = video_info.get('owner', {})
  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. 'view_count': int_or_none(video_info.get('views')),
  78. 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])]
  79. }
  80. else:
  81. raise ExtractorError('not a video', expected=True)