mdr.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. )
  6. class MDRIE(InfoExtractor):
  7. _VALID_URL = r'^(?P<domain>(?:https?://)?(?:www\.)?mdr\.de)/mediathek/(?:.*)/(?P<type>video|audio)(?P<video_id>[^/_]+)_.*'
  8. _TESTS = [{
  9. u'url': u'http://www.mdr.de/mediathek/themen/nachrichten/video165624_zc-c5c7de76_zs-3795826d.html',
  10. u'file': u'165624.mp4',
  11. u'md5': u'ae785f36ecbf2f19b42edf1bc9c85815',
  12. u'info_dict': {
  13. u"title": u"MDR aktuell Eins30 09.12.2013, 22:48 Uhr"
  14. },
  15. },
  16. {
  17. u'url': u'http://www.mdr.de/mediathek/radio/mdr1-radio-sachsen/audio718370_zc-67b21197_zs-1b9b2483.html',
  18. u'file': u'718370.mp3',
  19. u'md5': u'a9d21345a234c7b45dee612f290fd8d7',
  20. u'info_dict': {
  21. u"title": u"MDR 1 RADIO SACHSEN 10.12.2013, 05:00 Uhr"
  22. },
  23. }]
  24. def _real_extract(self, url):
  25. m = re.match(self._VALID_URL, url)
  26. video_id = m.group('video_id')
  27. domain = m.group('domain')
  28. mediatype = m.group('type')
  29. # determine title and media streams from webpage
  30. html = self._download_webpage(url, video_id)
  31. title = self._html_search_regex(r'<h2>(.*?)</h2>', html, u'title')
  32. xmlurl = self._search_regex(
  33. r'(/mediathek/(?:.+)/(?:video|audio)[0-9]+-avCustom.xml)', html, u'XML URL')
  34. doc = self._download_xml(domain + xmlurl, video_id)
  35. formats = []
  36. for a in doc.findall('./assets/asset'):
  37. url_el = a.find('.//progressiveDownloadUrl')
  38. if url_el is None:
  39. continue
  40. abr = int(a.find('bitrateAudio').text) // 1000
  41. media_type = a.find('mediaType').text
  42. format = {
  43. 'abr': abr,
  44. 'filesize': int(a.find('fileSize').text),
  45. 'url': url_el.text,
  46. }
  47. vbr_el = a.find('bitrateVideo')
  48. if vbr_el is None:
  49. format.update({
  50. 'vcodec': 'none',
  51. 'format_id': u'%s-%d' % (media_type, abr),
  52. })
  53. else:
  54. vbr = int(vbr_el.text) // 1000
  55. format.update({
  56. 'vbr': vbr,
  57. 'width': int(a.find('frameWidth').text),
  58. 'height': int(a.find('frameHeight').text),
  59. 'format_id': u'%s-%d' % (media_type, vbr),
  60. })
  61. formats.append(format)
  62. formats.sort(key=lambda f: (f.get('vbr'), f['abr']))
  63. if not formats:
  64. raise ValueError('Could not find any valid formats')
  65. return {
  66. 'id': video_id,
  67. 'title': title,
  68. 'formats': formats,
  69. }