br.py 3.3 KB

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