Merge pull request #9395 from pmrowla/afreecatv
[youtube-dl] / youtube_dl / extractor / tvp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8
9 class TVPIE(InfoExtractor):
10     IE_NAME = 'tvp'
11     IE_DESC = 'Telewizja Polska'
12     _VALID_URL = r'https?://[^/]+\.tvp\.(?:pl|info)/(?:(?!\d+/)[^/]+/)*(?P<id>\d+)'
13
14     _TESTS = [{
15         'url': 'http://vod.tvp.pl/194536/i-seria-odc-13',
16         'md5': '8aa518c15e5cc32dfe8db400dc921fbb',
17         'info_dict': {
18             'id': '194536',
19             'ext': 'mp4',
20             'title': 'Czas honoru, I seria – odc. 13',
21         },
22     }, {
23         'url': 'http://www.tvp.pl/there-can-be-anything-so-i-shortened-it/17916176',
24         'md5': 'c3b15ed1af288131115ff17a17c19dda',
25         'info_dict': {
26             'id': '17916176',
27             'ext': 'mp4',
28             'title': 'TVP Gorzów pokaże filmy studentów z podroży dookoła świata',
29         },
30     }, {
31         'url': 'http://vod.tvp.pl/seriale/obyczajowe/na-sygnale/sezon-2-27-/odc-39/17834272',
32         'only_matching': True,
33     }, {
34         'url': 'http://wiadomosci.tvp.pl/25169746/24052016-1200',
35         'only_matching': True,
36     }, {
37         'url': 'http://krakow.tvp.pl/25511623/25lecie-mck-wyjatkowe-miejsce-na-mapie-krakowa',
38         'only_matching': True,
39     }, {
40         'url': 'http://teleexpress.tvp.pl/25522307/wierni-wzieli-udzial-w-procesjach',
41         'only_matching': True,
42     }, {
43         'url': 'http://sport.tvp.pl/25522165/krychowiak-uspokaja-w-sprawie-kontuzji-dwa-tygodnie-to-maksimum',
44         'only_matching': True,
45     }, {
46         'url': 'http://www.tvp.info/25511919/trwa-rewolucja-wladza-zdecydowala-sie-na-pogwalcenie-konstytucji',
47         'only_matching': True,
48     }]
49
50     def _real_extract(self, url):
51         video_id = self._match_id(url)
52
53         webpage = self._download_webpage(
54             'http://www.tvp.pl/sess/tvplayer.php?object_id=%s' % video_id, video_id)
55
56         title = self._search_regex(
57             r'name\s*:\s*([\'"])Title\1\s*,\s*value\s*:\s*\1(?P<title>.+?)\1',
58             webpage, 'title', group='title')
59         series_title = self._search_regex(
60             r'name\s*:\s*([\'"])SeriesTitle\1\s*,\s*value\s*:\s*\1(?P<series>.+?)\1',
61             webpage, 'series', group='series', default=None)
62         if series_title:
63             title = '%s, %s' % (series_title, title)
64
65         thumbnail = self._search_regex(
66             r"poster\s*:\s*'([^']+)'", webpage, 'thumbnail', default=None)
67
68         video_url = self._search_regex(
69             r'0:{src:([\'"])(?P<url>.*?)\1', webpage, 'formats', group='url', default=None)
70         if not video_url:
71             video_url = self._download_json(
72                 'http://www.tvp.pl/pub/stat/videofileinfo?video_id=%s' % video_id,
73                 video_id)['video_url']
74
75         ext = video_url.rsplit('.', 1)[-1]
76         if ext != 'ism/manifest':
77             if '/' in ext:
78                 ext = 'mp4'
79             formats = [{
80                 'format_id': 'direct',
81                 'url': video_url,
82                 'ext': ext,
83             }]
84         else:
85             m3u8_url = re.sub('([^/]*)\.ism/manifest', r'\1.ism/\1.m3u8', video_url)
86             formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
87
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': title,
93             'thumbnail': thumbnail,
94             'formats': formats,
95         }
96
97
98 class TVPSeriesIE(InfoExtractor):
99     IE_NAME = 'tvp:series'
100     _VALID_URL = r'https?://vod\.tvp\.pl/(?:[^/]+/){2}(?P<id>[^/]+)/?$'
101
102     _TESTS = [{
103         'url': 'http://vod.tvp.pl/filmy-fabularne/filmy-za-darmo/ogniem-i-mieczem',
104         'info_dict': {
105             'title': 'Ogniem i mieczem',
106             'id': '4278026',
107         },
108         'playlist_count': 4,
109     }, {
110         'url': 'http://vod.tvp.pl/audycje/podroze/boso-przez-swiat',
111         'info_dict': {
112             'title': 'Boso przez świat',
113             'id': '9329207',
114         },
115         'playlist_count': 86,
116     }]
117
118     def _real_extract(self, url):
119         display_id = self._match_id(url)
120         webpage = self._download_webpage(url, display_id, tries=5)
121
122         title = self._html_search_regex(
123             r'(?s) id=[\'"]path[\'"]>(?:.*? / ){2}(.*?)</span>', webpage, 'series')
124         playlist_id = self._search_regex(r'nodeId:\s*(\d+)', webpage, 'playlist id')
125         playlist = self._download_webpage(
126             'http://vod.tvp.pl/vod/seriesAjax?type=series&nodeId=%s&recommend'
127             'edId=0&sort=&page=0&pageSize=10000' % playlist_id, display_id, tries=5,
128             note='Downloading playlist')
129
130         videos_paths = re.findall(
131             '(?s)class="shortTitle">.*?href="(/[^"]+)', playlist)
132         entries = [
133             self.url_result('http://vod.tvp.pl%s' % v_path, ie=TVPIE.ie_key())
134             for v_path in videos_paths]
135
136         return {
137             '_type': 'playlist',
138             'id': playlist_id,
139             'display_id': display_id,
140             'title': title,
141             'entries': entries,
142         }