[20min] Fix extraction
[youtube-dl] / youtube_dl / extractor / twentymin.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 remove_end
8
9
10 class TwentyMinutenIE(InfoExtractor):
11     IE_NAME = '20min'
12     _VALID_URL = r'https?://(?:www\.)?20min\.ch/(?:videotv/*\?.*\bvid=(?P<id>\d+)|(?:[^/]+/)*(?P<display_id>[^/#?]+))'
13     _TESTS = [{
14         # regular video
15         'url': 'http://www.20min.ch/videotv/?vid=469148&cid=2',
16         'md5': 'e7264320db31eed8c38364150c12496e',
17         'info_dict': {
18             'id': '469148',
19             'ext': 'mp4',
20             'title': '85 000 Franken für 15 perfekte Minuten',
21             'description': 'Was die Besucher vom Silvesterzauber erwarten können. (Video: Alice Grosjean/Murat Temel)',
22             'thumbnail': 'http://thumbnails.20min-tv.ch/server063/469148/frame-72-469148.jpg'
23         }
24     }, {
25         # news article with video
26         'url': 'http://www.20min.ch/schweiz/news/story/-Wir-muessen-mutig-nach-vorne-schauen--22050469',
27         'md5': 'cd4cbb99b94130cff423e967cd275e5e',
28         'info_dict': {
29             'id': '469408',
30             'display_id': '-Wir-muessen-mutig-nach-vorne-schauen--22050469',
31             'ext': 'flv',
32             'title': '«Wir müssen mutig nach vorne schauen»',
33             'description': 'Kein Land sei innovativer als die Schweiz, sagte Johann Schneider-Ammann in seiner Neujahrsansprache. Das Land müsse aber seine Hausaufgaben machen.',
34             'thumbnail': 'http://www.20min.ch/images/content/2/2/0/22050469/10/teaserbreit.jpg'
35         },
36         'skip': '"This video is no longer available" is shown both on the web page and in the downloaded file.',
37     }, {
38         # news article with video
39         'url': 'http://www.20min.ch/schweiz/news/story/So-kommen-Sie-bei-Eis-und-Schnee-sicher-an-27032552',
40         'md5': '372917ba85ed969e176d287ae54b2f94',
41         'info_dict': {
42             'id': '523629',
43             'display_id': 'So-kommen-Sie-bei-Eis-und-Schnee-sicher-an-27032552',
44             'ext': 'mp4',
45             'title': 'So kommen Sie bei Eis und Schnee sicher an',
46             'description': 'Schneegestöber und Glatteis führten in den letzten Tagen zu zahlreichen Strassenunfällen. Ein Experte erklärt, worauf man nun beim Autofahren achten muss.',
47             'thumbnail': 'http://www.20min.ch/images/content/2/7/0/27032552/83/teaserbreit.jpg',
48         }
49     }, {
50         # YouTube embed
51         'url': 'http://www.20min.ch/ro/sports/football/story/Il-marque-une-bicyclette-de-plus-de-30-metres--21115184',
52         'md5': 'e7e237fd98da2a3cc1422ce683df234d',
53         'info_dict': {
54             'id': 'ivM7A7SpDOs',
55             'ext': 'mp4',
56             'title': 'GOLAZO DE CHILENA DE JAVI GÓMEZ, FINALISTA AL BALÓN DE CLM 2016',
57             'description': 'md5:903c92fbf2b2f66c09de514bc25e9f5a',
58             'upload_date': '20160424',
59             'uploader': 'CMM Castilla-La Mancha Media',
60             'uploader_id': 'RTVCM',
61         },
62         'add_ie': ['Youtube'],
63     }, {
64         'url': 'http://www.20min.ch/videotv/?cid=44&vid=468738',
65         'only_matching': True,
66     }, {
67         'url': 'http://www.20min.ch/ro/sortir/cinema/story/Grandir-au-bahut--c-est-dur-18927411',
68         'only_matching': True,
69     }]
70
71     def _real_extract(self, url):
72         mobj = re.match(self._VALID_URL, url)
73         video_id = mobj.group('id')
74         display_id = mobj.group('display_id') or video_id
75
76         webpage = self._download_webpage(url, display_id)
77
78         youtube_url = self._html_search_regex(
79             r'<iframe[^>]+src="((?:https?:)?//www\.youtube\.com/embed/[^"]+)"',
80             webpage, 'YouTube embed URL', default=None)
81         if youtube_url is not None:
82             return self.url_result(youtube_url, 'Youtube')
83
84         title = self._html_search_regex(
85             r'<h1>.*?<span>(.+?)</span></h1>',
86             webpage, 'title', default=None)
87         if not title:
88             title = remove_end(re.sub(
89                 r'^20 [Mm]inuten.*? -', '', self._og_search_title(webpage)), ' - News')
90
91         if not video_id:
92             params = self._html_search_regex(
93                 r'<iframe[^>]+src="(?:https?:)?//www\.20min\.ch/videoplayer/videoplayer\.html\?params=(.+?[^"])"',
94                 webpage, '20min embed URL')
95             video_id = self._search_regex(
96                 r'.*videoId@(\d+)',
97                 params, 'Video Id')
98
99         description = self._html_search_meta(
100             'description', webpage, 'description')
101         thumbnail = self._og_search_thumbnail(webpage)
102
103         formats = []
104         format_preferences = [('sd', ''), ('hd', 'h')]
105         for format_id, url_extension in format_preferences:
106             format_url = 'http://podcast.20min-tv.ch/podcast/20min/%s%s.mp4' % (video_id, url_extension)
107             formats.append({
108                 'format_id': format_id,
109                 'url': format_url,
110             })
111
112         return {
113             'id': video_id,
114             'display_id': display_id,
115             'title': title,
116             'description': description,
117             'thumbnail': thumbnail,
118             'formats': formats,
119         }