logo

youtube-dl

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

tvplayer.py (2826B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. extract_attributes,
  10. try_get,
  11. urlencode_postdata,
  12. ExtractorError,
  13. )
  14. class TVPlayerIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?tvplayer\.com/watch/(?P<id>[^/?#]+)'
  16. _TEST = {
  17. 'url': 'http://tvplayer.com/watch/bbcone',
  18. 'info_dict': {
  19. 'id': '89',
  20. 'ext': 'mp4',
  21. 'title': r're:^BBC One [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  22. },
  23. 'params': {
  24. # m3u8 download
  25. 'skip_download': True,
  26. }
  27. }
  28. def _real_extract(self, url):
  29. display_id = self._match_id(url)
  30. webpage = self._download_webpage(url, display_id)
  31. current_channel = extract_attributes(self._search_regex(
  32. r'(<div[^>]+class="[^"]*current-channel[^"]*"[^>]*>)',
  33. webpage, 'channel element'))
  34. title = current_channel['data-name']
  35. resource_id = current_channel['data-id']
  36. token = self._search_regex(
  37. r'data-token=(["\'])(?P<token>(?!\1).+)\1', webpage,
  38. 'token', group='token')
  39. context = self._download_json(
  40. 'https://tvplayer.com/watch/context', display_id,
  41. 'Downloading JSON context', query={
  42. 'resource': resource_id,
  43. 'gen': token,
  44. })
  45. validate = context['validate']
  46. platform = try_get(
  47. context, lambda x: x['platform']['key'], compat_str) or 'firefox'
  48. try:
  49. response = self._download_json(
  50. 'http://api.tvplayer.com/api/v2/stream/live',
  51. display_id, 'Downloading JSON stream', headers={
  52. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  53. }, data=urlencode_postdata({
  54. 'id': resource_id,
  55. 'service': 1,
  56. 'platform': platform,
  57. 'validate': validate,
  58. }))['tvplayer']['response']
  59. except ExtractorError as e:
  60. if isinstance(e.cause, compat_HTTPError):
  61. response = self._parse_json(
  62. e.cause.read().decode(), resource_id)['tvplayer']['response']
  63. raise ExtractorError(
  64. '%s said: %s' % (self.IE_NAME, response['error']), expected=True)
  65. raise
  66. formats = self._extract_m3u8_formats(response['stream'], display_id, 'mp4')
  67. self._sort_formats(formats)
  68. return {
  69. 'id': resource_id,
  70. 'display_id': display_id,
  71. 'title': self._live_title(title),
  72. 'formats': formats,
  73. 'is_live': True,
  74. }