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.

192 lines
6.8 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. formats.extend(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. elif ext in ('mp4', 'wvm'):
  129. # wvm are drm-protected files
  130. formats.append({
  131. 'ext': ext,
  132. 'url': video_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. duration = int_or_none(info.get('Duration'))
  142. description = info.get('Description')
  143. created_at = parse_iso8601(info.get('CreationDate') or None)
  144. average_rating = float_or_none(info.get('Rating'))
  145. view_count = int_or_none(info.get('ViewCounter') or info.get('view_counter'))
  146. like_count = int_or_none(info.get('LikeCounter') or info.get('like_counter'))
  147. thumbnails = []
  148. for picture in info.get('Pictures', []):
  149. if not isinstance(picture, dict):
  150. continue
  151. pic_url = picture.get('URL')
  152. if not pic_url:
  153. continue
  154. thumbnail = {
  155. 'url': pic_url,
  156. }
  157. pic_size = picture.get('PicSize', '')
  158. m = re.search(r'(?P<width>\d+)[xX](?P<height>\d+)', pic_size)
  159. if m:
  160. thumbnail.update({
  161. 'width': int(m.group('width')),
  162. 'height': int(m.group('height')),
  163. })
  164. thumbnails.append(thumbnail)
  165. return {
  166. 'id': video_id,
  167. 'title': title,
  168. 'description': description,
  169. 'duration': duration,
  170. 'timestamp': created_at,
  171. 'average_rating': average_rating,
  172. 'view_count': view_count,
  173. 'like_count': like_count,
  174. 'thumbnails': thumbnails,
  175. 'formats': formats,
  176. }