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.

137 lines
4.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. limit_length,
  7. )
  8. class InstagramIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+)'
  10. _TESTS = [{
  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. 'url': 'https://instagram.com/p/-Cmh1cukG2/',
  22. 'only_matching': True,
  23. }]
  24. def _real_extract(self, url):
  25. video_id = self._match_id(url)
  26. webpage = self._download_webpage(url, video_id)
  27. uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
  28. webpage, 'uploader id', fatal=False)
  29. desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description',
  30. fatal=False)
  31. return {
  32. 'id': video_id,
  33. 'url': self._og_search_video_url(webpage, secure=False),
  34. 'ext': 'mp4',
  35. 'title': 'Video by %s' % uploader_id,
  36. 'thumbnail': self._og_search_thumbnail(webpage),
  37. 'uploader_id': uploader_id,
  38. 'description': desc,
  39. }
  40. class InstagramUserIE(InfoExtractor):
  41. _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  42. IE_DESC = 'Instagram user profile'
  43. IE_NAME = 'instagram:user'
  44. _TEST = {
  45. 'url': 'https://instagram.com/porsche',
  46. 'info_dict': {
  47. 'id': 'porsche',
  48. 'title': 'porsche',
  49. },
  50. 'playlist_mincount': 2,
  51. 'playlist': [{
  52. 'info_dict': {
  53. 'id': '614605558512799803_462752227',
  54. 'ext': 'mp4',
  55. 'title': '#Porsche Intelligent Performance.',
  56. 'thumbnail': 're:^https?://.*\.jpg',
  57. 'uploader': 'Porsche',
  58. 'uploader_id': 'porsche',
  59. 'timestamp': 1387486713,
  60. 'upload_date': '20131219',
  61. },
  62. }],
  63. 'params': {
  64. 'extract_flat': True,
  65. 'skip_download': True,
  66. }
  67. }
  68. def _real_extract(self, url):
  69. mobj = re.match(self._VALID_URL, url)
  70. uploader_id = mobj.group('username')
  71. entries = []
  72. page_count = 0
  73. media_url = 'http://instagram.com/%s/media' % uploader_id
  74. while True:
  75. page = self._download_json(
  76. media_url, uploader_id,
  77. note='Downloading page %d ' % (page_count + 1),
  78. )
  79. page_count += 1
  80. for it in page['items']:
  81. if it.get('type') != 'video':
  82. continue
  83. like_count = int_or_none(it.get('likes', {}).get('count'))
  84. user = it.get('user', {})
  85. formats = [{
  86. 'format_id': k,
  87. 'height': v.get('height'),
  88. 'width': v.get('width'),
  89. 'url': v['url'],
  90. } for k, v in it['videos'].items()]
  91. self._sort_formats(formats)
  92. thumbnails_el = it.get('images', {})
  93. thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
  94. # In some cases caption is null, which corresponds to None
  95. # in python. As a result, it.get('caption', {}) gives None
  96. title = (it.get('caption') or {}).get('text', it['id'])
  97. entries.append({
  98. 'id': it['id'],
  99. 'title': limit_length(title, 80),
  100. 'formats': formats,
  101. 'thumbnail': thumbnail,
  102. 'webpage_url': it.get('link'),
  103. 'uploader': user.get('full_name'),
  104. 'uploader_id': user.get('username'),
  105. 'like_count': like_count,
  106. 'timestamp': int_or_none(it.get('created_time')),
  107. })
  108. if not page['items']:
  109. break
  110. max_id = page['items'][-1]['id']
  111. media_url = (
  112. 'http://instagram.com/%s/media?max_id=%s' % (
  113. uploader_id, max_id))
  114. return {
  115. '_type': 'playlist',
  116. 'entries': entries,
  117. 'id': uploader_id,
  118. 'title': uploader_id,
  119. }