togglesg.py 6.3 KB

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