togglesg.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. int_or_none,
  9. parse_iso8601,
  10. sanitized_Request,
  11. )
  12. class ToggleSgIE(InfoExtractor):
  13. IE_NAME = 'togglesg'
  14. _VALID_URL = r'https?://video\.toggle\.sg/(?:en|zh)/(?:series|clips|movies)/.+?/(?P<id>[0-9]+)'
  15. _TESTS = [{
  16. 'url': 'http://video.toggle.sg/en/series/lion-moms-tif/trailers/lion-moms-premier/343115',
  17. 'info_dict': {
  18. 'id': '343115',
  19. 'ext': 'mp4',
  20. 'title': 'Lion Moms Premiere',
  21. 'description': 'md5:aea1149404bff4d7f7b6da11fafd8e6b',
  22. 'upload_date': '20150910',
  23. 'timestamp': 1441858274,
  24. },
  25. 'params': {
  26. 'skip_download': 'm3u8 download',
  27. }
  28. }, {
  29. 'note': 'DRM-protected video',
  30. 'url': 'http://video.toggle.sg/en/movies/dug-s-special-mission/341413',
  31. 'info_dict': {
  32. 'id': '341413',
  33. 'ext': 'wvm',
  34. 'title': 'Dug\'s Special Mission',
  35. 'description': 'md5:e86c6f4458214905c1772398fabc93e0',
  36. 'upload_date': '20150827',
  37. 'timestamp': 1440644006,
  38. },
  39. 'params': {
  40. 'skip_download': 'DRM-protected wvm download',
  41. }
  42. }, {
  43. 'note': 'm3u8 links are geo-restricted, but Android/mp4 is okay',
  44. 'url': 'http://video.toggle.sg/en/series/28th-sea-games-5-show/ep11/332861',
  45. 'info_dict': {
  46. 'id': '332861',
  47. 'ext': 'mp4',
  48. 'title': '28th SEA Games (5 Show) - Episode 11',
  49. 'description': 'md5:3cd4f5f56c7c3b1340c50a863f896faa',
  50. 'upload_date': '20150605',
  51. 'timestamp': 1433480166,
  52. },
  53. 'params': {
  54. 'skip_download': 'DRM-protected wvm download',
  55. },
  56. 'skip': 'm3u8 links are geo-restricted'
  57. }, {
  58. 'url': 'http://video.toggle.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
  59. 'only_matching': True,
  60. }, {
  61. 'url': 'http://video.toggle.sg/zh/series/zero-calling-s2-hd/ep13/336367',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'http://video.toggle.sg/en/series/vetri-s2/webisodes/jeeva-is-an-orphan-vetri-s2-webisode-7/342302',
  65. 'only_matching': True,
  66. }, {
  67. 'url': 'http://video.toggle.sg/en/movies/seven-days/321936',
  68. 'only_matching': True,
  69. }]
  70. _FORMAT_PREFERENCES = {
  71. 'wvm-STBMain': -10,
  72. 'wvm-iPadMain': -20,
  73. 'wvm-iPhoneMain': -30,
  74. 'wvm-Android': -40,
  75. }
  76. _API_USER = 'tvpapi_147'
  77. _API_PASS = '11111'
  78. def _real_extract(self, url):
  79. video_id = self._match_id(url)
  80. webpage = self._download_webpage(
  81. url, video_id, note='Downloading video page')
  82. api_user = self._search_regex(
  83. r'apiUser\s*:\s*(["\'])(?P<user>.+?)\1', webpage, 'apiUser',
  84. default=self._API_USER, group='user')
  85. api_pass = self._search_regex(
  86. r'apiPass\s*:\s*(["\'])(?P<pass>.+?)\1', webpage, 'apiPass',
  87. default=self._API_PASS, group='pass')
  88. params = {
  89. 'initObj': {
  90. 'Locale': {
  91. 'LocaleLanguage': '',
  92. 'LocaleCountry': '',
  93. 'LocaleDevice': '',
  94. 'LocaleUserState': 0
  95. },
  96. 'Platform': 0,
  97. 'SiteGuid': 0,
  98. 'DomainID': '0',
  99. 'UDID': '',
  100. 'ApiUser': api_user,
  101. 'ApiPass': api_pass
  102. },
  103. 'MediaID': video_id,
  104. 'mediaType': 0,
  105. }
  106. req = sanitized_Request(
  107. 'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
  108. json.dumps(params).encode('utf-8'))
  109. info = self._download_json(req, video_id, 'Downloading video info json')
  110. title = info['MediaName']
  111. duration = int_or_none(info.get('Duration'))
  112. thumbnail = info.get('PicURL')
  113. description = info.get('Description')
  114. created_at = parse_iso8601(info.get('CreationDate') or None)
  115. formats = []
  116. for video_file in info.get('Files', []):
  117. ext = determine_ext(video_file['URL'])
  118. vid_format = video_file['Format'].replace(' ', '')
  119. # if geo-restricted, m3u8 is inaccessible, but mp4 is okay
  120. if ext == 'm3u8':
  121. m3u8_formats = self._extract_m3u8_formats(
  122. video_file['URL'], video_id, ext='mp4', m3u8_id=vid_format,
  123. note='Downloading %s m3u8 information' % vid_format,
  124. errnote='Failed to download %s m3u8 information' % vid_format,
  125. fatal=False)
  126. if m3u8_formats:
  127. formats.extend(m3u8_formats)
  128. elif ext in ('mp4', 'wvm'):
  129. # wvm are drm-protected files
  130. formats.append({
  131. 'ext': ext,
  132. 'url': video_file['URL'],
  133. 'format_id': vid_format,
  134. 'preference': self._FORMAT_PREFERENCES.get(ext + '-' + vid_format) or -1,
  135. 'format_note': 'DRM-protected video' if ext == 'wvm' else None
  136. })
  137. if not formats:
  138. # Most likely because geo-blocked
  139. raise ExtractorError('No downloadable videos found', expected=True)
  140. self._sort_formats(formats)
  141. return {
  142. 'id': video_id,
  143. 'title': title,
  144. 'description': description,
  145. 'duration': duration,
  146. 'timestamp': created_at,
  147. 'thumbnail': thumbnail,
  148. 'formats': formats,
  149. }