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.

226 lines
7.6 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. try_get,
  10. )
  11. class InstagramIE(InfoExtractor):
  12. _VALID_URL = r'(?P<url>https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+))'
  13. _TESTS = [{
  14. 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
  15. 'md5': '0d2da106a9d2631273e192b372806516',
  16. 'info_dict': {
  17. 'id': 'aye83DjauH',
  18. 'ext': 'mp4',
  19. 'title': 'Video by naomipq',
  20. 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
  21. 'thumbnail': 're:^https?://.*\.jpg',
  22. 'timestamp': 1371748545,
  23. 'upload_date': '20130620',
  24. 'uploader_id': 'naomipq',
  25. 'uploader': 'Naomi Leonor Phan-Quang',
  26. 'like_count': int,
  27. 'comment_count': int,
  28. },
  29. }, {
  30. # missing description
  31. 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
  32. 'info_dict': {
  33. 'id': 'BA-pQFBG8HZ',
  34. 'ext': 'mp4',
  35. 'uploader_id': 'britneyspears',
  36. 'title': 'Video by britneyspears',
  37. 'thumbnail': 're:^https?://.*\.jpg',
  38. 'timestamp': 1453760977,
  39. 'upload_date': '20160125',
  40. 'uploader_id': 'britneyspears',
  41. 'uploader': 'Britney Spears',
  42. 'like_count': int,
  43. 'comment_count': int,
  44. },
  45. 'params': {
  46. 'skip_download': True,
  47. },
  48. }, {
  49. 'url': 'https://instagram.com/p/-Cmh1cukG2/',
  50. 'only_matching': True,
  51. }, {
  52. 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
  53. 'only_matching': True,
  54. }]
  55. @staticmethod
  56. def _extract_embed_url(webpage):
  57. mobj = re.search(
  58. r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?instagram\.com/p/[^/]+/embed.*?)\1',
  59. webpage)
  60. if mobj:
  61. return mobj.group('url')
  62. blockquote_el = get_element_by_attribute(
  63. 'class', 'instagram-media', webpage)
  64. if blockquote_el is None:
  65. return
  66. mobj = re.search(
  67. r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1', blockquote_el)
  68. if mobj:
  69. return mobj.group('link')
  70. def _real_extract(self, url):
  71. mobj = re.match(self._VALID_URL, url)
  72. video_id = mobj.group('id')
  73. url = mobj.group('url')
  74. webpage = self._download_webpage(url, video_id)
  75. (video_url, description, thumbnail, timestamp, uploader,
  76. uploader_id, like_count, comment_count) = [None] * 8
  77. shared_data = self._parse_json(
  78. self._search_regex(
  79. r'window\._sharedData\s*=\s*({.+?});',
  80. webpage, 'shared data', default='{}'),
  81. video_id, fatal=False)
  82. if shared_data:
  83. media = try_get(
  84. shared_data, lambda x: x['entry_data']['PostPage'][0]['media'], dict)
  85. if media:
  86. video_url = media.get('video_url')
  87. description = media.get('caption')
  88. thumbnail = media.get('display_src')
  89. timestamp = int_or_none(media.get('date'))
  90. uploader = media.get('owner', {}).get('full_name')
  91. uploader_id = media.get('owner', {}).get('username')
  92. like_count = int_or_none(media.get('likes', {}).get('count'))
  93. comment_count = int_or_none(media.get('comments', {}).get('count'))
  94. if not video_url:
  95. video_url = self._og_search_video_url(webpage, secure=False)
  96. if not uploader_id:
  97. uploader_id = self._search_regex(
  98. r'"owner"\s*:\s*{\s*"username"\s*:\s*"(.+?)"',
  99. webpage, 'uploader id', fatal=False)
  100. if not description:
  101. description = self._search_regex(
  102. r'"caption"\s*:\s*"(.+?)"', webpage, 'description', default=None)
  103. if description is not None:
  104. description = lowercase_escape(description)
  105. if not thumbnail:
  106. thumbnail = self._og_search_thumbnail(webpage)
  107. return {
  108. 'id': video_id,
  109. 'url': video_url,
  110. 'ext': 'mp4',
  111. 'title': 'Video by %s' % uploader_id,
  112. 'description': description,
  113. 'thumbnail': thumbnail,
  114. 'timestamp': timestamp,
  115. 'uploader_id': uploader_id,
  116. 'uploader': uploader,
  117. 'like_count': like_count,
  118. 'comment_count': comment_count,
  119. }
  120. class InstagramUserIE(InfoExtractor):
  121. _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  122. IE_DESC = 'Instagram user profile'
  123. IE_NAME = 'instagram:user'
  124. _TEST = {
  125. 'url': 'https://instagram.com/porsche',
  126. 'info_dict': {
  127. 'id': 'porsche',
  128. 'title': 'porsche',
  129. },
  130. 'playlist_mincount': 2,
  131. 'playlist': [{
  132. 'info_dict': {
  133. 'id': '614605558512799803_462752227',
  134. 'ext': 'mp4',
  135. 'title': '#Porsche Intelligent Performance.',
  136. 'thumbnail': 're:^https?://.*\.jpg',
  137. 'uploader': 'Porsche',
  138. 'uploader_id': 'porsche',
  139. 'timestamp': 1387486713,
  140. 'upload_date': '20131219',
  141. },
  142. }],
  143. 'params': {
  144. 'extract_flat': True,
  145. 'skip_download': True,
  146. }
  147. }
  148. def _real_extract(self, url):
  149. mobj = re.match(self._VALID_URL, url)
  150. uploader_id = mobj.group('username')
  151. entries = []
  152. page_count = 0
  153. media_url = 'http://instagram.com/%s/media' % uploader_id
  154. while True:
  155. page = self._download_json(
  156. media_url, uploader_id,
  157. note='Downloading page %d ' % (page_count + 1),
  158. )
  159. page_count += 1
  160. for it in page['items']:
  161. if it.get('type') != 'video':
  162. continue
  163. like_count = int_or_none(it.get('likes', {}).get('count'))
  164. user = it.get('user', {})
  165. formats = [{
  166. 'format_id': k,
  167. 'height': v.get('height'),
  168. 'width': v.get('width'),
  169. 'url': v['url'],
  170. } for k, v in it['videos'].items()]
  171. self._sort_formats(formats)
  172. thumbnails_el = it.get('images', {})
  173. thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
  174. # In some cases caption is null, which corresponds to None
  175. # in python. As a result, it.get('caption', {}) gives None
  176. title = (it.get('caption') or {}).get('text', it['id'])
  177. entries.append({
  178. 'id': it['id'],
  179. 'title': limit_length(title, 80),
  180. 'formats': formats,
  181. 'thumbnail': thumbnail,
  182. 'webpage_url': it.get('link'),
  183. 'uploader': user.get('full_name'),
  184. 'uploader_id': user.get('username'),
  185. 'like_count': like_count,
  186. 'timestamp': int_or_none(it.get('created_time')),
  187. })
  188. if not page['items']:
  189. break
  190. max_id = page['items'][-1]['id'].split('_')[0]
  191. media_url = (
  192. 'http://instagram.com/%s/media?max_id=%s' % (
  193. uploader_id, max_id))
  194. return {
  195. '_type': 'playlist',
  196. 'entries': entries,
  197. 'id': uploader_id,
  198. 'title': uploader_id,
  199. }