tubitv.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/(?P<id>[0-9]+)'
  13. _LOGIN_URL = 'http://tubitv.com/login'
  14. _NETRC_MACHINE = 'tubitv'
  15. _GEO_COUNTRIES = ['US']
  16. _TEST = {
  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. def _login(self):
  28. (username, password) = self._get_login_info()
  29. if username is None:
  30. return
  31. self.report_login()
  32. form_data = {
  33. 'username': username,
  34. 'password': password,
  35. }
  36. payload = urlencode_postdata(form_data)
  37. request = sanitized_Request(self._LOGIN_URL, payload)
  38. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  39. login_page = self._download_webpage(
  40. request, None, False, 'Wrong login info')
  41. if not re.search(r'id="tubi-logout"', login_page):
  42. raise ExtractorError(
  43. 'Login failed (invalid username/password)', expected=True)
  44. def _real_initialize(self):
  45. self._login()
  46. def _real_extract(self, url):
  47. video_id = self._match_id(url)
  48. video_data = self._download_json(
  49. 'http://tubitv.com/oz/videos/%s/content' % video_id, video_id)
  50. title = video_data['title']
  51. formats = self._extract_m3u8_formats(
  52. self._proto_relative_url(video_data['url']),
  53. video_id, 'mp4', 'm3u8_native')
  54. self._sort_formats(formats)
  55. thumbnails = []
  56. for thumbnail_url in video_data.get('thumbnails', []):
  57. if not thumbnail_url:
  58. continue
  59. thumbnails.append({
  60. 'url': self._proto_relative_url(thumbnail_url),
  61. })
  62. subtitles = {}
  63. for sub in video_data.get('subtitles', []):
  64. sub_url = sub.get('url')
  65. if not sub_url:
  66. continue
  67. subtitles.setdefault(sub.get('lang', 'English'), []).append({
  68. 'url': self._proto_relative_url(sub_url),
  69. })
  70. return {
  71. 'id': video_id,
  72. 'title': title,
  73. 'formats': formats,
  74. 'subtitles': subtitles,
  75. 'thumbnails': thumbnails,
  76. 'description': video_data.get('description'),
  77. 'duration': int_or_none(video_data.get('duration')),
  78. 'uploader_id': video_data.get('publisher_id'),
  79. }