1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-14 07:02:34 +01:00

[packtpub] add support for authentication(closes #12622)

This commit is contained in:
Remita Amine 2017-05-09 11:14:29 +01:00
parent 8d65880e24
commit 5d0968f0af

View File

@ -3,7 +3,10 @@
import re import re
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import compat_str from ..compat import (
compat_str,
compat_HTTPError,
)
from ..utils import ( from ..utils import (
clean_html, clean_html,
ExtractorError, ExtractorError,
@ -11,6 +14,7 @@
strip_or_none, strip_or_none,
unified_timestamp, unified_timestamp,
urljoin, urljoin,
urlencode_postdata,
) )
@ -34,6 +38,32 @@ class PacktPubIE(PacktPubBaseIE):
'upload_date': '20170331', 'upload_date': '20170331',
}, },
} }
_NETRC_MACHINE = 'packtpub'
_TOKEN = None
def _real_initialize(self):
(username, password) = self._get_login_info()
if username is None:
return
webpage = self._download_webpage(self._PACKT_BASE, None)
login_form = self._form_hidden_inputs(
'packt-user-login-form', webpage)
login_form.update({
'email': username,
'password': password,
})
self._download_webpage(
self._PACKT_BASE, None, 'Logging in as %s' % username,
data=urlencode_postdata(login_form))
try:
self._TOKEN = self._download_json(
'%s/users/tokens/sessions' % self._MAPT_REST, None,
'Downloading Authorization Token')['data']['token']
except ExtractorError as e:
if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 404):
message = self._parse_json(e.cause.read().decode(), None)['message']
raise ExtractorError(message, expected=True)
raise
def _handle_error(self, response): def _handle_error(self, response):
if response.get('status') != 'success': if response.get('status') != 'success':
@ -51,14 +81,17 @@ def _real_extract(self, url):
course_id, chapter_id, video_id = mobj.group( course_id, chapter_id, video_id = mobj.group(
'course_id', 'chapter_id', 'id') 'course_id', 'chapter_id', 'id')
headers = {}
if self._TOKEN:
headers['Authorization'] = self._TOKEN
video = self._download_json( video = self._download_json(
'%s/users/me/products/%s/chapters/%s/sections/%s' '%s/users/me/products/%s/chapters/%s/sections/%s'
% (self._MAPT_REST, course_id, chapter_id, video_id), video_id, % (self._MAPT_REST, course_id, chapter_id, video_id), video_id,
'Downloading JSON video')['data'] 'Downloading JSON video', headers=headers)['data']
content = video.get('content') content = video.get('content')
if not content: if not content:
raise ExtractorError('This video is locked', expected=True) self.raise_login_required('This video is locked')
video_url = content['file'] video_url = content['file']