You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
4.1 KiB

11 years ago
  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. _TITLE = r'<h2>(?P<title1>[^<]+)<span>(?P<title2>[^<]+)</span></h2>'
  9. _MEDIA_XML = r'(?P<xmlurl>/mediathek/(.+)/(video|audio)([0-9]+)-avCustom.xml)'
  10. _MEDIA_STREAM_VIDEO = r'<asset>.*<frameWidth>(?P<frameWidth>[0-9]+)</frameWidth>.*<flashMediaServerApplicationURL>(?P<flashMediaServerApplicationURL>[^<]+)</flashMediaServerApplicationURL><flashMediaServerURL>(?P<flashMediaServerURL>[^<]+)</flashMediaServerURL>.*<progressiveDownloadUrl>(?P<progressiveDownloadUrl>[^<]+)</progressiveDownloadUrl></asset>'
  11. _MEDIA_STREAM_AUDIO = r'<asset>.*<mediaType>(?P<mediaType>[A-Z0-9]+)</mediaType><bitrateAudio>(?P<bitrateAudio>[0-9]+)</bitrateAudio>.*<flashMediaServerApplicationURL>(?P<flashMediaServerApplicationURL>[^<]+)</flashMediaServerApplicationURL><flashMediaServerURL>(?P<flashMediaServerURL>[^<]+)</flashMediaServerURL>.*<progressiveDownloadUrl>(?P<progressiveDownloadUrl>[^<]+)</progressiveDownloadUrl></asset>'
  12. _TESTS = [{
  13. u'url': u'http://www.mdr.de/mediathek/themen/nachrichten/video165624_zc-c5c7de76_zs-3795826d.html',
  14. u'file': u'165624.mp4',
  15. u'md5': u'95165945756198b8fa2dea10f0b04614',
  16. u'info_dict': {
  17. u"title": u"MDR aktuell Eins30 09.12.2013, 22:48 Uhr"
  18. },
  19. #u'skip': u'Requires rtmpdump' # rtmp is optional
  20. },
  21. {
  22. u'url': u' http://www.mdr.de/mediathek/radio/mdr1-radio-sachsen/audio718370_zc-67b21197_zs-1b9b2483.html',
  23. u'file': u'718370.mp4',
  24. u'md5': u'4a5b1fbb5519fb0d929c384b6ff7cb8b',
  25. u'info_dict': {
  26. u"title": u"MDR 1 RADIO SACHSEN 10.12.2013, 05:00 Uhr"
  27. },
  28. #u'skip': u'Requires rtmpdump' # rtmp is optional
  29. }]
  30. def _real_extract(self, url):
  31. # determine video id from url
  32. m = re.match(self._VALID_URL, url)
  33. video_id = m.group('video_id')
  34. domain = m.group('domain')
  35. mediatype = m.group('type')
  36. # determine title and media streams from webpage
  37. html = self._download_webpage(url, video_id)
  38. t = re.search(self._TITLE, html)
  39. if not t:
  40. raise ExtractorError(u'no title found')
  41. title = t.group('title1') + t.group('title2')
  42. m = re.search(self._MEDIA_XML, html)
  43. if not m:
  44. raise ExtractorError(u'no xml found')
  45. xmlurl = m.group('xmlurl')
  46. xml = self._download_webpage(domain+xmlurl, video_id, 'download XML').replace('\n','').replace('\r','').replace('<asset>','\n<asset>').replace('</asset>','</asset>\n')
  47. if(mediatype == "video"):
  48. streams = [mo.groupdict() for mo in re.finditer(self._MEDIA_STREAM_VIDEO, xml)]
  49. if not streams:
  50. raise ExtractorError(u'no media found')
  51. # choose default media type and highest quality for now
  52. stream = max([s for s in streams if s["progressiveDownloadUrl"].startswith("http://") ],
  53. key=lambda s: int(s["frameWidth"]))
  54. else:
  55. streams = [mo.groupdict() for mo in re.finditer(self._MEDIA_STREAM_AUDIO, xml)]
  56. if not streams:
  57. raise ExtractorError(u'no media found')
  58. # choose default media type (MP4) and highest quality for now
  59. stream = max([s for s in streams if s["progressiveDownloadUrl"].startswith("http://") and s["mediaType"] == "MP4" ],
  60. key=lambda s: int(s["bitrateAudio"]))
  61. # there's two possibilities: RTMP stream or HTTP download
  62. info = {'id': video_id, 'title': title, 'ext': 'mp4'}
  63. if not stream["progressiveDownloadUrl"]:
  64. self.to_screen(u'RTMP download detected')
  65. assert stream['flashMediaServerURL'].startswith('mp4:')
  66. info["url"] = stream["flashMediaServerApplicationURL"]
  67. info["play_path"] = stream['flashMediaServerURL']
  68. else:
  69. assert stream["progressiveDownloadUrl"].endswith('.mp4')
  70. info["url"] = stream["progressiveDownloadUrl"]
  71. return [info]