[lnkgo] Make more robust
[youtube-dl] / youtube_dl / extractor / lnkgo.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     int_or_none,
9     js_to_json,
10     unified_strdate,
11 )
12
13
14 class LnkGoIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?lnkgo\.alfa\.lt/visi\-video/(?P<show>[^/]+)/ziurek\-(?P<display_id>[A-Za-z0-9\-]+)'
16     _TESTS = [{
17         'url': 'http://lnkgo.alfa.lt/visi-video/yra-kaip-yra/ziurek-yra-kaip-yra-162',
18         'info_dict': {
19             'id': '46712',
20             'ext': 'mp4',
21             'title': 'Yra kaip yra',
22             'upload_date': '20150107',
23             'description': 'md5:d82a5e36b775b7048617f263a0e3475e',
24             'age_limit': 7,
25             'duration': 3019,
26             'thumbnail': 're:^https?://.*\.jpg$'
27         },
28         'params': {
29             'skip_download': True,  # HLS download
30         },
31     }, {
32         'url': 'http://lnkgo.alfa.lt/visi-video/aktualai-pratesimas/ziurek-nerdas-taiso-kompiuteri-2',
33         'info_dict': {
34             'id': '47289',
35             'ext': 'mp4',
36             'title': 'Nėrdas: Kompiuterio Valymas',
37             'upload_date': '20150113',
38             'description': 'md5:7352d113a242a808676ff17e69db6a69',
39             'age_limit': 18,
40             'duration': 346,
41             'thumbnail': 're:^https?://.*\.jpg$'
42         },
43         'params': {
44             'skip_download': True,  # HLS download
45         },
46     }]
47     _AGE_LIMITS = {
48         'N-7': 7,
49         'N-14': 14,
50         'S': 18,
51     }
52
53     def _real_extract(self, url):
54         mobj = re.match(self._VALID_URL, url)
55         display_id = mobj.group('display_id')
56
57         webpage = self._download_webpage(
58             url, display_id, 'Downloading player webpage')
59
60         video_id = self._search_regex(
61             r'data-ep="([^"]+)"', webpage, 'video ID')
62         title = self._og_search_title(webpage)
63         description = self._og_search_description(webpage)
64
65         thumbnail_w = int_or_none(
66             self._og_search_property('image:width', webpage, 'thumbnail width', fatal=False))
67         thumbnail_h = int_or_none(
68             self._og_search_property('image:height', webpage, 'thumbnail height', fatal=False))
69         thumbnail = {
70             'url': self._og_search_thumbnail(webpage),
71         }
72         if thumbnail_w and thumbnail_h:
73             thumbnail.update({
74                 'width': thumbnail_w,
75                 'height': thumbnail_h,
76             })
77
78         upload_date = unified_strdate(self._search_regex(
79             r'class="meta-item\sair-time">.*?<strong>([^<]+)</strong>', webpage, 'upload date', fatal=False))
80         duration = int_or_none(self._search_regex(
81             r'VideoDuration = "([^"]+)"', webpage, 'duration', fatal=False))
82
83         pg_rating = self._search_regex(
84             r'pgrating="([^"]+)"', webpage, 'PG rating', fatal=False, default='')
85         age_limit = self._AGE_LIMITS.get(pg_rating.upper(), 0)
86
87         sources_js = self._search_regex(
88             r'(?s)sources:\s(\[.*?\]),', webpage, 'sources')
89         sources = self._parse_json(
90             sources_js, video_id, transform_source=js_to_json)
91
92         formats = []
93         for source in sources:
94             if source.get('provider') == 'rtmp':
95                 m = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<play_path>.+)$', source['file'])
96                 if not m:
97                     continue
98                 formats.append({
99                     'format_id': 'rtmp',
100                     'ext': 'flv',
101                     'url': m.group('url'),
102                     'play_path': m.group('play_path'),
103                     'page_url': url,
104                 })
105             elif source.get('file').endswith('.m3u8'):
106                 formats.append({
107                     'format_id': 'hls',
108                     'ext': source.get('type', 'mp4'),
109                     'url': source['file'],
110                 })
111
112         self._sort_formats(formats)
113
114         return {
115             'id': video_id,
116             'display_id': display_id,
117             'title': title,
118             'formats': formats,
119             'thumbnails': [thumbnail],
120             'duration': duration,
121             'description': description,
122             'age_limit': age_limit,
123             'upload_date': upload_date,
124         }