[lnkgo] Add new extractor
[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 = self._og_search_thumbnail(webpage)
66         thumbnail_w = int_or_none(
67             self._og_search_property('image:width', webpage, 'thumbnail width', fatal=False))
68         thumbnail_h = int_or_none(
69             self._og_search_property('image:height', webpage, 'thumbnail height', fatal=False))
70         thumbnails = [{
71             'url': thumbnail,
72             'width': thumbnail_w,
73             'height': thumbnail_h,
74         }]
75
76         upload_date = unified_strdate(self._search_regex(
77             r'class="meta-item\sair-time">.*?<strong>([^<]+)</strong>', webpage, 'upload date', fatal=False))
78         duration = int_or_none(self._search_regex(
79             r'VideoDuration = "([^"]+)"', webpage, 'duration', fatal=False))
80
81         pg_rating = self._search_regex(
82             r'pgrating="([^"]+)"', webpage, 'PG rating', fatal=False, default='')
83         age_limit = self._AGE_LIMITS.get(pg_rating, 0)
84
85         sources_js = self._search_regex(
86             r'(?s)sources:\s(\[.*?\]),', webpage, 'sources')
87         sources = self._parse_json(
88             sources_js, video_id, transform_source=js_to_json)
89
90         formats = []
91         for source in sources:
92             if source.get('provider') == 'rtmp':
93                 m = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<play_path>.+)$', source['file'])
94                 if not m:
95                     continue
96                 formats.append({
97                     'format_id': 'rtmp',
98                     'ext': 'flv',
99                     'url': m.group('url'),
100                     'play_path': m.group('play_path'),
101                     'page_url': url,
102                 })
103             elif source.get('file').endswith('.m3u8'):
104                 formats.append({
105                     'format_id': 'hls',
106                     'ext': source.get('type', 'mp4'),
107                     'url': source['file'],
108                 })
109
110         self._sort_formats(formats)
111
112         return {
113             'id': video_id,
114             'display_id': display_id,
115             'title': title,
116             'formats': formats,
117             'thumbnails': thumbnails,
118             'duration': duration,
119             'description': description,
120             'age_limit': age_limit,
121             'upload_date': upload_date,
122         }