Merge branch 'douyutv' of https://github.com/bonfy/youtube-dl into bonfy-douyutv
[youtube-dl] / youtube_dl / extractor / tvplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     parse_iso8601,
10     qualities,
11 )
12
13
14 class TVPlayIE(InfoExtractor):
15     IE_DESC = 'TV3Play and related services'
16     _VALID_URL = r'''(?x)http://(?:www\.)?
17         (?:tvplay\.lv/parraides|
18            tv3play\.lt/programos|
19            play\.tv3\.lt/programos|
20            tv3play\.ee/sisu|
21            tv3play\.se/program|
22            tv6play\.se/program|
23            tv8play\.se/program|
24            tv10play\.se/program|
25            tv3play\.no/programmer|
26            viasat4play\.no/programmer|
27            tv6play\.no/programmer|
28            tv3play\.dk/programmer|
29         )/[^/]+/(?P<id>\d+)
30         '''
31     _TESTS = [
32         {
33             'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
34             'info_dict': {
35                 'id': '418113',
36                 'ext': 'flv',
37                 'title': 'Kādi ir īri? - Viņas melo labāk',
38                 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
39                 'duration': 25,
40                 'timestamp': 1406097056,
41                 'upload_date': '20140723',
42             },
43             'params': {
44                 # rtmp download
45                 'skip_download': True,
46             },
47         },
48         {
49             'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
50             'info_dict': {
51                 'id': '409229',
52                 'ext': 'flv',
53                 'title': 'Moterys meluoja geriau',
54                 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
55                 'duration': 1330,
56                 'timestamp': 1403769181,
57                 'upload_date': '20140626',
58             },
59             'params': {
60                 # rtmp download
61                 'skip_download': True,
62             },
63         },
64         {
65             'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
66             'info_dict': {
67                 'id': '238551',
68                 'ext': 'flv',
69                 'title': 'Kodu keset linna 398537',
70                 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
71                 'duration': 1257,
72                 'timestamp': 1292449761,
73                 'upload_date': '20101215',
74             },
75             'params': {
76                 # rtmp download
77                 'skip_download': True,
78             },
79         },
80         {
81             'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
82             'info_dict': {
83                 'id': '395385',
84                 'ext': 'flv',
85                 'title': 'Husräddarna S02E07',
86                 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
87                 'duration': 2574,
88                 'timestamp': 1400596321,
89                 'upload_date': '20140520',
90             },
91             'params': {
92                 # rtmp download
93                 'skip_download': True,
94             },
95         },
96         {
97             'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
98             'info_dict': {
99                 'id': '266636',
100                 'ext': 'flv',
101                 'title': 'Den sista dokusåpan S01E08',
102                 'description': 'md5:295be39c872520221b933830f660b110',
103                 'duration': 1492,
104                 'timestamp': 1330522854,
105                 'upload_date': '20120229',
106             },
107             'params': {
108                 # rtmp download
109                 'skip_download': True,
110             },
111         },
112         {
113             'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
114             'info_dict': {
115                 'id': '282756',
116                 'ext': 'flv',
117                 'title': 'Antikjakten S01E10',
118                 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
119                 'duration': 2646,
120                 'timestamp': 1348575868,
121                 'upload_date': '20120925',
122             },
123             'params': {
124                 # rtmp download
125                 'skip_download': True,
126             },
127         },
128         {
129             'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
130             'info_dict': {
131                 'id': '230898',
132                 'ext': 'flv',
133                 'title': 'Anna Anka søker assistent - Ep. 8',
134                 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
135                 'duration': 2656,
136                 'timestamp': 1277720005,
137                 'upload_date': '20100628',
138             },
139             'params': {
140                 # rtmp download
141                 'skip_download': True,
142             },
143         },
144         {
145             'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
146             'info_dict': {
147                 'id': '21873',
148                 'ext': 'flv',
149                 'title': 'Budbringerne program 10',
150                 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
151                 'duration': 1297,
152                 'timestamp': 1254205102,
153                 'upload_date': '20090929',
154             },
155             'params': {
156                 # rtmp download
157                 'skip_download': True,
158             },
159         },
160         {
161             'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
162             'info_dict': {
163                 'id': '361883',
164                 'ext': 'flv',
165                 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
166                 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
167                 'duration': 2594,
168                 'timestamp': 1393236292,
169                 'upload_date': '20140224',
170             },
171             'params': {
172                 # rtmp download
173                 'skip_download': True,
174             },
175         },
176     ]
177
178     def _real_extract(self, url):
179         video_id = self._match_id(url)
180
181         video = self._download_json(
182             'http://playapi.mtgx.tv/v1/videos/%s' % video_id, video_id, 'Downloading video JSON')
183
184         if video['is_geo_blocked']:
185             self.report_warning(
186                 'This content might not be available in your country due to copyright reasons')
187
188         streams = self._download_json(
189             'http://playapi.mtgx.tv/v1/videos/stream/%s' % video_id, video_id, 'Downloading streams JSON')
190
191         quality = qualities(['hls', 'medium', 'high'])
192         formats = []
193         for format_id, video_url in streams['streams'].items():
194             if not video_url or not isinstance(video_url, compat_str):
195                 continue
196             fmt = {
197                 'format_id': format_id,
198                 'preference': quality(format_id),
199             }
200             if video_url.startswith('rtmp'):
201                 m = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
202                 if not m:
203                     continue
204                 fmt.update({
205                     'ext': 'flv',
206                     'url': m.group('url'),
207                     'app': m.group('app'),
208                     'play_path': m.group('playpath'),
209                 })
210             elif video_url.endswith('.f4m'):
211                 formats.extend(self._extract_f4m_formats(
212                     video_url + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81', video_id))
213                 continue
214             else:
215                 fmt.update({
216                     'url': video_url,
217                 })
218             formats.append(fmt)
219
220         self._sort_formats(formats)
221
222         return {
223             'id': video_id,
224             'title': video['title'],
225             'description': video['description'],
226             'duration': video['duration'],
227             'timestamp': parse_iso8601(video['created_at']),
228             'view_count': video['views']['total'],
229             'age_limit': video.get('age_limit', 0),
230             'formats': formats,
231         }