Merge remote-tracking branch 'upstream/master'
[youtube-dl] / youtube_dl / extractor / noco.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_request,
9     compat_urllib_parse,
10     ExtractorError,
11     clean_html,
12     unified_strdate,
13     compat_str,
14 )
15
16
17 class NocoIE(InfoExtractor):
18     _VALID_URL = r'http://(?:(?:www\.)?noco\.tv/emission/|player\.noco\.tv/\?idvideo=)(?P<id>\d+)'
19     _LOGIN_URL = 'http://noco.tv/do.php'
20     _NETRC_MACHINE = 'noco'
21
22     _TEST = {
23         'url': 'http://noco.tv/emission/11538/nolife/ami-ami-idol-hello-france/',
24         'md5': '0a993f0058ddbcd902630b2047ef710e',
25         'info_dict': {
26             'id': '11538',
27             'ext': 'mp4',
28             'title': 'Ami Ami Idol - Hello! France',
29             'description': 'md5:4eaab46ab68fa4197a317a88a53d3b86',
30             'upload_date': '20140412',
31             'uploader': 'Nolife',
32             'uploader_id': 'NOL',
33             'duration': 2851.2,
34         },
35         'skip': 'Requires noco account',
36     }
37
38     def _real_initialize(self):
39         self._login()
40
41     def _login(self):
42         (username, password) = self._get_login_info()
43         if username is None:
44             return
45
46         login_form = {
47             'a': 'login',
48             'cookie': '1',
49             'username': username,
50             'password': password,
51         }
52         request = compat_urllib_request.Request(self._LOGIN_URL, compat_urllib_parse.urlencode(login_form))
53         request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
54
55         login = self._download_json(request, None, 'Logging in as %s' % username)
56
57         if 'erreur' in login:
58             raise  ExtractorError('Unable to login: %s' % clean_html(login['erreur']), expected=True)
59
60     def _real_extract(self, url):
61         mobj = re.match(self._VALID_URL, url)
62         video_id = mobj.group('id')
63
64         medias = self._download_json(
65             'https://api.noco.tv/1.0/video/medias/%s' % video_id, video_id, 'Downloading video JSON')
66
67         formats = []
68
69         for fmt in medias['fr']['video_list']['default']['quality_list']:
70             format_id = fmt['quality_key']
71
72             file = self._download_json(
73                 'https://api.noco.tv/1.0/video/file/%s/fr/%s' % (format_id.lower(), video_id),
74                 video_id, 'Downloading %s video JSON' % format_id)
75
76             file_url = file['file']
77             if not file_url:
78                 continue
79
80             if file_url == 'forbidden':
81                 raise ExtractorError(
82                     '%s returned error: %s - %s' % (
83                         self.IE_NAME, file['popmessage']['title'], file['popmessage']['message']),
84                     expected=True)
85
86             formats.append({
87                 'url': file_url,
88                 'format_id': format_id,
89                 'width': fmt['res_width'],
90                 'height': fmt['res_lines'],
91                 'abr': fmt['audiobitrate'],
92                 'vbr': fmt['videobitrate'],
93                 'filesize': fmt['filesize'],
94                 'format_note': fmt['quality_name'],
95                 'preference': fmt['priority'],
96             })
97
98         self._sort_formats(formats)
99
100         show = self._download_json(
101             'https://api.noco.tv/1.0/shows/show/%s' % video_id, video_id, 'Downloading show JSON')[0]
102
103         upload_date = unified_strdate(show['indexed'])
104         uploader = show['partner_name']
105         uploader_id = show['partner_key']
106         duration = show['duration_ms'] / 1000.0
107         thumbnail = show['screenshot']
108
109         episode = show.get('show_TT') or show.get('show_OT')
110         family = show.get('family_TT') or show.get('family_OT')
111         episode_number = show.get('episode_number')
112
113         title = ''
114         if family:
115             title += family
116         if episode_number:
117             title += ' #' + compat_str(episode_number)
118         if episode:
119             title += ' - ' + episode
120
121         description = show.get('show_resume') or show.get('family_resume')
122
123         return {
124             'id': video_id,
125             'title': title,
126             'description': description,
127             'thumbnail': thumbnail,
128             'upload_date': upload_date,
129             'uploader': uploader,
130             'uploader_id': uploader_id,
131             'duration': duration,
132             'formats': formats,
133         }