togglesg.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import itertools
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. determine_ext,
  11. parse_iso8601,
  12. remove_end
  13. )
  14. from ..compat import compat_urllib_request
  15. class ToggleSgIE(InfoExtractor):
  16. IE_NAME = 'togglesg'
  17. _VALID_URL = r'https?://video\.toggle\.sg/(?:en|zh)/(?:series|clips|movies)/.+?/(?P<id>[0-9]+)'
  18. _TESTS = [{
  19. 'url': 'http://video.toggle.sg/en/series/lion-moms-tif/trailers/lion-moms-premier/343115',
  20. 'info_dict': {
  21. 'id': '343115',
  22. 'ext': 'mp4',
  23. 'title': 'Lion Moms Premiere',
  24. 'description': 'md5:aea1149404bff4d7f7b6da11fafd8e6b',
  25. 'upload_date': '20150910',
  26. 'timestamp': 1441858274,
  27. },
  28. 'params': {
  29. 'skip_download': 'm3u8 download',
  30. }
  31. }, {
  32. 'note': 'DRM-protected video',
  33. 'url': 'http://video.toggle.sg/en/movies/dug-s-special-mission/341413',
  34. 'info_dict': {
  35. 'id': '341413',
  36. 'ext': 'wvm',
  37. 'title': 'Dug\'s Special Mission',
  38. 'description': 'md5:e86c6f4458214905c1772398fabc93e0',
  39. 'upload_date': '20150827',
  40. 'timestamp': 1440644006,
  41. },
  42. 'params': {
  43. 'skip_download': 'DRM-protected wvm download',
  44. }
  45. }, {
  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/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(url, video_id, note='Downloading video page')
  84. api_user = self._search_regex(
  85. r'apiUser:\s*"([^"]+)"', webpage, 'apiUser', default=self._API_USER)
  86. api_pass = self._search_regex(
  87. r'apiPass:\s*"([^"]+)"', webpage, 'apiPass', default=self._API_PASS)
  88. params = {
  89. 'initObj': {
  90. 'Locale': {
  91. 'LocaleLanguage': '', 'LocaleCountry': '',
  92. 'LocaleDevice': '', 'LocaleUserState': 0
  93. },
  94. 'Platform': 0, 'SiteGuid': 0, 'DomainID': '0', 'UDID': '',
  95. 'ApiUser': api_user, 'ApiPass': api_pass
  96. },
  97. 'MediaID': video_id,
  98. 'mediaType': 0,
  99. }
  100. req = compat_urllib_request.Request(
  101. 'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
  102. json.dumps(params).encode('utf-8'))
  103. info = self._download_json(req, video_id, 'Downloading video info json')
  104. title = info['MediaName']
  105. duration = int_or_none(info.get('Duration'))
  106. thumbnail = info.get('PicURL')
  107. description = info.get('Description')
  108. created_at = parse_iso8601(info.get('CreationDate') or None)
  109. formats = []
  110. for video_file in info.get('Files', []):
  111. ext = determine_ext(video_file['URL'])
  112. vid_format = video_file['Format'].replace(' ', '')
  113. # if geo-restricted, m3u8 is inaccessible, but mp4 is okay
  114. if ext == 'm3u8':
  115. m3u8_formats = self._extract_m3u8_formats(
  116. video_file['URL'], video_id, ext='mp4', m3u8_id=vid_format,
  117. note='Downloading %s m3u8 information' % vid_format,
  118. errnote='Failed to download %s m3u8 information' % vid_format,
  119. fatal=False
  120. )
  121. if m3u8_formats:
  122. formats.extend(m3u8_formats)
  123. if ext in ['mp4', 'wvm']:
  124. # wvm are drm-protected files
  125. formats.append({
  126. 'ext': ext,
  127. 'url': video_file['URL'],
  128. 'format_id': vid_format,
  129. 'preference': self._FORMAT_PREFERENCES.get(ext + '-' + vid_format) or -1,
  130. 'format_note': 'DRM-protected video' if ext == 'wvm' else None
  131. })
  132. if not formats:
  133. # Most likely because geo-blocked
  134. raise ExtractorError('No downloadable videos found', expected=True)
  135. self._sort_formats(formats)
  136. return {
  137. 'id': video_id,
  138. 'title': title,
  139. 'description': description,
  140. 'duration': duration,
  141. 'timestamp': created_at,
  142. 'thumbnail': thumbnail,
  143. 'formats': formats,
  144. }