Move ARD, Arte, ZDF into their own files
[youtube-dl] / youtube_dl / extractor / arte.py
1 import re
2 import socket
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_http_client,
7     compat_str,
8     compat_urllib_error,
9     compat_urllib_parse,
10     compat_urllib_request,
11
12     ExtractorError,
13     unified_strdate,
14 )
15
16 class ArteTvIE(InfoExtractor):
17     """arte.tv information extractor."""
18
19     _VALID_URL = r'(?:http://)?videos\.arte\.tv/(?:fr|de)/videos/.*'
20     _LIVE_URL = r'index-[0-9]+\.html$'
21
22     IE_NAME = u'arte.tv'
23
24     def fetch_webpage(self, url):
25         request = compat_urllib_request.Request(url)
26         try:
27             self.report_download_webpage(url)
28             webpage = compat_urllib_request.urlopen(request).read()
29         except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
30             raise ExtractorError(u'Unable to retrieve video webpage: %s' % compat_str(err))
31         except ValueError as err:
32             raise ExtractorError(u'Invalid URL: %s' % url)
33         return webpage
34
35     def grep_webpage(self, url, regex, regexFlags, matchTuples):
36         page = self.fetch_webpage(url)
37         mobj = re.search(regex, page, regexFlags)
38         info = {}
39
40         if mobj is None:
41             raise ExtractorError(u'Invalid URL: %s' % url)
42
43         for (i, key, err) in matchTuples:
44             if mobj.group(i) is None:
45                 raise ExtractorError(err)
46             else:
47                 info[key] = mobj.group(i)
48
49         return info
50
51     def extractLiveStream(self, url):
52         video_lang = url.split('/')[-4]
53         info = self.grep_webpage(
54             url,
55             r'src="(.*?/videothek_js.*?\.js)',
56             0,
57             [
58                 (1, 'url', u'Invalid URL: %s' % url)
59             ]
60         )
61         http_host = url.split('/')[2]
62         next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
63         info = self.grep_webpage(
64             next_url,
65             r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
66                 '(http://.*?\.swf).*?' +
67                 '(rtmp://.*?)\'',
68             re.DOTALL,
69             [
70                 (1, 'path',   u'could not extract video path: %s' % url),
71                 (2, 'player', u'could not extract video player: %s' % url),
72                 (3, 'url',    u'could not extract video url: %s' % url)
73             ]
74         )
75         video_url = u'%s/%s' % (info.get('url'), info.get('path'))
76
77     def extractPlus7Stream(self, url):
78         video_lang = url.split('/')[-3]
79         info = self.grep_webpage(
80             url,
81             r'param name="movie".*?videorefFileUrl=(http[^\'"&]*)',
82             0,
83             [
84                 (1, 'url', u'Invalid URL: %s' % url)
85             ]
86         )
87         next_url = compat_urllib_parse.unquote(info.get('url'))
88         info = self.grep_webpage(
89             next_url,
90             r'<video lang="%s" ref="(http[^\'"&]*)' % video_lang,
91             0,
92             [
93                 (1, 'url', u'Could not find <video> tag: %s' % url)
94             ]
95         )
96         next_url = compat_urllib_parse.unquote(info.get('url'))
97
98         info = self.grep_webpage(
99             next_url,
100             r'<video id="(.*?)".*?>.*?' +
101                 '<name>(.*?)</name>.*?' +
102                 '<dateVideo>(.*?)</dateVideo>.*?' +
103                 '<url quality="hd">(.*?)</url>',
104             re.DOTALL,
105             [
106                 (1, 'id',    u'could not extract video id: %s' % url),
107                 (2, 'title', u'could not extract video title: %s' % url),
108                 (3, 'date',  u'could not extract video date: %s' % url),
109                 (4, 'url',   u'could not extract video url: %s' % url)
110             ]
111         )
112
113         return {
114             'id':           info.get('id'),
115             'url':          compat_urllib_parse.unquote(info.get('url')),
116             'uploader':     u'arte.tv',
117             'upload_date':  unified_strdate(info.get('date')),
118             'title':        info.get('title').decode('utf-8'),
119             'ext':          u'mp4',
120             'format':       u'NA',
121             'player_url':   None,
122         }
123
124     def _real_extract(self, url):
125         video_id = url.split('/')[-1]
126         self.report_extraction(video_id)
127
128         if re.search(self._LIVE_URL, video_id) is not None:
129             self.extractLiveStream(url)
130             return
131         else:
132             info = self.extractPlus7Stream(url)
133
134         return [info]