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.

82 lines
2.6 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import codecs
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_urllib_parse,
  8. compat_urllib_request
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. int_or_none,
  13. )
  14. class TubiTvIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?tubitv\.com/video\?id=(?P<id>[0-9]+)'
  16. _LOGIN_URL = 'http://tubitv.com/login'
  17. _NETRC_MACHINE = 'tubitv'
  18. _TEST = {
  19. 'url': 'http://tubitv.com/video?id=54411&title=The_Kitchen_Musical_-_EP01',
  20. 'info_dict': {
  21. 'id': '54411',
  22. 'ext': 'mp4',
  23. 'title': 'The Kitchen Musical - EP01',
  24. 'thumbnail': 're:^https?://.*\.png$',
  25. 'description': 'md5:37532716166069b353e8866e71fefae7',
  26. 'duration': 2407,
  27. },
  28. 'params': {
  29. 'skip_download': 'HLS download',
  30. },
  31. }
  32. def _login(self):
  33. (username, password) = self._get_login_info()
  34. if username is None:
  35. return
  36. self.report_login()
  37. form_data = {
  38. 'username': username,
  39. 'password': password,
  40. }
  41. payload = compat_urllib_parse.urlencode(form_data).encode('utf-8')
  42. request = compat_urllib_request.Request(self._LOGIN_URL, payload)
  43. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  44. login_page = self._download_webpage(
  45. request, None, False, 'Wrong login info')
  46. if not re.search(r'id="tubi-logout"', login_page):
  47. raise ExtractorError(
  48. 'Login failed (invalid username/password)', expected=True)
  49. def _real_initialize(self):
  50. self._login()
  51. def _real_extract(self, url):
  52. video_id = self._match_id(url)
  53. webpage = self._download_webpage(url, video_id)
  54. if re.search(r"<(?:DIV|div) class='login-required-screen'>", webpage):
  55. self.raise_login_required('This video requires login')
  56. title = self._og_search_title(webpage)
  57. description = self._og_search_description(webpage)
  58. thumbnail = self._og_search_thumbnail(webpage)
  59. duration = int_or_none(self._html_search_meta(
  60. 'video:duration', webpage, 'duration'))
  61. apu = self._search_regex(r"apu='([^']+)'", webpage, 'apu')
  62. m3u8_url = codecs.decode(apu, 'rot_13')[::-1]
  63. formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
  64. return {
  65. 'id': video_id,
  66. 'title': title,
  67. 'formats': formats,
  68. 'thumbnail': thumbnail,
  69. 'description': description,
  70. 'duration': duration,
  71. }