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.

194 lines
6.9 KiB

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