tvplayer.py 2.6 KB

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