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.

213 lines
7.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?://(?:(?:www\.)?mewatch|video\.toggle)\.sg/(?:en|zh)/(?:[^/]+/){2,}(?P<id>[0-9]+)'
  17. _TESTS = [{
  18. 'url': 'http://www.mewatch.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://www.mewatch.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://www.mewatch.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://www.mewatch.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
  65. 'only_matching': True,
  66. }, {
  67. 'url': 'http://www.mewatch.sg/zh/series/zero-calling-s2-hd/ep13/336367',
  68. 'only_matching': True,
  69. }, {
  70. 'url': 'http://www.mewatch.sg/en/series/vetri-s2/webisodes/jeeva-is-an-orphan-vetri-s2-webisode-7/342302',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'http://www.mewatch.sg/en/movies/seven-days/321936',
  74. 'only_matching': True,
  75. }, {
  76. 'url': 'https://www.mewatch.sg/en/tv-show/news/may-2017-cna-singapore-tonight/fri-19-may-2017/512456',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'http://www.mewatch.sg/en/channels/eleven-plus/401585',
  80. 'only_matching': True,
  81. }]
  82. _FORMAT_PREFERENCES = {
  83. 'wvm-STBMain': -10,
  84. 'wvm-iPadMain': -20,
  85. 'wvm-iPhoneMain': -30,
  86. 'wvm-Android': -40,
  87. }
  88. _API_USER = 'tvpapi_147'
  89. _API_PASS = '11111'
  90. def _real_extract(self, url):
  91. video_id = self._match_id(url)
  92. webpage = self._download_webpage(
  93. url, video_id, note='Downloading video page')
  94. api_user = self._search_regex(
  95. r'apiUser\s*:\s*(["\'])(?P<user>.+?)\1', webpage, 'apiUser',
  96. default=self._API_USER, group='user')
  97. api_pass = self._search_regex(
  98. r'apiPass\s*:\s*(["\'])(?P<pass>.+?)\1', webpage, 'apiPass',
  99. default=self._API_PASS, group='pass')
  100. params = {
  101. 'initObj': {
  102. 'Locale': {
  103. 'LocaleLanguage': '',
  104. 'LocaleCountry': '',
  105. 'LocaleDevice': '',
  106. 'LocaleUserState': 0
  107. },
  108. 'Platform': 0,
  109. 'SiteGuid': 0,
  110. 'DomainID': '0',
  111. 'UDID': '',
  112. 'ApiUser': api_user,
  113. 'ApiPass': api_pass
  114. },
  115. 'MediaID': video_id,
  116. 'mediaType': 0,
  117. }
  118. req = sanitized_Request(
  119. 'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
  120. json.dumps(params).encode('utf-8'))
  121. info = self._download_json(req, video_id, 'Downloading video info json')
  122. title = info['MediaName']
  123. formats = []
  124. for video_file in info.get('Files', []):
  125. video_url, vid_format = video_file.get('URL'), video_file.get('Format')
  126. if not video_url or video_url == 'NA' or not vid_format:
  127. continue
  128. ext = determine_ext(video_url)
  129. vid_format = vid_format.replace(' ', '')
  130. # if geo-restricted, m3u8 is inaccessible, but mp4 is okay
  131. if ext == 'm3u8':
  132. formats.extend(self._extract_m3u8_formats(
  133. video_url, video_id, ext='mp4', m3u8_id=vid_format,
  134. note='Downloading %s m3u8 information' % vid_format,
  135. errnote='Failed to download %s m3u8 information' % vid_format,
  136. fatal=False))
  137. elif ext == 'mpd':
  138. formats.extend(self._extract_mpd_formats(
  139. video_url, video_id, mpd_id=vid_format,
  140. note='Downloading %s MPD manifest' % vid_format,
  141. errnote='Failed to download %s MPD manifest' % vid_format,
  142. fatal=False))
  143. elif ext == 'ism':
  144. formats.extend(self._extract_ism_formats(
  145. video_url, video_id, ism_id=vid_format,
  146. note='Downloading %s ISM manifest' % vid_format,
  147. errnote='Failed to download %s ISM manifest' % vid_format,
  148. fatal=False))
  149. elif ext in ('mp4', 'wvm'):
  150. # wvm are drm-protected files
  151. formats.append({
  152. 'ext': ext,
  153. 'url': video_url,
  154. 'format_id': vid_format,
  155. 'preference': self._FORMAT_PREFERENCES.get(ext + '-' + vid_format) or -1,
  156. 'format_note': 'DRM-protected video' if ext == 'wvm' else None
  157. })
  158. if not formats:
  159. # Most likely because geo-blocked
  160. raise ExtractorError('No downloadable videos found', expected=True)
  161. self._sort_formats(formats)
  162. duration = int_or_none(info.get('Duration'))
  163. description = info.get('Description')
  164. created_at = parse_iso8601(info.get('CreationDate') or None)
  165. average_rating = float_or_none(info.get('Rating'))
  166. view_count = int_or_none(info.get('ViewCounter') or info.get('view_counter'))
  167. like_count = int_or_none(info.get('LikeCounter') or info.get('like_counter'))
  168. thumbnails = []
  169. for picture in info.get('Pictures', []):
  170. if not isinstance(picture, dict):
  171. continue
  172. pic_url = picture.get('URL')
  173. if not pic_url:
  174. continue
  175. thumbnail = {
  176. 'url': pic_url,
  177. }
  178. pic_size = picture.get('PicSize', '')
  179. m = re.search(r'(?P<width>\d+)[xX](?P<height>\d+)', pic_size)
  180. if m:
  181. thumbnail.update({
  182. 'width': int(m.group('width')),
  183. 'height': int(m.group('height')),
  184. })
  185. thumbnails.append(thumbnail)
  186. return {
  187. 'id': video_id,
  188. 'title': title,
  189. 'description': description,
  190. 'duration': duration,
  191. 'timestamp': created_at,
  192. 'average_rating': average_rating,
  193. 'view_count': view_count,
  194. 'like_count': like_count,
  195. 'thumbnails': thumbnails,
  196. 'formats': formats,
  197. }