Skip to content

Commit

Permalink
[Instagram] Add login (yt-dlp#1288)
Browse files Browse the repository at this point in the history
Authored by: u-spec-png
  • Loading branch information
u-spec-png authored Oct 22, 2021
1 parent f656a23 commit ab2ffab
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion yt_dlp/extractor/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import json
import re
import time

from .common import InfoExtractor
from ..compat import (
Expand All @@ -20,11 +21,13 @@
try_get,
url_or_none,
variadic,
urlencode_postdata,
)


class InstagramIE(InfoExtractor):
_VALID_URL = r'(?P<url>https?://(?:www\.)?instagram\.com/(?:p|tv|reel)/(?P<id>[^/?#&]+))'
_NETRC_MACHINE = 'instagram'
_TESTS = [{
'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
'md5': '0d2da106a9d2631273e192b372806516',
Expand Down Expand Up @@ -140,14 +143,53 @@ def _extract_embed_url(webpage):
if mobj:
return mobj.group('link')

def _login(self):
username, password = self._get_login_info()

login_webpage = self._download_webpage(
'https://www.instagram.com/accounts/login/', None,
note='Downloading login webpage', errnote='Failed to download login webpage')

shared_data = self._parse_json(
self._search_regex(
r'window\._sharedData\s*=\s*({.+?});',
login_webpage, 'shared data', default='{}'),
None)

login = self._download_json('https://www.instagram.com/accounts/login/ajax/', None, note='Logging in', headers={
'Accept': '*/*',
'X-IG-App-ID': '936619743392459',
'X-ASBD-ID': '198387',
'X-IG-WWW-Claim': '0',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRFToken': shared_data['config']['csrf_token'],
'X-Instagram-AJAX': shared_data['rollout_hash'],
'Referer': 'https://www.instagram.com/',
}, data=urlencode_postdata({
'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{int(time.time())}:{password}',
'username': username,
'queryParams': '{}',
'optIntoOneTap': 'false',
'stopDeletionNonce': '',
'trustedDeviceRecords': '{}',
}))

if not login.get('authenticated'):
if login.get('message'):
raise ExtractorError(f'Unable to login: {login["message"]}')
raise ExtractorError('Unable to login')

def _real_initialize(self):
self._login()

def _real_extract(self, url):
mobj = self._match_valid_url(url)
video_id = mobj.group('id')
url = mobj.group('url')

webpage, urlh = self._download_webpage_handle(url, video_id)
if 'www.instagram.com/accounts/login' in urlh.geturl().rstrip('/'):
self.raise_login_required('You need to log in to access this content', method='cookies')
self.raise_login_required('You need to log in to access this content')

(media, video_url, description, thumbnail, timestamp, uploader,
uploader_id, like_count, comment_count, comments, height,
Expand Down

0 comments on commit ab2ffab

Please sign in to comment.