dplay.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import time
  6. from .common import InfoExtractor
  7. from ..utils import int_or_none
  8. class DPlayIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?P<domain>it\.dplay\.com|www\.dplay\.(?:dk|se|no))/[^/]+/(?P<id>[^/?#]+)'
  10. _TESTS = [{
  11. # geo restricted, via direct unsigned hls URL
  12. 'url': 'http://it.dplay.com/take-me-out/stagione-1-episodio-25/',
  13. 'info_dict': {
  14. 'id': '1255600',
  15. 'display_id': 'stagione-1-episodio-25',
  16. 'ext': 'mp4',
  17. 'title': 'Episodio 25',
  18. 'description': 'md5:cae5f40ad988811b197d2d27a53227eb',
  19. 'duration': 2761,
  20. 'timestamp': 1454701800,
  21. 'upload_date': '20160205',
  22. 'creator': 'RTIT',
  23. 'series': 'Take me out',
  24. 'season_number': 1,
  25. 'episode_number': 25,
  26. 'age_limit': 0,
  27. },
  28. 'expected_warnings': ['Unable to download f4m manifest'],
  29. }, {
  30. # non geo restricted, via secure api
  31. 'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
  32. 'info_dict': {
  33. 'id': '3172',
  34. 'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
  35. 'ext': 'flv',
  36. 'title': 'Svensken lär sig njuta av livet',
  37. 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
  38. 'duration': 2650,
  39. 'timestamp': 1365454320,
  40. 'upload_date': '20130408',
  41. 'creator': 'Kanal 5 (Home)',
  42. 'series': 'Nugammalt - 77 händelser som format Sverige',
  43. 'season_number': 1,
  44. 'episode_number': 1,
  45. 'age_limit': 0,
  46. },
  47. }, {
  48. # geo restricted, via secure api
  49. 'url': 'http://www.dplay.dk/mig-og-min-mor/season-6-episode-12/',
  50. 'info_dict': {
  51. 'id': '70816',
  52. 'display_id': 'season-6-episode-12',
  53. 'ext': 'flv',
  54. 'title': 'Episode 12',
  55. 'description': 'md5:9c86e51a93f8a4401fc9641ef9894c90',
  56. 'duration': 2563,
  57. 'timestamp': 1429696800,
  58. 'upload_date': '20150422',
  59. 'creator': 'Kanal 4',
  60. 'series': 'Mig og min mor',
  61. 'season_number': 6,
  62. 'episode_number': 12,
  63. 'age_limit': 0,
  64. },
  65. }, {
  66. # geo restricted, via direct unsigned hls URL
  67. 'url': 'http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/',
  68. 'only_matching': True,
  69. }]
  70. def _real_extract(self, url):
  71. mobj = re.match(self._VALID_URL, url)
  72. display_id = mobj.group('id')
  73. domain = mobj.group('domain')
  74. webpage = self._download_webpage(url, display_id)
  75. video_id = self._search_regex(
  76. r'data-video-id=["\'](\d+)', webpage, 'video id')
  77. info = self._download_json(
  78. 'http://%s/api/v2/ajax/videos?video_id=%s' % (domain, video_id),
  79. video_id)['data'][0]
  80. title = info['title']
  81. PROTOCOLS = ('hls', 'hds')
  82. formats = []
  83. def extract_formats(protocol, manifest_url):
  84. if protocol == 'hls':
  85. formats.extend(self._extract_m3u8_formats(
  86. manifest_url, video_id, ext='mp4',
  87. entry_protocol='m3u8_native', m3u8_id=protocol, fatal=False))
  88. elif protocol == 'hds':
  89. formats.extend(self._extract_f4m_formats(
  90. manifest_url + '&hdcore=3.8.0&plugin=flowplayer-3.8.0.0',
  91. video_id, f4m_id=protocol, fatal=False))
  92. domain_tld = domain.split('.')[-1]
  93. if domain_tld in ('se', 'dk', 'no'):
  94. for protocol in PROTOCOLS:
  95. # Providing dsc-geo allows to bypass geo restriction in some cases
  96. self._set_cookie(
  97. 'secure.dplay.%s' % domain_tld, 'dsc-geo',
  98. json.dumps({
  99. 'countryCode': domain_tld.upper(),
  100. 'expiry': (time.time() + 20 * 60) * 1000,
  101. }))
  102. stream = self._download_json(
  103. 'https://secure.dplay.%s/secure/api/v2/user/authorization/stream/%s?stream_type=%s'
  104. % (domain_tld, video_id, protocol), video_id,
  105. 'Downloading %s stream JSON' % protocol, fatal=False)
  106. if stream and stream.get(protocol):
  107. extract_formats(protocol, stream[protocol])
  108. # The last resort is to try direct unsigned hls/hds URLs from info dictionary.
  109. # Sometimes this does work even when secure API with dsc-geo has failed (e.g.
  110. # http://www.dplay.no/pga-tour/season-1-hoydepunkter-18-21-februar/).
  111. if not formats:
  112. for protocol in PROTOCOLS:
  113. if info.get(protocol):
  114. extract_formats(protocol, info[protocol])
  115. self._sort_formats(formats)
  116. return {
  117. 'id': video_id,
  118. 'display_id': display_id,
  119. 'title': title,
  120. 'description': info.get('video_metadata_longDescription'),
  121. 'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
  122. 'timestamp': int_or_none(info.get('video_publish_date')),
  123. 'creator': info.get('video_metadata_homeChannel'),
  124. 'series': info.get('video_metadata_show'),
  125. 'season_number': int_or_none(info.get('season')),
  126. 'episode_number': int_or_none(info.get('episode')),
  127. 'age_limit': int_or_none(info.get('minimum_age')),
  128. 'formats': formats,
  129. }