]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/atresplayer.py
[youtube] Improve signature function detection (#641)
[yt-dlp.git] / yt_dlp / extractor / atresplayer.py
CommitLineData
00011575 1# coding: utf-8
a3498732
S
2from __future__ import unicode_literals
3
61ebb401 4import re
a3498732 5
6b597516 6from .common import InfoExtractor
6d394a66 7from ..compat import compat_HTTPError
c8dfe360 8from ..utils import (
6e6bc8da 9 ExtractorError,
6e6bc8da 10 int_or_none,
6e6bc8da 11 urlencode_postdata,
a3498732
S
12)
13
14
6b597516 15class AtresPlayerIE(InfoExtractor):
6d394a66 16 _VALID_URL = r'https?://(?:www\.)?atresplayer\.com/[^/]+/[^/]+/[^/]+/[^/]+/(?P<display_id>.+?)_(?P<id>[0-9a-f]{24})'
499bfcbf 17 _NETRC_MACHINE = 'atresplayer'
a3498732
S
18 _TESTS = [
19 {
6d394a66 20 'url': 'https://www.atresplayer.com/antena3/series/pequenas-coincidencias/temporada-1/capitulo-7-asuntos-pendientes_5d4aa2c57ed1a88fc715a615/',
a3498732 21 'info_dict': {
6d394a66 22 'id': '5d4aa2c57ed1a88fc715a615',
a3498732 23 'ext': 'mp4',
6d394a66
RA
24 'title': 'Capítulo 7: Asuntos pendientes',
25 'description': 'md5:7634cdcb4d50d5381bedf93efb537fbc',
26 'duration': 3413,
27 },
28 'params': {
29 'format': 'bestvideo',
a3498732 30 },
61ebb401 31 'skip': 'This video is only available for registered users'
32 },
33 {
6d394a66
RA
34 'url': 'https://www.atresplayer.com/lasexta/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_5ad08edf986b2855ed47adc4/',
35 'only_matching': True,
a3498732
S
36 },
37 {
6d394a66 38 'url': 'https://www.atresplayer.com/antena3/series/el-secreto-de-puente-viejo/el-chico-de-los-tres-lunares/capitulo-977-29-12-14_5ad51046986b2886722ccdea/',
a3498732
S
39 'only_matching': True,
40 },
41 ]
6d394a66 42 _API_BASE = 'https://api.atresplayer.com/'
61ebb401 43
c8dfe360
S
44 def _real_initialize(self):
45 self._login()
46
6d394a66
RA
47 def _handle_error(self, e, code):
48 if isinstance(e.cause, compat_HTTPError) and e.cause.code == code:
49 error = self._parse_json(e.cause.read(), None)
50 if error.get('error') == 'required_registered':
51 self.raise_login_required()
52 raise ExtractorError(error['error_description'], expected=True)
53 raise
54
c8dfe360 55 def _login(self):
68217024 56 username, password = self._get_login_info()
c8dfe360
S
57 if username is None:
58 return
59
6d394a66
RA
60 self._request_webpage(
61 self._API_BASE + 'login', None, 'Downloading login page')
c8dfe360 62
6d394a66
RA
63 try:
64 target_url = self._download_json(
65 'https://account.atresmedia.com/api/login', None,
66 'Logging in', headers={
67 'Content-Type': 'application/x-www-form-urlencoded'
68 }, data=urlencode_postdata({
69 'username': username,
70 'password': password,
71 }))['targetUrl']
72 except ExtractorError as e:
73 self._handle_error(e, 400)
c8dfe360 74
6d394a66 75 self._request_webpage(target_url, None, 'Following Target URL')
c8dfe360 76
a3498732 77 def _real_extract(self, url):
6d394a66 78 display_id, video_id = re.match(self._VALID_URL, url).groups()
a3498732 79
6d394a66
RA
80 try:
81 episode = self._download_json(
82 self._API_BASE + 'client/v1/player/episode/' + video_id, video_id)
83 except ExtractorError as e:
84 self._handle_error(e, 403)
a3498732 85
6d394a66 86 title = episode['titulo']
61ebb401 87
88 formats = []
c811e8d8 89 subtitles = {}
6d394a66
RA
90 for source in episode.get('sources', []):
91 src = source.get('src')
92 if not src:
61ebb401 93 continue
6d394a66
RA
94 src_type = source.get('type')
95 if src_type == 'application/vnd.apple.mpegurl':
c811e8d8 96 formats, subtitles = self._extract_m3u8_formats(
6d394a66 97 src, video_id, 'mp4', 'm3u8_native',
c811e8d8 98 m3u8_id='hls', fatal=False)
6d394a66 99 elif src_type == 'application/dash+xml':
c811e8d8
F
100 formats, subtitles = self._extract_mpd_formats(
101 src, video_id, mpd_id='dash', fatal=False)
61ebb401 102 self._sort_formats(formats)
a3498732 103
6d394a66
RA
104 heartbeat = episode.get('heartbeat') or {}
105 omniture = episode.get('omniture') or {}
106 get_meta = lambda x: heartbeat.get(x) or omniture.get(x)
0c172788 107
a3498732 108 return {
6d394a66 109 'display_id': display_id,
a3498732
S
110 'id': video_id,
111 'title': title,
6d394a66
RA
112 'description': episode.get('descripcion'),
113 'thumbnail': episode.get('imgPoster'),
114 'duration': int_or_none(episode.get('duration')),
a3498732 115 'formats': formats,
6d394a66
RA
116 'channel': get_meta('channel'),
117 'season': get_meta('season'),
118 'episode_number': int_or_none(get_meta('episodeNumber')),
c811e8d8 119 'subtitles': subtitles,
a3498732 120 }