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.

116 lines
4.7 KiB

  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_str,
  5. compat_urllib_parse_urlencode,
  6. )
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. qualities,
  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. 'uploader_url': 'https://www.flickr.com/photos/forestwander-nature-pictures/',
  28. 'comment_count': int,
  29. 'view_count': int,
  30. 'tags': list,
  31. 'license': 'Attribution-ShareAlike',
  32. }
  33. }
  34. _API_BASE_URL = 'https://api.flickr.com/services/rest?'
  35. # https://help.yahoo.com/kb/flickr/SLN25525.html
  36. _LICENSES = {
  37. '0': 'All Rights Reserved',
  38. '1': 'Attribution-NonCommercial-ShareAlike',
  39. '2': 'Attribution-NonCommercial',
  40. '3': 'Attribution-NonCommercial-NoDerivs',
  41. '4': 'Attribution',
  42. '5': 'Attribution-ShareAlike',
  43. '6': 'Attribution-NoDerivs',
  44. '7': 'No known copyright restrictions',
  45. '8': 'United States government work',
  46. '9': 'Public Domain Dedication (CC0)',
  47. '10': 'Public Domain Work',
  48. }
  49. def _call_api(self, method, video_id, api_key, note, secret=None):
  50. query = {
  51. 'photo_id': video_id,
  52. 'method': 'flickr.%s' % method,
  53. 'api_key': api_key,
  54. 'format': 'json',
  55. 'nojsoncallback': 1,
  56. }
  57. if secret:
  58. query['secret'] = secret
  59. data = self._download_json(self._API_BASE_URL + compat_urllib_parse_urlencode(query), video_id, note)
  60. if data['stat'] != 'ok':
  61. raise ExtractorError(data['message'])
  62. return data
  63. def _real_extract(self, url):
  64. video_id = self._match_id(url)
  65. api_key = self._download_json(
  66. 'https://www.flickr.com/hermes_error_beacon.gne', video_id,
  67. 'Downloading api key')['site_key']
  68. video_info = self._call_api(
  69. 'photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
  70. if video_info['media'] == 'video':
  71. streams = self._call_api(
  72. 'video.getStreamInfo', video_id, api_key,
  73. 'Downloading streams info', video_info['secret'])['streams']
  74. preference = qualities(
  75. ['288p', 'iphone_wifi', '100', '300', '700', '360p', 'appletv', '720p', '1080p', 'orig'])
  76. formats = []
  77. for stream in streams['stream']:
  78. stream_type = compat_str(stream.get('type'))
  79. formats.append({
  80. 'format_id': stream_type,
  81. 'url': stream['_content'],
  82. 'preference': preference(stream_type),
  83. })
  84. self._sort_formats(formats)
  85. owner = video_info.get('owner', {})
  86. uploader_id = owner.get('nsid')
  87. uploader_path = owner.get('path_alias') or uploader_id
  88. uploader_url = 'https://www.flickr.com/photos/%s/' % uploader_path if uploader_path else None
  89. return {
  90. 'id': video_id,
  91. 'title': video_info['title']['_content'],
  92. 'description': video_info.get('description', {}).get('_content'),
  93. 'formats': formats,
  94. 'timestamp': int_or_none(video_info.get('dateuploaded')),
  95. 'duration': int_or_none(video_info.get('video', {}).get('duration')),
  96. 'uploader_id': uploader_id,
  97. 'uploader': owner.get('realname'),
  98. 'uploader_url': uploader_url,
  99. 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
  100. 'view_count': int_or_none(video_info.get('views')),
  101. 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])],
  102. 'license': self._LICENSES.get(video_info.get('license')),
  103. }
  104. else:
  105. raise ExtractorError('not a video', expected=True)