flickr.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import compat_urllib_parse
  4. from ..utils import (
  5. int_or_none,
  6. qualities,
  7. )
  8. class FlickrIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.|secure\.)?flickr\.com/photos/[\w\-_@]+/(?P<id>\d+)'
  10. _TEST = {
  11. 'url': 'http://www.flickr.com/photos/forestwander-nature-pictures/5645318632/in/photostream/',
  12. 'md5': '164fe3fa6c22e18d448d4d5af2330f31',
  13. 'info_dict': {
  14. 'id': '5645318632',
  15. 'ext': 'mpg',
  16. '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.',
  17. 'title': 'Dark Hollow Waterfalls',
  18. 'duration': 19,
  19. 'timestamp': 1303528740,
  20. 'upload_date': '20110423',
  21. 'uploader_id': '10922353@N03',
  22. 'uploader': 'Forest Wander',
  23. 'comment_count': int,
  24. }
  25. }
  26. _API_BASE_URL = 'https://api.flickr.com/services/rest?'
  27. def _call_api(self, method, video_id, api_key, note, secret=None):
  28. query = {
  29. 'photo_id': video_id,
  30. 'method': 'flickr.%s' % method,
  31. 'api_key': api_key,
  32. 'format': 'json',
  33. 'nojsoncallback': 1,
  34. }
  35. if secret:
  36. query['secret'] = secret
  37. return self._download_json(self._API_BASE_URL + compat_urllib_parse.urlencode(query), video_id, note)
  38. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. api_key = self._download_json('https://www.flickr.com/hermes_error_beacon.gne', video_id, 'Downloading api key',)['site_key']
  41. video_info = self._call_api('photos.getInfo', video_id, api_key, 'Downloading video info')['photo']
  42. if video_info['media'] == 'video':
  43. streams = self._call_api('video.getStreamInfo', video_id, api_key, 'Downloading streams info', video_info['secret'])['streams']
  44. preference = qualities(['iphone_wifi', '700', 'appletv', 'orig'])
  45. formats = []
  46. for stream in streams['stream']:
  47. stream_type = str(stream.get('type'))
  48. formats.append({
  49. 'format_id': stream_type,
  50. 'url': stream['_content'],
  51. 'preference': preference(stream_type),
  52. })
  53. self._sort_formats(formats)
  54. owner = video_info.get('owner', {})
  55. return {
  56. 'id': video_id,
  57. 'title': video_info['title']['_content'],
  58. 'description': video_info.get('description', {}).get('_content'),
  59. 'formats': formats,
  60. 'timestamp': int_or_none(video_info.get('dateuploaded')),
  61. 'duration': int_or_none(video_info.get('video', {}).get('duration')),
  62. 'uploader_id': owner.get('nsid'),
  63. 'uploader': owner.get('realname'),
  64. 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
  65. }