]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/adobepass.py
9e3a3e3620152390bffe996a542697f6f01736af
[youtubedl] / youtube_dl / extractor / adobepass.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import xml.etree.ElementTree as etree
7
8 from .common import InfoExtractor
9 from ..utils import (
10 unescapeHTML,
11 urlencode_postdata,
12 unified_timestamp,
13 )
14
15
16 class AdobePassIE(InfoExtractor):
17 _SERVICE_PROVIDER_TEMPLATE = 'https://sp.auth.adobe.com/adobe-services/%s'
18 _USER_AGENT = 'Mozilla/5.0 (X11; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0'
19
20 @staticmethod
21 def _get_mvpd_resource(provider_id, title, guid, rating):
22 channel = etree.Element('channel')
23 channel_title = etree.SubElement(channel, 'title')
24 channel_title.text = provider_id
25 item = etree.SubElement(channel, 'item')
26 resource_title = etree.SubElement(item, 'title')
27 resource_title.text = title
28 resource_guid = etree.SubElement(item, 'guid')
29 resource_guid.text = guid
30 resource_rating = etree.SubElement(item, 'media:rating')
31 resource_rating.attrib = {'scheme': 'urn:v-chip'}
32 resource_rating.text = rating
33 return '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">' + etree.tostring(channel).decode() + '</rss>'
34
35 def _extract_mvpd_auth(self, url, video_id, requestor_id, resource):
36 def xml_text(xml_str, tag):
37 return self._search_regex(
38 '<%s>(.+?)</%s>' % (tag, tag), xml_str, tag)
39
40 mvpd_headers = {
41 'ap_42': 'anonymous',
42 'ap_11': 'Linux i686',
43 'ap_z': self._USER_AGENT,
44 'User-Agent': self._USER_AGENT,
45 }
46
47 guid = xml_text(resource, 'guid')
48 requestor_info = self._downloader.cache.load('mvpd', requestor_id) or {}
49 authn_token = requestor_info.get('authn_token')
50 if authn_token:
51 token_expires = unified_timestamp(re.sub(r'[_ ]GMT', '', xml_text(authn_token, 'simpleTokenExpires')))
52 if token_expires and token_expires <= int(time.time()):
53 authn_token = None
54 requestor_info = {}
55 if not authn_token:
56 # TODO add support for other TV Providers
57 mso_id = 'DTV'
58 username, password = self._get_netrc_login_info(mso_id)
59 if not username or not password:
60 return ''
61
62 def post_form(form_page, note, data={}):
63 post_url = self._html_search_regex(r'<form[^>]+action=(["\'])(?P<url>.+?)\1', form_page, 'post url', group='url')
64 return self._download_webpage(
65 post_url, video_id, note, data=urlencode_postdata(data or self._hidden_inputs(form_page)), headers={
66 'Content-Type': 'application/x-www-form-urlencoded',
67 })
68
69 provider_redirect_page = self._download_webpage(
70 self._SERVICE_PROVIDER_TEMPLATE % 'authenticate/saml', video_id,
71 'Downloading Provider Redirect Page', query={
72 'noflash': 'true',
73 'mso_id': mso_id,
74 'requestor_id': requestor_id,
75 'no_iframe': 'false',
76 'domain_name': 'adobe.com',
77 'redirect_url': url,
78 })
79 provider_login_page = post_form(
80 provider_redirect_page, 'Downloading Provider Login Page')
81 mvpd_confirm_page = post_form(provider_login_page, 'Logging in', {
82 'username': username,
83 'password': password,
84 })
85 post_form(mvpd_confirm_page, 'Confirming Login')
86
87 session = self._download_webpage(
88 self._SERVICE_PROVIDER_TEMPLATE % 'session', video_id,
89 'Retrieving Session', data=urlencode_postdata({
90 '_method': 'GET',
91 'requestor_id': requestor_id,
92 }), headers=mvpd_headers)
93 if '<pendingLogout' in session:
94 self._downloader.cache.store('mvpd', requestor_id, {})
95 return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
96 authn_token = unescapeHTML(xml_text(session, 'authnToken'))
97 requestor_info['authn_token'] = authn_token
98 self._downloader.cache.store('mvpd', requestor_id, requestor_info)
99
100 authz_token = requestor_info.get(guid)
101 if not authz_token:
102 authorize = self._download_webpage(
103 self._SERVICE_PROVIDER_TEMPLATE % 'authorize', video_id,
104 'Retrieving Authorization Token', data=urlencode_postdata({
105 'resource_id': resource,
106 'requestor_id': requestor_id,
107 'authentication_token': authn_token,
108 'mso_id': xml_text(authn_token, 'simpleTokenMsoID'),
109 'userMeta': '1',
110 }), headers=mvpd_headers)
111 if '<pendingLogout' in authorize:
112 self._downloader.cache.store('mvpd', requestor_id, {})
113 return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
114 authz_token = unescapeHTML(xml_text(authorize, 'authzToken'))
115 requestor_info[guid] = authz_token
116 self._downloader.cache.store('mvpd', requestor_id, requestor_info)
117
118 mvpd_headers.update({
119 'ap_19': xml_text(authn_token, 'simpleSamlNameID'),
120 'ap_23': xml_text(authn_token, 'simpleSamlSessionIndex'),
121 })
122
123 short_authorize = self._download_webpage(
124 self._SERVICE_PROVIDER_TEMPLATE % 'shortAuthorize',
125 video_id, 'Retrieving Media Token', data=urlencode_postdata({
126 'authz_token': authz_token,
127 'requestor_id': requestor_id,
128 'session_guid': xml_text(authn_token, 'simpleTokenAuthenticationGuid'),
129 'hashed_guid': 'false',
130 }), headers=mvpd_headers)
131 if '<pendingLogout' in short_authorize:
132 self._downloader.cache.store('mvpd', requestor_id, {})
133 return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
134 return short_authorize