srf.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. parse_iso8601,
  8. xpath_text,
  9. )
  10. class SrfIE(InfoExtractor):
  11. _VALID_URL = r'http://www\.srf\.ch/play(?:er)?/tv/[^/]+/video/(?P<display_id>[^?]+)\?id=(?P<id>[0-9a-f\-]{36})'
  12. _TESTS = [{
  13. 'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  14. 'md5': '4cd93523723beff51bb4bee974ee238d',
  15. 'info_dict': {
  16. 'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  17. 'display_id': 'snowden-beantragt-asyl-in-russland',
  18. 'ext': 'm4v',
  19. 'upload_date': '20130701',
  20. 'title': 'Snowden beantragt Asyl in Russland',
  21. 'timestamp': 1372713995,
  22. }
  23. }, {
  24. # No Speichern (Save) button
  25. 'url': 'http://www.srf.ch/play/tv/top-gear/video/jaguar-xk120-shadow-und-tornado-dampflokomotive?id=677f5829-e473-4823-ac83-a1087fe97faa',
  26. 'info_dict': {
  27. 'id': '677f5829-e473-4823-ac83-a1087fe97faa',
  28. 'display_id': 'jaguar-xk120-shadow-und-tornado-dampflokomotive',
  29. 'ext': 'mp4',
  30. 'upload_date': '20130710',
  31. 'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
  32. 'timestamp': 1373493600,
  33. },
  34. 'params': {
  35. # Require ffmpeg/avconv
  36. 'skip_download': True,
  37. }
  38. }, {
  39. 'url': 'http://www.srf.ch/player/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  40. 'only_matching': True,
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. video_data = self._download_xml(
  45. 'http://il.srgssr.ch/integrationlayer/1.0/ue/srf/video/play/%s.xml' % video_id,
  46. video_id)
  47. display_id = re.match(self._VALID_URL, url).group('display_id')
  48. title = xpath_text(
  49. video_data, './AssetMetadatas/AssetMetadata/title', fatal=True)
  50. thumbnails = [{
  51. 'url': s.text
  52. } for s in video_data.findall('.//ImageRepresentation/url')]
  53. timestamp = parse_iso8601(xpath_text(video_data, './createdDate'))
  54. # The <duration> field in XML is different from the exact duration, skipping
  55. formats = []
  56. for item in video_data.findall('./Playlists/Playlist') + video_data.findall('./Downloads/Download'):
  57. url_node = item.find('url')
  58. quality = url_node.attrib['quality']
  59. full_url = url_node.text
  60. original_ext = determine_ext(full_url)
  61. if original_ext == 'f4m':
  62. full_url += '?hdcore=3.4.0' # Without this, you get a 403 error
  63. formats.append({
  64. 'url': full_url,
  65. 'ext': 'mp4' if original_ext == 'm3u8' else original_ext,
  66. 'format_id': '%s-%s' % (quality, item.attrib['protocol']),
  67. 'preference': 0 if 'HD' in quality else -1,
  68. })
  69. self._sort_formats(formats)
  70. return {
  71. 'id': video_id,
  72. 'display_id': display_id,
  73. 'formats': formats,
  74. 'title': title,
  75. 'thumbnails': thumbnails,
  76. 'timestamp': timestamp,
  77. }