]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/noco.py
7f1bc6377a42e99d853a38dd406a60dda929e49f
[youtubedl] / youtube_dl / extractor / noco.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import hashlib
7
8 from .common import InfoExtractor
9 from ..utils import (
10 compat_urllib_request,
11 compat_urllib_parse,
12 ExtractorError,
13 clean_html,
14 unified_strdate,
15 compat_str,
16 )
17
18
19 class NocoIE(InfoExtractor):
20 _VALID_URL = r'http://(?:(?:www\.)?noco\.tv/emission/|player\.noco\.tv/\?idvideo=)(?P<id>\d+)'
21 _LOGIN_URL = 'http://noco.tv/do.php'
22 _API_URL_TEMPLATE = 'https://api.noco.tv/1.1/%s?ts=%s&tk=%s'
23 _NETRC_MACHINE = 'noco'
24
25 _TEST = {
26 'url': 'http://noco.tv/emission/11538/nolife/ami-ami-idol-hello-france/',
27 'md5': '0a993f0058ddbcd902630b2047ef710e',
28 'info_dict': {
29 'id': '11538',
30 'ext': 'mp4',
31 'title': 'Ami Ami Idol - Hello! France',
32 'description': 'md5:4eaab46ab68fa4197a317a88a53d3b86',
33 'upload_date': '20140412',
34 'uploader': 'Nolife',
35 'uploader_id': 'NOL',
36 'duration': 2851.2,
37 },
38 'skip': 'Requires noco account',
39 }
40
41 def _real_initialize(self):
42 self._login()
43
44 def _login(self):
45 (username, password) = self._get_login_info()
46 if username is None:
47 return
48
49 login_form = {
50 'a': 'login',
51 'cookie': '1',
52 'username': username,
53 'password': password,
54 }
55 request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
56 request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
57
58 login = self._download_json(request, None, 'Logging in as %s' % username)
59
60 if 'erreur' in login:
61 raise ExtractorError('Unable to login: %s' % clean_html(login['erreur']), expected=True)
62
63 def _call_api(self, path, video_id, note):
64 ts = compat_str(int(time.time() * 1000))
65 tk = hashlib.md5((hashlib.md5(ts.encode('ascii')).hexdigest() + '#8S?uCraTedap6a').encode('ascii')).hexdigest()
66 url = self._API_URL_TEMPLATE % (path, ts, tk)
67
68 resp = self._download_json(url, video_id, note)
69
70 if isinstance(resp, dict) and resp.get('error'):
71 self._raise_error(resp['error'], resp['description'])
72
73 return resp
74
75 def _raise_error(self, error, description):
76 raise ExtractorError(
77 '%s returned error: %s - %s' % (self.IE_NAME, error, description),
78 expected=True)
79
80 def _real_extract(self, url):
81 mobj = re.match(self._VALID_URL, url)
82 video_id = mobj.group('id')
83
84 medias = self._call_api(
85 'shows/%s/medias' % video_id,
86 video_id, 'Downloading video JSON')
87
88 qualities = self._call_api(
89 'qualities',
90 video_id, 'Downloading qualities JSON')
91
92 formats = []
93
94 for format_id, fmt in medias['fr']['video_list']['none']['quality_list'].items():
95
96 video = self._call_api(
97 'shows/%s/video/%s/fr' % (video_id, format_id.lower()),
98 video_id, 'Downloading %s video JSON' % format_id)
99
100 file_url = video['file']
101 if not file_url:
102 continue
103
104 if file_url in ['forbidden', 'not found']:
105 popmessage = video['popmessage']
106 self._raise_error(popmessage['title'], popmessage['message'])
107
108 formats.append({
109 'url': file_url,
110 'format_id': format_id,
111 'width': fmt['res_width'],
112 'height': fmt['res_lines'],
113 'abr': fmt['audiobitrate'],
114 'vbr': fmt['videobitrate'],
115 'filesize': fmt['filesize'],
116 'format_note': qualities[format_id]['quality_name'],
117 'preference': qualities[format_id]['priority'],
118 })
119
120 self._sort_formats(formats)
121
122 show = self._call_api(
123 'shows/by_id/%s' % video_id,
124 video_id, 'Downloading show JSON')[0]
125
126 upload_date = unified_strdate(show['online_date_start_utc'])
127 uploader = show['partner_name']
128 uploader_id = show['partner_key']
129 duration = show['duration_ms'] / 1000.0
130
131 thumbnails = []
132 for thumbnail_key, thumbnail_url in show.items():
133 m = re.search(r'^screenshot_(?P<width>\d+)x(?P<height>\d+)$', thumbnail_key)
134 if not m:
135 continue
136 thumbnails.append({
137 'url': thumbnail_url,
138 'width': int(m.group('width')),
139 'height': int(m.group('height')),
140 })
141
142 episode = show.get('show_TT') or show.get('show_OT')
143 family = show.get('family_TT') or show.get('family_OT')
144 episode_number = show.get('episode_number')
145
146 title = ''
147 if family:
148 title += family
149 if episode_number:
150 title += ' #' + compat_str(episode_number)
151 if episode:
152 title += ' - ' + episode
153
154 description = show.get('show_resume') or show.get('family_resume')
155
156 return {
157 'id': video_id,
158 'title': title,
159 'description': description,
160 'thumbnails': thumbnails,
161 'upload_date': upload_date,
162 'uploader': uploader,
163 'uploader_id': uploader_id,
164 'duration': duration,
165 'formats': formats,
166 }