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.

113 lines
4.6 KiB

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