You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.6 KiB

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