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.

165 lines
5.3 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. get_element_by_attribute,
  6. int_or_none,
  7. limit_length,
  8. lowercase_escape,
  9. )
  10. class InstagramIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+)'
  12. _TESTS = [{
  13. 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
  14. 'md5': '0d2da106a9d2631273e192b372806516',
  15. 'info_dict': {
  16. 'id': 'aye83DjauH',
  17. 'ext': 'mp4',
  18. 'uploader_id': 'naomipq',
  19. 'title': 'Video by naomipq',
  20. 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
  21. }
  22. }, {
  23. # missing description
  24. 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
  25. 'info_dict': {
  26. 'id': 'BA-pQFBG8HZ',
  27. 'ext': 'mp4',
  28. 'uploader_id': 'britneyspears',
  29. 'title': 'Video by britneyspears',
  30. },
  31. 'params': {
  32. 'skip_download': True,
  33. },
  34. }, {
  35. 'url': 'https://instagram.com/p/-Cmh1cukG2/',
  36. 'only_matching': True,
  37. }]
  38. @staticmethod
  39. def _extract_embed_url(webpage):
  40. blockquote_el = get_element_by_attribute(
  41. 'class', 'instagram-media', webpage)
  42. if blockquote_el is None:
  43. return
  44. mobj = re.search(
  45. r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1', blockquote_el)
  46. if mobj:
  47. return mobj.group('link')
  48. def _real_extract(self, url):
  49. video_id = self._match_id(url)
  50. webpage = self._download_webpage(url, video_id)
  51. uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
  52. webpage, 'uploader id', fatal=False)
  53. desc = self._search_regex(
  54. r'"caption":"(.+?)"', webpage, 'description', default=None)
  55. if desc is not None:
  56. desc = lowercase_escape(desc)
  57. return {
  58. 'id': video_id,
  59. 'url': self._og_search_video_url(webpage, secure=False),
  60. 'ext': 'mp4',
  61. 'title': 'Video by %s' % uploader_id,
  62. 'thumbnail': self._og_search_thumbnail(webpage),
  63. 'uploader_id': uploader_id,
  64. 'description': desc,
  65. }
  66. class InstagramUserIE(InfoExtractor):
  67. _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  68. IE_DESC = 'Instagram user profile'
  69. IE_NAME = 'instagram:user'
  70. _TEST = {
  71. 'url': 'https://instagram.com/porsche',
  72. 'info_dict': {
  73. 'id': 'porsche',
  74. 'title': 'porsche',
  75. },
  76. 'playlist_mincount': 2,
  77. 'playlist': [{
  78. 'info_dict': {
  79. 'id': '614605558512799803_462752227',
  80. 'ext': 'mp4',
  81. 'title': '#Porsche Intelligent Performance.',
  82. 'thumbnail': 're:^https?://.*\.jpg',
  83. 'uploader': 'Porsche',
  84. 'uploader_id': 'porsche',
  85. 'timestamp': 1387486713,
  86. 'upload_date': '20131219',
  87. },
  88. }],
  89. 'params': {
  90. 'extract_flat': True,
  91. 'skip_download': True,
  92. }
  93. }
  94. def _real_extract(self, url):
  95. mobj = re.match(self._VALID_URL, url)
  96. uploader_id = mobj.group('username')
  97. entries = []
  98. page_count = 0
  99. media_url = 'http://instagram.com/%s/media' % uploader_id
  100. while True:
  101. page = self._download_json(
  102. media_url, uploader_id,
  103. note='Downloading page %d ' % (page_count + 1),
  104. )
  105. page_count += 1
  106. for it in page['items']:
  107. if it.get('type') != 'video':
  108. continue
  109. like_count = int_or_none(it.get('likes', {}).get('count'))
  110. user = it.get('user', {})
  111. formats = [{
  112. 'format_id': k,
  113. 'height': v.get('height'),
  114. 'width': v.get('width'),
  115. 'url': v['url'],
  116. } for k, v in it['videos'].items()]
  117. self._sort_formats(formats)
  118. thumbnails_el = it.get('images', {})
  119. thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
  120. # In some cases caption is null, which corresponds to None
  121. # in python. As a result, it.get('caption', {}) gives None
  122. title = (it.get('caption') or {}).get('text', it['id'])
  123. entries.append({
  124. 'id': it['id'],
  125. 'title': limit_length(title, 80),
  126. 'formats': formats,
  127. 'thumbnail': thumbnail,
  128. 'webpage_url': it.get('link'),
  129. 'uploader': user.get('full_name'),
  130. 'uploader_id': user.get('username'),
  131. 'like_count': like_count,
  132. 'timestamp': int_or_none(it.get('created_time')),
  133. })
  134. if not page['items']:
  135. break
  136. max_id = page['items'][-1]['id'].split('_')[0]
  137. media_url = (
  138. 'http://instagram.com/%s/media?max_id=%s' % (
  139. uploader_id, max_id))
  140. return {
  141. '_type': 'playlist',
  142. 'entries': entries,
  143. 'id': uploader_id,
  144. 'title': uploader_id,
  145. }