xstream.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. parse_iso8601,
  6. xpath_with_ns,
  7. xpath_text,
  8. find_xpath_attr,
  9. )
  10. class XstreamIE(InfoExtractor):
  11. _VALID_URL = r'''(?x)
  12. (?:
  13. xstream:|
  14. https?://frontend\.xstream\.(?:dk|net)/
  15. )
  16. (?P<partner_id>[^/]+)
  17. (?:
  18. :|
  19. /feed/video/\?.*?\bid=
  20. )
  21. (?P<id>\d+)
  22. '''
  23. _TESTS = [{
  24. 'url': 'http://frontend.xstream.dk/btno/feed/video/?platform=web&id=86588',
  25. 'md5': 'd7d17e3337dc80de6d3a540aefbe441b',
  26. 'info_dict': {
  27. 'id': '86588',
  28. 'ext': 'mov',
  29. 'title': 'Otto Wollertsen',
  30. 'description': 'Vestlendingen Otto Fredrik Wollertsen',
  31. 'timestamp': 1430473209,
  32. 'upload_date': '20150501',
  33. },
  34. }, {
  35. 'url': 'http://frontend.xstream.dk/ap/feed/video/?platform=web&id=21039',
  36. 'only_matching': True,
  37. }]
  38. def _extract_video_info(self, partner_id, video_id):
  39. data = self._download_xml(
  40. 'http://frontend.xstream.dk/%s/feed/video/?platform=web&id=%s'
  41. % (partner_id, video_id),
  42. video_id)
  43. NS_MAP = {
  44. 'atom': 'http://www.w3.org/2005/Atom',
  45. 'xt': 'http://xstream.dk/',
  46. 'media': 'http://search.yahoo.com/mrss/',
  47. }
  48. entry = data.find(xpath_with_ns('./atom:entry', NS_MAP))
  49. title = xpath_text(
  50. entry, xpath_with_ns('./atom:title', NS_MAP), 'title')
  51. description = xpath_text(
  52. entry, xpath_with_ns('./atom:summary', NS_MAP), 'description')
  53. timestamp = parse_iso8601(xpath_text(
  54. entry, xpath_with_ns('./atom:published', NS_MAP), 'upload date'))
  55. formats = []
  56. media_group = entry.find(xpath_with_ns('./media:group', NS_MAP))
  57. for media_content in media_group.findall(xpath_with_ns('./media:content', NS_MAP)):
  58. media_url = media_content.get('url')
  59. if not media_url:
  60. continue
  61. tbr = int_or_none(media_content.get('bitrate'))
  62. mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', media_url)
  63. if mobj:
  64. formats.append({
  65. 'url': mobj.group('url'),
  66. 'play_path': 'mp4:%s' % mobj.group('playpath'),
  67. 'app': mobj.group('app'),
  68. 'ext': 'flv',
  69. 'tbr': tbr,
  70. 'format_id': 'rtmp-%d' % tbr,
  71. })
  72. else:
  73. formats.append({
  74. 'url': media_url,
  75. 'tbr': tbr,
  76. })
  77. link = find_xpath_attr(
  78. entry, xpath_with_ns('./atom:link', NS_MAP), 'rel', 'original')
  79. if link is not None:
  80. formats.append({
  81. 'url': link.get('href'),
  82. 'format_id': link.get('rel'),
  83. 'quality': 1,
  84. })
  85. thumbnails = [{
  86. 'url': splash.get('url'),
  87. 'width': int_or_none(splash.get('width')),
  88. 'height': int_or_none(splash.get('height')),
  89. } for splash in media_group.findall(xpath_with_ns('./xt:splash', NS_MAP))]
  90. return {
  91. 'id': video_id,
  92. 'title': title,
  93. 'description': description,
  94. 'timestamp': timestamp,
  95. 'formats': formats,
  96. 'thumbnails': thumbnails,
  97. }
  98. def _real_extract(self, url):
  99. mobj = self._match_valid_url(url)
  100. partner_id = mobj.group('partner_id')
  101. video_id = mobj.group('id')
  102. return self._extract_video_info(partner_id, video_id)