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.

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