[BehindKink] Minor fixes
[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 ..utils import (
8     ExtractorError,
9     parse_iso8601,
10     qualities,
11 )
12
13
14 class TVPlayIE(InfoExtractor):
15     _VALID_URL = r'http://(?:www\.)?tvplay\.lv/parraides/[^/]+/(?P<id>\d+)'
16     _TESTS = [
17         {
18             'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
19             'info_dict': {
20                 'id': '418113',
21                 'ext': 'flv',
22                 'title': 'Kādi ir īri? - Viņas melo labāk',
23                 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
24                 'duration': 25,
25                 'timestamp': 1406097056,
26                 'upload_date': '20140723',
27             },
28             'params': {
29                 # rtmp download
30                 'skip_download': True,
31             },
32         },
33     ]
34
35     def _real_extract(self, url):
36         mobj = re.match(self._VALID_URL, url)
37         video_id = mobj.group('id')
38
39         video = self._download_json(
40             'http://playapi.mtgx.tv/v1/videos/%s' % video_id, video_id, 'Downloading video JSON')
41
42         if video['is_geo_blocked']:
43             raise ExtractorError(
44                 'This content is not available in your country due to copyright reasons', expected=True)
45
46         streams = self._download_json(
47             'http://playapi.mtgx.tv/v1/videos/stream/%s' % video_id, video_id, 'Downloading streams JSON')
48
49         quality = qualities(['hls', 'medium', 'high'])
50         formats = []
51         for format_id, video_url in streams['streams'].items():
52             if not video_url:
53                 continue
54             fmt = {
55                 'format_id': format_id,
56                 'preference': quality(format_id),
57             }
58             if video_url.startswith('rtmp'):
59                 m = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
60                 if not m:
61                     continue
62                 fmt.update({
63                     'ext': 'flv',
64                     'url': m.group('url'),
65                     'app': m.group('app'),
66                     'play_path': m.group('playpath'),
67                 })
68             else:
69                 fmt.update({
70                     'url': video_url,
71                 })
72             formats.append(fmt)
73
74         self._sort_formats(formats)
75
76         return {
77             'id': video_id,
78             'title': video['title'],
79             'description': video['description'],
80             'duration': video['duration'],
81             'timestamp': parse_iso8601(video['created_at']),
82             'view_count': video['views']['total'],
83             'age_limit': video.get('age_limit', 0),
84             'formats': formats,
85         }