]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/adn.py
66caf6a818278796648dd4496da95c661fcdb36c
[youtubedl] / youtube_dl / extractor / adn.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import json
6 import os
7
8 from .common import InfoExtractor
9 from ..aes import aes_cbc_decrypt
10 from ..compat import compat_ord
11 from ..utils import (
12 bytes_to_intlist,
13 ExtractorError,
14 float_or_none,
15 intlist_to_bytes,
16 srt_subtitles_timecode,
17 strip_or_none,
18 )
19
20
21 class ADNIE(InfoExtractor):
22 IE_DESC = 'Anime Digital Network'
23 _VALID_URL = r'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)'
24 _TEST = {
25 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites',
26 'md5': 'e497370d847fd79d9d4c74be55575c7a',
27 'info_dict': {
28 'id': '7778',
29 'ext': 'mp4',
30 'title': 'Blue Exorcist - Kyôto Saga - Épisode 1',
31 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5',
32 }
33 }
34
35 def _get_subtitles(self, sub_path, video_id):
36 if not sub_path:
37 return None
38
39 enc_subtitles = self._download_webpage(
40 'http://animedigitalnetwork.fr/' + sub_path,
41 video_id, fatal=False)
42 if not enc_subtitles:
43 return None
44
45 # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
46 dec_subtitles = intlist_to_bytes(aes_cbc_decrypt(
47 bytes_to_intlist(base64.b64decode(enc_subtitles[24:])),
48 bytes_to_intlist(b'\nd\xaf\xd2J\xd0\xfc\xe1\xfc\xdf\xb61\xe8\xe1\xf0\xcc'),
49 bytes_to_intlist(base64.b64decode(enc_subtitles[:24]))
50 ))
51 subtitles_json = self._parse_json(
52 dec_subtitles[:-compat_ord(dec_subtitles[-1])],
53 None, fatal=False)
54 if not subtitles_json:
55 return None
56
57 subtitles = {}
58 for sub_lang, sub in subtitles_json.items():
59 srt = ''
60 for num, current in enumerate(sub):
61 start, end, text = (
62 float_or_none(current.get('startTime')),
63 float_or_none(current.get('endTime')),
64 current.get('text'))
65 if start is None or end is None or text is None:
66 continue
67 srt += os.linesep.join(
68 (
69 '%d' % num,
70 '%s --> %s' % (
71 srt_subtitles_timecode(start),
72 srt_subtitles_timecode(end)),
73 text,
74 os.linesep,
75 ))
76
77 if sub_lang == 'vostf':
78 sub_lang = 'fr'
79 subtitles.setdefault(sub_lang, []).extend([{
80 'ext': 'json',
81 'data': json.dumps(sub),
82 }, {
83 'ext': 'srt',
84 'data': srt,
85 }])
86 return subtitles
87
88 def _real_extract(self, url):
89 video_id = self._match_id(url)
90 webpage = self._download_webpage(url, video_id)
91 player_config = self._parse_json(self._search_regex(
92 r'playerConfig\s*=\s*({.+});', webpage, 'player config'), video_id)
93
94 video_info = {}
95 video_info_str = self._search_regex(
96 r'videoInfo\s*=\s*({.+});', webpage,
97 'video info', fatal=False)
98 if video_info_str:
99 video_info = self._parse_json(
100 video_info_str, video_id, fatal=False) or {}
101
102 options = player_config.get('options') or {}
103 metas = options.get('metas') or {}
104 title = metas.get('title') or video_info['title']
105 links = player_config.get('links') or {}
106
107 formats = []
108 for format_id, qualities in links.items():
109 for load_balancer_url in qualities.values():
110 load_balancer_data = self._download_json(
111 load_balancer_url, video_id, fatal=False) or {}
112 m3u8_url = load_balancer_data.get('location')
113 if not m3u8_url:
114 continue
115 m3u8_formats = self._extract_m3u8_formats(
116 m3u8_url, video_id, 'mp4', 'm3u8_native',
117 m3u8_id=format_id, fatal=False)
118 if format_id == 'vf':
119 for f in m3u8_formats:
120 f['language'] = 'fr'
121 formats.extend(m3u8_formats)
122 error = options.get('error')
123 if not formats and error:
124 raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
125 self._sort_formats(formats)
126
127 return {
128 'id': video_id,
129 'title': title,
130 'description': strip_or_none(metas.get('summary') or video_info.get('resume')),
131 'thumbnail': video_info.get('image'),
132 'formats': formats,
133 'subtitles': self.extract_subtitles(player_config.get('subtitles'), video_id),
134 'episode': metas.get('subtitle') or video_info.get('videoTitle'),
135 'series': video_info.get('playlistTitle'),
136 }