xstream.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. find_xpath_attr,
  5. int_or_none,
  6. parse_iso8601,
  7. xpath_text,
  8. xpath_with_ns,
  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. f'http://frontend.xstream.dk/{partner_id}/feed/video/?platform=web&id={video_id}',
  41. video_id)
  42. NS_MAP = {
  43. 'atom': 'http://www.w3.org/2005/Atom',
  44. 'xt': 'http://xstream.dk/',
  45. 'media': 'http://search.yahoo.com/mrss/',
  46. }
  47. entry = data.find(xpath_with_ns('./atom:entry', NS_MAP))
  48. title = xpath_text(
  49. entry, xpath_with_ns('./atom:title', NS_MAP), 'title')
  50. description = xpath_text(
  51. entry, xpath_with_ns('./atom:summary', NS_MAP), 'description')
  52. timestamp = parse_iso8601(xpath_text(
  53. entry, xpath_with_ns('./atom:published', NS_MAP), 'upload date'))
  54. formats = []
  55. media_group = entry.find(xpath_with_ns('./media:group', NS_MAP))
  56. for media_content in media_group.findall(xpath_with_ns('./media:content', NS_MAP)):
  57. media_url = media_content.get('url')
  58. if not media_url:
  59. continue
  60. tbr = int_or_none(media_content.get('bitrate'))
  61. mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', media_url)
  62. if mobj:
  63. formats.append({
  64. 'url': mobj.group('url'),
  65. 'play_path': 'mp4:{}'.format(mobj.group('playpath')),
  66. 'app': mobj.group('app'),
  67. 'ext': 'flv',
  68. 'tbr': tbr,
  69. 'format_id': 'rtmp-%d' % tbr,
  70. })
  71. else:
  72. formats.append({
  73. 'url': media_url,
  74. 'tbr': tbr,
  75. })
  76. link = find_xpath_attr(
  77. entry, xpath_with_ns('./atom:link', NS_MAP), 'rel', 'original')
  78. if link is not None:
  79. formats.append({
  80. 'url': link.get('href'),
  81. 'format_id': link.get('rel'),
  82. 'quality': 1,
  83. })
  84. thumbnails = [{
  85. 'url': splash.get('url'),
  86. 'width': int_or_none(splash.get('width')),
  87. 'height': int_or_none(splash.get('height')),
  88. } for splash in media_group.findall(xpath_with_ns('./xt:splash', NS_MAP))]
  89. return {
  90. 'id': video_id,
  91. 'title': title,
  92. 'description': description,
  93. 'timestamp': timestamp,
  94. 'formats': formats,
  95. 'thumbnails': thumbnails,
  96. }
  97. def _real_extract(self, url):
  98. mobj = self._match_valid_url(url)
  99. partner_id = mobj.group('partner_id')
  100. video_id = mobj.group('id')
  101. return self._extract_video_info(partner_id, video_id)