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.

134 lines
4.4 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. limit_length,
  7. )
  8. class InstagramIE(InfoExtractor):
  9. _VALID_URL = r'https://instagram\.com/p/(?P<id>[\da-zA-Z]+)'
  10. _TEST = {
  11. 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
  12. 'md5': '0d2da106a9d2631273e192b372806516',
  13. 'info_dict': {
  14. 'id': 'aye83DjauH',
  15. 'ext': 'mp4',
  16. 'uploader_id': 'naomipq',
  17. 'title': 'Video by naomipq',
  18. 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
  19. }
  20. }
  21. def _real_extract(self, url):
  22. video_id = self._match_id(url)
  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'https://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  39. IE_DESC = 'Instagram user profile'
  40. IE_NAME = 'instagram:user'
  41. _TEST = {
  42. 'url': 'https://instagram.com/porsche',
  43. 'info_dict': {
  44. 'id': 'porsche',
  45. 'title': 'porsche',
  46. },
  47. 'playlist_mincount': 2,
  48. 'playlist': [{
  49. 'info_dict': {
  50. 'id': '614605558512799803_462752227',
  51. 'ext': 'mp4',
  52. 'title': '#Porsche Intelligent Performance.',
  53. 'thumbnail': 're:^https?://.*\.jpg',
  54. 'uploader': 'Porsche',
  55. 'uploader_id': 'porsche',
  56. 'timestamp': 1387486713,
  57. 'upload_date': '20131219',
  58. },
  59. }],
  60. 'params': {
  61. 'extract_flat': True,
  62. 'skip_download': True,
  63. }
  64. }
  65. def _real_extract(self, url):
  66. mobj = re.match(self._VALID_URL, url)
  67. uploader_id = mobj.group('username')
  68. entries = []
  69. page_count = 0
  70. media_url = 'http://instagram.com/%s/media' % uploader_id
  71. while True:
  72. page = self._download_json(
  73. media_url, uploader_id,
  74. note='Downloading page %d ' % (page_count + 1),
  75. )
  76. page_count += 1
  77. for it in page['items']:
  78. if it.get('type') != 'video':
  79. continue
  80. like_count = int_or_none(it.get('likes', {}).get('count'))
  81. user = it.get('user', {})
  82. formats = [{
  83. 'format_id': k,
  84. 'height': v.get('height'),
  85. 'width': v.get('width'),
  86. 'url': v['url'],
  87. } for k, v in it['videos'].items()]
  88. self._sort_formats(formats)
  89. thumbnails_el = it.get('images', {})
  90. thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
  91. # In some cases caption is null, which corresponds to None
  92. # in python. As a result, it.get('caption', {}) gives None
  93. title = (it.get('caption') or {}).get('text', it['id'])
  94. entries.append({
  95. 'id': it['id'],
  96. 'title': limit_length(title, 80),
  97. 'formats': formats,
  98. 'thumbnail': thumbnail,
  99. 'webpage_url': it.get('link'),
  100. 'uploader': user.get('full_name'),
  101. 'uploader_id': user.get('username'),
  102. 'like_count': like_count,
  103. 'timestamp': int_or_none(it.get('created_time')),
  104. })
  105. if not page['items']:
  106. break
  107. max_id = page['items'][-1]['id']
  108. media_url = (
  109. 'http://instagram.com/%s/media?max_id=%s' % (
  110. uploader_id, max_id))
  111. return {
  112. '_type': 'playlist',
  113. 'entries': entries,
  114. 'id': uploader_id,
  115. 'title': uploader_id,
  116. }