orf.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import re
  2. import xml.etree.ElementTree
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urlparse,
  7. ExtractorError,
  8. find_xpath_attr,
  9. )
  10. class ORFIE(InfoExtractor):
  11. _VALID_URL = r'https?://tvthek.orf.at/(programs/.+?/episodes|topics/.+?)/(?P<id>\d+)'
  12. _TEST = {
  13. u'url': u'http://tvthek.orf.at/programs/1171769-Wetter-ZIB/episodes/6557323-Wetter',
  14. u'file': u'6566957.flv',
  15. u'info_dict': {
  16. u'title': u'Wetter',
  17. u'description': u'Christa Kummer, Marcus Wadsak und Kollegen präsentieren abwechselnd ihre täglichen Wetterprognosen für Österreich.\r \r Mehr Wetter unter wetter.ORF.at',
  18. },
  19. u'params': {
  20. # It uses rtmp
  21. u'skip_download': True,
  22. }
  23. }
  24. def _real_extract(self, url):
  25. mobj = re.match(self._VALID_URL, url)
  26. playlist_id = mobj.group('id')
  27. webpage = self._download_webpage(url, playlist_id)
  28. flash_xml = self._search_regex('ORF.flashXML = \'(.+?)\'', webpage, u'flash xml')
  29. flash_xml = compat_urlparse.parse_qs('xml='+flash_xml)['xml'][0]
  30. flash_config = xml.etree.ElementTree.fromstring(flash_xml.encode('utf-8'))
  31. playlist_json = self._search_regex(r'playlist\': \'(\[.*?\])\'', webpage, u'playlist').replace(r'\"','"')
  32. playlist = json.loads(playlist_json)
  33. videos = []
  34. ns = '{http://tempuri.org/XMLSchema.xsd}'
  35. xpath = '%(ns)sPlaylist/%(ns)sItems/%(ns)sItem' % {'ns': ns}
  36. webpage_description = self._og_search_description(webpage)
  37. for (i, (item, info)) in enumerate(zip(flash_config.findall(xpath), playlist), 1):
  38. # Get best quality url
  39. rtmp_url = None
  40. for q in ['Q6A', 'Q4A', 'Q1A']:
  41. video_url = find_xpath_attr(item, '%sVideoUrl' % ns, 'quality', q)
  42. if video_url is not None:
  43. rtmp_url = video_url.text
  44. break
  45. if rtmp_url is None:
  46. raise ExtractorError(u'Couldn\'t get video url: %s' % info['id'])
  47. description = self._html_search_regex(
  48. r'id="playlist_entry_%s".*?<p>(.*?)</p>' % i, webpage,
  49. u'description', default=webpage_description, flags=re.DOTALL)
  50. videos.append({
  51. '_type': 'video',
  52. 'id': info['id'],
  53. 'title': info['title'],
  54. 'url': rtmp_url,
  55. 'ext': 'flv',
  56. 'description': description,
  57. })
  58. return videos