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.

131 lines
4.2 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. )
  7. class InstagramIE(InfoExtractor):
  8. _VALID_URL = r'http://instagram\.com/p/(?P<id>.*?)/'
  9. _TEST = {
  10. 'url': 'http://instagram.com/p/aye83DjauH/?foo=bar#abc',
  11. 'md5': '0d2da106a9d2631273e192b372806516',
  12. 'info_dict': {
  13. 'id': 'aye83DjauH',
  14. 'ext': 'mp4',
  15. 'uploader_id': 'naomipq',
  16. 'title': 'Video by naomipq',
  17. 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
  18. }
  19. }
  20. def _real_extract(self, url):
  21. mobj = re.match(self._VALID_URL, url)
  22. video_id = mobj.group('id')
  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'http://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
  39. IE_DESC = 'Instagram user profile'
  40. IE_NAME = 'instagram:user'
  41. _TEST = {
  42. 'url': 'http://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. title = it.get('caption', {}).get('text', it['id'])
  92. entries.append({
  93. 'id': it['id'],
  94. 'title': title,
  95. 'formats': formats,
  96. 'thumbnail': thumbnail,
  97. 'webpage_url': it.get('link'),
  98. 'uploader': user.get('full_name'),
  99. 'uploader_id': user.get('username'),
  100. 'like_count': like_count,
  101. 'timestamp': int_or_none(it.get('created_time')),
  102. })
  103. if not page['items']:
  104. break
  105. max_id = page['items'][-1]['id']
  106. media_url = (
  107. 'http://instagram.com/%s/media?max_id=%s' % (
  108. uploader_id, max_id))
  109. return {
  110. '_type': 'playlist',
  111. 'entries': entries,
  112. 'id': uploader_id,
  113. 'title': uploader_id,
  114. }