[BR] Add "BR" extractor
[youtube-dl] / youtube_dl / extractor / br.py
1 # coding: utf-8
2
3 from .common import InfoExtractor
4
5 class BRIE(InfoExtractor):
6
7     IE_DESC = u"Bayerischer Rundfunk Mediathek"
8     _VALID_URL = r"^https?://(?:www\.)?br\.de/mediathek/video/(?:sendungen/)?(?:[a-z0-9\-]+\.html)$"
9     _BASE_URL = u"http://www.br.de"
10
11     _TESTS = []
12
13     def _real_extract(self, url):
14         page = self._download_webpage(url, None)
15         xml_url = self._search_regex(r"return BRavFramework\.register\(BRavFramework\('avPlayer_(?:[a-f0-9-]{36})'\)\.setup\({dataURL:'(/mediathek/video/[a-z0-9/~_.-]+)'}\)\);", page, "XMLURL")
16         xml = self._download_xml(self._BASE_URL + xml_url, None)
17
18         videos = []
19         for xml_video in xml.findall("video"):
20             video = {}
21             video["id"] = xml_video.get("externalId")
22             video["title"] = xml_video.find("title").text
23             video["formats"] = self._extract_formats(xml_video.find("assets"))
24             video["thumbnails"] = self._extract_thumbnails(xml_video.find("teaserImage/variants"))
25             video["thumbnail"] = video["thumbnails"][0]["url"]
26             video["description"] = " ".join(xml_video.find("shareTitle").text.splitlines())
27             video["uploader"] = xml_video.find("author").text
28             video["upload_date"] = "".join(reversed(xml_video.find("broadcastDate").text.split(".")))
29             video["webpage_url"] = xml_video.find("permalink").text
30             videos.append(video)
31
32         if len(videos) > 1:
33             self._downloader.report_warning(u'found multiple videos; please'
34                 u'report this with the video URL to http://yt-dl.org/bug')
35         return videos[0]
36
37     def _extract_formats(self, assets):
38         vformats = []
39         for asset in assets.findall("asset"):
40             if asset.find("downloadUrl") is None:
41                 continue
42             vformat = {}
43             vformat["url"] = asset.find("downloadUrl").text
44             vformat["ext"] = asset.find("mediaType").text
45             vformat["format_id"] = asset.get("type")
46             vformat["width"] = int(asset.find("frameWidth").text)
47             vformat["height"] = int(asset.find("frameHeight").text)
48             vformat["resolution"] = "%ix%i" % (vformat["width"], vformat["height"])
49             vformat["tbr"] = int(asset.find("bitrateVideo").text)
50             vformat["abr"] = int(asset.find("bitrateAudio").text)
51             vformat["vcodec"] = asset.find("codecVideo").text
52             vformat["container"] = vformat["ext"]
53             vformat["filesize"] = int(asset.find("size").text)
54             vformat["preference"] = vformat["quality"] = -1
55             vformat["format"] = "%s container with %i Kbps %s" % (vformat["container"], vformat["tbr"], vformat["vcodec"])
56             vformats.append(vformat)
57         self._sort_formats(vformats)
58         return vformats
59
60     def _extract_thumbnails(self, variants):
61         thumbnails = []
62         for variant in variants.findall("variant"):
63             thumbnail = {}
64             thumbnail["url"] = self._BASE_URL + variant.find("url").text
65             thumbnail["width"] = int(variant.find("width").text)
66             thumbnail["height"] = int(variant.find("height").text)
67             thumbnail["resolution"] = "%ix%i" % (thumbnail["width"], thumbnail["height"])
68             thumbnails.append(thumbnail)
69         thumbnails.sort(key = lambda x: x["width"] * x["height"], reverse=True)
70         return thumbnails