togglesg.py 6.7 KB

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