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.

107 lines
3.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. )
  7. class InstagramIE(InfoExtractor):
  8. _VALID_URL = r'http://instagram\.com/p/(?P<id>.*?)/'
  9. _TEST = {
  10. 'url': 'http://instagram.com/p/aye83DjauH/?foo=bar#abc',
  11. 'md5': '0d2da106a9d2631273e192b372806516',
  12. 'info_dict': {
  13. 'id': 'aye83DjauH',
  14. 'ext': 'mp4',
  15. 'uploader_id': 'naomipq',
  16. 'title': 'Video by naomipq',
  17. 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
  18. }
  19. }
  20. def _real_extract(self, url):
  21. mobj = re.match(self._VALID_URL, url)
  22. video_id = mobj.group('id')
  23. webpage = self._download_webpage(url, video_id)
  24. uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
  25. webpage, 'uploader id', fatal=False)
  26. desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description',
  27. fatal=False)
  28. return {
  29. 'id': video_id,
  30. 'url': self._og_search_video_url(webpage, secure=False),
  31. 'ext': 'mp4',
  32. 'title': 'Video by %s' % uploader_id,
  33. 'thumbnail': self._og_search_thumbnail(webpage),
  34. 'uploader_id': uploader_id,
  35. 'description': desc,
  36. }
  37. class InstagramUserIE(InfoExtractor):
  38. _VALID_URL = r'http://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  39. IE_DESC = 'Instagram user profile'
  40. IE_NAME = 'instagram:user'
  41. def _real_extract(self, url):
  42. mobj = re.match(self._VALID_URL, url)
  43. uploader_id = mobj.group('username')
  44. entries = []
  45. page_count = 0
  46. media_url = 'http://instagram.com/%s/media' % uploader_id
  47. while True:
  48. page = self._download_json(
  49. media_url, uploader_id,
  50. note='Downloading page %d ' % (page_count + 1),
  51. )
  52. page_count += 1
  53. for it in page['items']:
  54. if it.get('type') != 'video':
  55. continue
  56. like_count = int_or_none(it.get('likes', {}).get('count'))
  57. user = it.get('user', {})
  58. formats = [{
  59. 'format_id': k,
  60. 'height': v.get('height'),
  61. 'width': v.get('width'),
  62. 'url': v['url'],
  63. } for k, v in it['videos'].items()]
  64. self._sort_formats(formats)
  65. thumbnails_el = it.get('images', {})
  66. thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
  67. title = it.get('caption', {}).get('text', it['id'])
  68. entries.append({
  69. 'id': it['id'],
  70. 'title': title,
  71. 'formats': formats,
  72. 'thumbnail': thumbnail,
  73. 'webpage_url': it.get('link'),
  74. 'uploader': user.get('full_name'),
  75. 'uploader_id': user.get('username'),
  76. 'like_count': like_count,
  77. 'timestamp': int_or_none(it.get('created_time')),
  78. })
  79. if not page['items']:
  80. break
  81. max_id = page['items'][-1]['id']
  82. media_url = (
  83. 'http://instagram.com/%s/media?max_id=%s' % (
  84. uploader_id, max_id))
  85. return {
  86. '_type': 'playlist',
  87. 'entries': entries,
  88. 'id': uploader_id,
  89. 'title': uploader_id,
  90. }