logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

dplay.py (14744B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. determine_ext,
  9. ExtractorError,
  10. float_or_none,
  11. int_or_none,
  12. strip_or_none,
  13. unified_timestamp,
  14. )
  15. class DPlayIE(InfoExtractor):
  16. _PATH_REGEX = r'/(?P<id>[^/]+/[^/?#]+)'
  17. _VALID_URL = r'''(?x)https?://
  18. (?P<domain>
  19. (?:www\.)?(?P<host>d
  20. (?:
  21. play\.(?P<country>dk|fi|jp|se|no)|
  22. iscoveryplus\.(?P<plus_country>dk|es|fi|it|se|no)
  23. )
  24. )|
  25. (?P<subdomain_country>es|it)\.dplay\.com
  26. )/[^/]+''' + _PATH_REGEX
  27. _TESTS = [{
  28. # non geo restricted, via secure api, unsigned download hls URL
  29. 'url': 'https://www.dplay.se/videos/nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
  30. 'info_dict': {
  31. 'id': '13628',
  32. 'display_id': 'nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
  33. 'ext': 'mp4',
  34. 'title': 'Svensken lär sig njuta av livet',
  35. 'description': 'md5:d3819c9bccffd0fe458ca42451dd50d8',
  36. 'duration': 2649.856,
  37. 'timestamp': 1365453720,
  38. 'upload_date': '20130408',
  39. 'creator': 'Kanal 5',
  40. 'series': 'Nugammalt - 77 händelser som format Sverige',
  41. 'season_number': 1,
  42. 'episode_number': 1,
  43. },
  44. 'params': {
  45. 'format': 'bestvideo',
  46. 'skip_download': True,
  47. },
  48. }, {
  49. # geo restricted, via secure api, unsigned download hls URL
  50. 'url': 'http://www.dplay.dk/videoer/ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
  51. 'info_dict': {
  52. 'id': '104465',
  53. 'display_id': 'ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
  54. 'ext': 'mp4',
  55. 'title': 'Ted Bundy: Mind Of A Monster',
  56. 'description': 'md5:8b780f6f18de4dae631668b8a9637995',
  57. 'duration': 5290.027,
  58. 'timestamp': 1570694400,
  59. 'upload_date': '20191010',
  60. 'creator': 'ID - Investigation Discovery',
  61. 'series': 'Ted Bundy: Mind Of A Monster',
  62. 'season_number': 1,
  63. 'episode_number': 1,
  64. },
  65. 'params': {
  66. 'format': 'bestvideo',
  67. 'skip_download': True,
  68. },
  69. }, {
  70. # disco-api
  71. 'url': 'https://www.dplay.no/videoer/i-kongens-klr/sesong-1-episode-7',
  72. 'info_dict': {
  73. 'id': '40206',
  74. 'display_id': 'i-kongens-klr/sesong-1-episode-7',
  75. 'ext': 'mp4',
  76. 'title': 'Episode 7',
  77. 'description': 'md5:e3e1411b2b9aebeea36a6ec5d50c60cf',
  78. 'duration': 2611.16,
  79. 'timestamp': 1516726800,
  80. 'upload_date': '20180123',
  81. 'series': 'I kongens klær',
  82. 'season_number': 1,
  83. 'episode_number': 7,
  84. },
  85. 'params': {
  86. 'format': 'bestvideo',
  87. 'skip_download': True,
  88. },
  89. 'skip': 'Available for Premium users',
  90. }, {
  91. 'url': 'http://it.dplay.com/nove/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij/',
  92. 'md5': '2b808ffb00fc47b884a172ca5d13053c',
  93. 'info_dict': {
  94. 'id': '6918',
  95. 'display_id': 'biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij',
  96. 'ext': 'mp4',
  97. 'title': 'Luigi Di Maio: la psicosi di Stanislawskij',
  98. 'description': 'md5:3c7a4303aef85868f867a26f5cc14813',
  99. 'thumbnail': r're:^https?://.*\.jpe?g',
  100. 'upload_date': '20160524',
  101. 'timestamp': 1464076800,
  102. 'series': 'Biografie imbarazzanti',
  103. 'season_number': 1,
  104. 'episode': 'Episode 1',
  105. 'episode_number': 1,
  106. },
  107. }, {
  108. 'url': 'https://es.dplay.com/dmax/la-fiebre-del-oro/temporada-8-episodio-1/',
  109. 'info_dict': {
  110. 'id': '21652',
  111. 'display_id': 'la-fiebre-del-oro/temporada-8-episodio-1',
  112. 'ext': 'mp4',
  113. 'title': 'Episodio 1',
  114. 'description': 'md5:b9dcff2071086e003737485210675f69',
  115. 'thumbnail': r're:^https?://.*\.png',
  116. 'upload_date': '20180709',
  117. 'timestamp': 1531173540,
  118. 'series': 'La fiebre del oro',
  119. 'season_number': 8,
  120. 'episode': 'Episode 1',
  121. 'episode_number': 1,
  122. },
  123. 'params': {
  124. 'skip_download': True,
  125. },
  126. }, {
  127. 'url': 'https://www.dplay.fi/videot/shifting-gears-with-aaron-kaufman/episode-16',
  128. 'only_matching': True,
  129. }, {
  130. 'url': 'https://www.dplay.jp/video/gold-rush/24086',
  131. 'only_matching': True,
  132. }, {
  133. 'url': 'https://www.discoveryplus.se/videos/nugammalt-77-handelser-som-format-sverige/nugammalt-77-handelser-som-format-sverige-101',
  134. 'only_matching': True,
  135. }, {
  136. 'url': 'https://www.discoveryplus.dk/videoer/ted-bundy-mind-of-a-monster/ted-bundy-mind-of-a-monster',
  137. 'only_matching': True,
  138. }, {
  139. 'url': 'https://www.discoveryplus.no/videoer/i-kongens-klr/sesong-1-episode-7',
  140. 'only_matching': True,
  141. }, {
  142. 'url': 'https://www.discoveryplus.it/videos/biografie-imbarazzanti/luigi-di-maio-la-psicosi-di-stanislawskij',
  143. 'only_matching': True,
  144. }, {
  145. 'url': 'https://www.discoveryplus.es/videos/la-fiebre-del-oro/temporada-8-episodio-1',
  146. 'only_matching': True,
  147. }, {
  148. 'url': 'https://www.discoveryplus.fi/videot/shifting-gears-with-aaron-kaufman/episode-16',
  149. 'only_matching': True,
  150. }]
  151. def _process_errors(self, e, geo_countries):
  152. info = self._parse_json(e.cause.read().decode('utf-8'), None)
  153. error = info['errors'][0]
  154. error_code = error.get('code')
  155. if error_code == 'access.denied.geoblocked':
  156. self.raise_geo_restricted(countries=geo_countries)
  157. elif error_code in ('access.denied.missingpackage', 'invalid.token'):
  158. raise ExtractorError(
  159. 'This video is only available for registered users. You may want to use --cookies.', expected=True)
  160. raise ExtractorError(info['errors'][0]['detail'], expected=True)
  161. def _update_disco_api_headers(self, headers, disco_base, display_id, realm):
  162. headers['Authorization'] = 'Bearer ' + self._download_json(
  163. disco_base + 'token', display_id, 'Downloading token',
  164. query={
  165. 'realm': realm,
  166. })['data']['attributes']['token']
  167. def _download_video_playback_info(self, disco_base, video_id, headers):
  168. streaming = self._download_json(
  169. disco_base + 'playback/videoPlaybackInfo/' + video_id,
  170. video_id, headers=headers)['data']['attributes']['streaming']
  171. streaming_list = []
  172. for format_id, format_dict in streaming.items():
  173. streaming_list.append({
  174. 'type': format_id,
  175. 'url': format_dict.get('url'),
  176. })
  177. return streaming_list
  178. def _get_disco_api_info(self, url, display_id, disco_host, realm, country):
  179. geo_countries = [country.upper()]
  180. self._initialize_geo_bypass({
  181. 'countries': geo_countries,
  182. })
  183. disco_base = 'https://%s/' % disco_host
  184. headers = {
  185. 'Referer': url,
  186. }
  187. self._update_disco_api_headers(headers, disco_base, display_id, realm)
  188. try:
  189. video = self._download_json(
  190. disco_base + 'content/videos/' + display_id, display_id,
  191. headers=headers, query={
  192. 'fields[channel]': 'name',
  193. 'fields[image]': 'height,src,width',
  194. 'fields[show]': 'name',
  195. 'fields[tag]': 'name',
  196. 'fields[video]': 'description,episodeNumber,name,publishStart,seasonNumber,videoDuration',
  197. 'include': 'images,primaryChannel,show,tags'
  198. })
  199. except ExtractorError as e:
  200. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  201. self._process_errors(e, geo_countries)
  202. raise
  203. video_id = video['data']['id']
  204. info = video['data']['attributes']
  205. title = info['name'].strip()
  206. formats = []
  207. try:
  208. streaming = self._download_video_playback_info(
  209. disco_base, video_id, headers)
  210. except ExtractorError as e:
  211. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  212. self._process_errors(e, geo_countries)
  213. raise
  214. for format_dict in streaming:
  215. if not isinstance(format_dict, dict):
  216. continue
  217. format_url = format_dict.get('url')
  218. if not format_url:
  219. continue
  220. format_id = format_dict.get('type')
  221. ext = determine_ext(format_url)
  222. if format_id == 'dash' or ext == 'mpd':
  223. formats.extend(self._extract_mpd_formats(
  224. format_url, display_id, mpd_id='dash', fatal=False))
  225. elif format_id == 'hls' or ext == 'm3u8':
  226. formats.extend(self._extract_m3u8_formats(
  227. format_url, display_id, 'mp4',
  228. entry_protocol='m3u8_native', m3u8_id='hls',
  229. fatal=False))
  230. else:
  231. formats.append({
  232. 'url': format_url,
  233. 'format_id': format_id,
  234. })
  235. self._sort_formats(formats)
  236. creator = series = None
  237. tags = []
  238. thumbnails = []
  239. included = video.get('included') or []
  240. if isinstance(included, list):
  241. for e in included:
  242. attributes = e.get('attributes')
  243. if not attributes:
  244. continue
  245. e_type = e.get('type')
  246. if e_type == 'channel':
  247. creator = attributes.get('name')
  248. elif e_type == 'image':
  249. src = attributes.get('src')
  250. if src:
  251. thumbnails.append({
  252. 'url': src,
  253. 'width': int_or_none(attributes.get('width')),
  254. 'height': int_or_none(attributes.get('height')),
  255. })
  256. if e_type == 'show':
  257. series = attributes.get('name')
  258. elif e_type == 'tag':
  259. name = attributes.get('name')
  260. if name:
  261. tags.append(name)
  262. return {
  263. 'id': video_id,
  264. 'display_id': display_id,
  265. 'title': title,
  266. 'description': strip_or_none(info.get('description')),
  267. 'duration': float_or_none(info.get('videoDuration'), 1000),
  268. 'timestamp': unified_timestamp(info.get('publishStart')),
  269. 'series': series,
  270. 'season_number': int_or_none(info.get('seasonNumber')),
  271. 'episode_number': int_or_none(info.get('episodeNumber')),
  272. 'creator': creator,
  273. 'tags': tags,
  274. 'thumbnails': thumbnails,
  275. 'formats': formats,
  276. }
  277. def _real_extract(self, url):
  278. mobj = re.match(self._VALID_URL, url)
  279. display_id = mobj.group('id')
  280. domain = mobj.group('domain').lstrip('www.')
  281. country = mobj.group('country') or mobj.group('subdomain_country') or mobj.group('plus_country')
  282. host = 'disco-api.' + domain if domain[0] == 'd' else 'eu2-prod.disco-api.com'
  283. return self._get_disco_api_info(
  284. url, display_id, host, 'dplay' + country, country)
  285. class DiscoveryPlusIE(DPlayIE):
  286. _VALID_URL = r'https?://(?:www\.)?discoveryplus\.com/video' + DPlayIE._PATH_REGEX
  287. _TESTS = [{
  288. 'url': 'https://www.discoveryplus.com/video/property-brothers-forever-home/food-and-family',
  289. 'info_dict': {
  290. 'id': '1140794',
  291. 'display_id': 'property-brothers-forever-home/food-and-family',
  292. 'ext': 'mp4',
  293. 'title': 'Food and Family',
  294. 'description': 'The brothers help a Richmond family expand their single-level home.',
  295. 'duration': 2583.113,
  296. 'timestamp': 1609304400,
  297. 'upload_date': '20201230',
  298. 'creator': 'HGTV',
  299. 'series': 'Property Brothers: Forever Home',
  300. 'season_number': 1,
  301. 'episode_number': 1,
  302. },
  303. 'skip': 'Available for Premium users',
  304. }]
  305. def _update_disco_api_headers(self, headers, disco_base, display_id, realm):
  306. headers['x-disco-client'] = 'WEB:UNKNOWN:dplus_us:15.0.0'
  307. def _download_video_playback_info(self, disco_base, video_id, headers):
  308. return self._download_json(
  309. disco_base + 'playback/v3/videoPlaybackInfo',
  310. video_id, headers=headers, data=json.dumps({
  311. 'deviceInfo': {
  312. 'adBlocker': False,
  313. },
  314. 'videoId': video_id,
  315. 'wisteriaProperties': {
  316. 'platform': 'desktop',
  317. 'product': 'dplus_us',
  318. },
  319. }).encode('utf-8'))['data']['attributes']['streaming']
  320. def _real_extract(self, url):
  321. display_id = self._match_id(url)
  322. return self._get_disco_api_info(
  323. url, display_id, 'us1-prod-direct.discoveryplus.com', 'go', 'us')
  324. class HGTVDeIE(DPlayIE):
  325. _VALID_URL = r'https?://de\.hgtv\.com/sendungen' + DPlayIE._PATH_REGEX
  326. _TESTS = [{
  327. 'url': 'https://de.hgtv.com/sendungen/tiny-house-klein-aber-oho/wer-braucht-schon-eine-toilette/',
  328. 'info_dict': {
  329. 'id': '151205',
  330. 'display_id': 'tiny-house-klein-aber-oho/wer-braucht-schon-eine-toilette',
  331. 'ext': 'mp4',
  332. 'title': 'Wer braucht schon eine Toilette',
  333. 'description': 'md5:05b40a27e7aed2c9172de34d459134e2',
  334. 'duration': 1177.024,
  335. 'timestamp': 1595705400,
  336. 'upload_date': '20200725',
  337. 'creator': 'HGTV',
  338. 'series': 'Tiny House - klein, aber oho',
  339. 'season_number': 3,
  340. 'episode_number': 3,
  341. },
  342. 'params': {
  343. 'format': 'bestvideo',
  344. },
  345. }]
  346. def _real_extract(self, url):
  347. display_id = self._match_id(url)
  348. return self._get_disco_api_info(
  349. url, display_id, 'eu1-prod.disco-api.com', 'hgtv', 'de')