2
0

flickr.py 4.3 KB

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