flickr.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. format_field,
  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': f'flickr.{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 + 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. 'quality': preference(stream_type),
  80. })
  81. owner = video_info.get('owner', {})
  82. uploader_id = owner.get('nsid')
  83. uploader_path = owner.get('path_alias') or uploader_id
  84. uploader_url = format_field(uploader_path, None, 'https://www.flickr.com/photos/%s/')
  85. return {
  86. 'id': video_id,
  87. 'title': video_info['title']['_content'],
  88. 'description': video_info.get('description', {}).get('_content'),
  89. 'formats': formats,
  90. 'timestamp': int_or_none(video_info.get('dateuploaded')),
  91. 'duration': int_or_none(video_info.get('video', {}).get('duration')),
  92. 'uploader_id': uploader_id,
  93. 'uploader': owner.get('realname'),
  94. 'uploader_url': uploader_url,
  95. 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
  96. 'view_count': int_or_none(video_info.get('views')),
  97. 'tags': [tag.get('_content') for tag in video_info.get('tags', {}).get('tag', [])],
  98. 'license': self._LICENSES.get(video_info.get('license')),
  99. }
  100. else:
  101. raise ExtractorError('not a video', expected=True)