JukeboxIE: support more countries
[youtube-dl] / youtube_dl / extractor / jukebox.py
1 # coding: utf-8
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     unescapeHTML,
8 )
9
10 class JukeboxIE(InfoExtractor):
11     _VALID_URL = r'^http://www\.jukebox?\..+?\/.+[,](?P<video_id>[a-z0-9\-]+).html'
12     _IFRAME = r'<iframe .*src="(?P<iframe>[^"]*)".*>'
13     _VIDEO_URL = r'"config":{"file":"(?P<video_url>http:[^"]+[.](?P<video_ext>[^.?]+)[?]mdtk=[0-9]+)"'
14     _TITLE = r'<h1 class="inline">(?P<title>[^<]+)</h1>.*<span id="infos_article_artist">(?P<artist>[^<]+)</span>'
15     _IS_YOUTUBE = r'config":{"file":"(?P<youtube_url>http:[\\][/][\\][/]www[.]youtube[.]com[\\][/]watch[?]v=[^"]+)"'
16
17     def _real_extract(self, url):
18         mobj = re.match(self._VALID_URL, url)
19         video_id = mobj.group('video_id')
20
21         html = self._download_webpage(url, video_id)
22
23         mobj = re.search(self._IFRAME, html)
24         if mobj is None:
25             raise ExtractorError(u'Cannot extract iframe url')
26         iframe_url = unescapeHTML(mobj.group('iframe'))
27
28         iframe_html = self._download_webpage(iframe_url, video_id, 'Downloading iframe')
29         mobj = re.search(r'class="jkb_waiting"', iframe_html)
30         if mobj is not None:
31             raise ExtractorError(u'Video is not available(in your country?)!')
32
33         self.report_extraction(video_id)
34
35         mobj = re.search(self._VIDEO_URL, iframe_html)
36         if mobj is None:
37             mobj = re.search(self._IS_YOUTUBE, iframe_html)
38             if mobj is None:
39                 raise ExtractorError(u'Cannot extract video url')
40             youtube_url = unescapeHTML(mobj.group('youtube_url')).replace('\/','/')
41             self.to_screen(u'Youtube video detected')
42             return self.url_result(youtube_url,ie='Youtube')
43         video_url = unescapeHTML(mobj.group('video_url')).replace('\/','/')
44         video_ext = unescapeHTML(mobj.group('video_ext'))
45
46         mobj = re.search(self._TITLE, html)
47         if mobj is None:
48             raise ExtractorError(u'Cannot extract title')
49         title = unescapeHTML(mobj.group('title'))
50         artist = unescapeHTML(mobj.group('artist'))
51
52         return [{'id': video_id,
53                  'url': video_url,
54                  'title': artist + '-' + title,
55                  'ext': video_ext
56                  }]