tubitv.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. sanitized_Request,
  9. urlencode_postdata,
  10. )
  11. class TubiTvIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?tubitv\.com/(?:video|movies|tv-shows)/(?P<id>[0-9]+)'
  13. _LOGIN_URL = 'http://tubitv.com/login'
  14. _NETRC_MACHINE = 'tubitv'
  15. _GEO_COUNTRIES = ['US']
  16. _TESTS = [{
  17. 'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday',
  18. 'md5': '43ac06be9326f41912dc64ccf7a80320',
  19. 'info_dict': {
  20. 'id': '283829',
  21. 'ext': 'mp4',
  22. 'title': 'The Comedian at The Friday',
  23. 'description': 'A stand up comedian is forced to look at the decisions in his life while on a one week trip to the west coast.',
  24. 'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434',
  25. },
  26. }, {
  27. 'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories',
  28. 'only_matching': True,
  29. }, {
  30. 'url': 'http://tubitv.com/movies/383676/tracker',
  31. 'only_matching': True,
  32. }]
  33. def _login(self):
  34. username, password = self._get_login_info()
  35. if username is None:
  36. return
  37. self.report_login()
  38. form_data = {
  39. 'username': username,
  40. 'password': password,
  41. }
  42. payload = urlencode_postdata(form_data)
  43. request = sanitized_Request(self._LOGIN_URL, payload)
  44. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  45. login_page = self._download_webpage(
  46. request, None, False, 'Wrong login info')
  47. if not re.search(r'id="tubi-logout"', login_page):
  48. raise ExtractorError(
  49. 'Login failed (invalid username/password)', expected=True)
  50. def _real_initialize(self):
  51. self._login()
  52. def _real_extract(self, url):
  53. video_id = self._match_id(url)
  54. video_data = self._download_json(
  55. 'http://tubitv.com/oz/videos/%s/content' % video_id, video_id)
  56. title = video_data['title']
  57. formats = self._extract_m3u8_formats(
  58. self._proto_relative_url(video_data['url']),
  59. video_id, 'mp4', 'm3u8_native')
  60. self._sort_formats(formats)
  61. thumbnails = []
  62. for thumbnail_url in video_data.get('thumbnails', []):
  63. if not thumbnail_url:
  64. continue
  65. thumbnails.append({
  66. 'url': self._proto_relative_url(thumbnail_url),
  67. })
  68. subtitles = {}
  69. for sub in video_data.get('subtitles', []):
  70. sub_url = sub.get('url')
  71. if not sub_url:
  72. continue
  73. subtitles.setdefault(sub.get('lang', 'English'), []).append({
  74. 'url': self._proto_relative_url(sub_url),
  75. })
  76. return {
  77. 'id': video_id,
  78. 'title': title,
  79. 'formats': formats,
  80. 'subtitles': subtitles,
  81. 'thumbnails': thumbnails,
  82. 'description': video_data.get('description'),
  83. 'duration': int_or_none(video_data.get('duration')),
  84. 'uploader_id': video_data.get('publisher_id'),
  85. }