flickr.py 3.0 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. 'uploader_id': 'forestwander-nature-pictures',
  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. }
  26. }
  27. _API_BASE_URL = 'https://api.flickr.com/services/rest?'
  28. _API_KEY = '61b16865f916058e63580a912d9143be'
  29. def _call_api(self, method, video_id, secret=None):
  30. query = {
  31. 'photo_id': video_id,
  32. 'method': 'flickr.%s' % method,
  33. 'api_key': self._API_KEY,
  34. 'format': 'json',
  35. 'nojsoncallback': 1,
  36. }
  37. if secret:
  38. query['secret'] = secret
  39. return self._download_json(self._API_BASE_URL + compat_urllib_parse.urlencode(query), video_id)
  40. def _real_extract(self, url):
  41. video_id = self._match_id(url)
  42. video_info = self._call_api('photos.getInfo', video_id)['photo']
  43. if video_info['media'] == 'video':
  44. streams = self._call_api('video.getStreamInfo', video_id, video_info['secret'])['streams']
  45. preference = qualities(['iphone_wifi', '700', 'appletv', 'orig'])
  46. formats = []
  47. for stream in streams['stream']:
  48. stream_type = str(stream.get('type'))
  49. formats.append({
  50. 'format_id': stream_type,
  51. 'url': stream['_content'],
  52. 'preference': preference(stream_type),
  53. })
  54. self._sort_formats(formats)
  55. owner = video_info.get('owner', {})
  56. return {
  57. 'id': video_id,
  58. 'title': video_info['title']['_content'],
  59. 'description': video_info.get('description', {}).get('_content'),
  60. 'formats': formats,
  61. 'timestamp': int_or_none(video_info.get('dateuploaded')),
  62. 'duration': int_or_none(video_info.get('video', {}).get('duration')),
  63. 'uploader_id': owner.get('nsid'),
  64. 'uploader': owner.get('realname'),
  65. 'comment_count': int_or_none(video_info.get('comments', {}).get('_content')),
  66. }