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.

259 lines
9.5 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. parse_duration,
  9. try_get,
  10. unified_timestamp,
  11. urlencode_postdata,
  12. )
  13. class MedialaanIE(InfoExtractor):
  14. _VALID_URL = r'''(?x)
  15. https?://
  16. (?:www\.)?
  17. (?:
  18. (?P<site_id>vtm|q2|vtmkzoom)\.be/
  19. (?:
  20. video(?:/[^/]+/id/|/?\?.*?\baid=)|
  21. (?:[^/]+/)*
  22. )
  23. )
  24. (?P<id>[^/?#&]+)
  25. '''
  26. _NETRC_MACHINE = 'medialaan'
  27. _APIKEY = '3_HZ0FtkMW_gOyKlqQzW5_0FHRC7Nd5XpXJZcDdXY4pk5eES2ZWmejRW5egwVm4ug-'
  28. _SITE_TO_APP_ID = {
  29. 'vtm': 'vtm_watch',
  30. 'q2': 'q2',
  31. 'vtmkzoom': 'vtmkzoom',
  32. }
  33. _TESTS = [{
  34. # vod
  35. 'url': 'http://vtm.be/video/volledige-afleveringen/id/vtm_20170219_VM0678361_vtmwatch',
  36. 'info_dict': {
  37. 'id': 'vtm_20170219_VM0678361_vtmwatch',
  38. 'ext': 'mp4',
  39. 'title': 'Allemaal Chris afl. 6',
  40. 'description': 'md5:4be86427521e7b07e0adb0c9c554ddb2',
  41. 'timestamp': 1487533280,
  42. 'upload_date': '20170219',
  43. 'duration': 2562,
  44. 'series': 'Allemaal Chris',
  45. 'season': 'Allemaal Chris',
  46. 'season_number': 1,
  47. 'season_id': '256936078124527',
  48. 'episode': 'Allemaal Chris afl. 6',
  49. 'episode_number': 6,
  50. 'episode_id': '256936078591527',
  51. },
  52. 'params': {
  53. 'skip_download': True,
  54. },
  55. 'skip': 'Requires account credentials',
  56. }, {
  57. # clip
  58. 'url': 'http://vtm.be/video?aid=168332',
  59. 'info_dict': {
  60. 'id': '168332',
  61. 'ext': 'mp4',
  62. 'title': '"Veronique liegt!"',
  63. 'description': 'md5:1385e2b743923afe54ba4adc38476155',
  64. 'timestamp': 1489002029,
  65. 'upload_date': '20170308',
  66. 'duration': 96,
  67. },
  68. }, {
  69. # vod
  70. 'url': 'http://vtm.be/video/volledige-afleveringen/id/257107153551000',
  71. 'only_matching': True,
  72. }, {
  73. # vod
  74. 'url': 'http://vtm.be/video?aid=163157',
  75. 'only_matching': True,
  76. }, {
  77. # vod
  78. 'url': 'http://www.q2.be/video/volledige-afleveringen/id/2be_20170301_VM0684442_q2',
  79. 'only_matching': True,
  80. }, {
  81. # clip
  82. 'url': 'http://vtmkzoom.be/k3-dansstudio/een-nieuw-seizoen-van-k3-dansstudio',
  83. 'only_matching': True,
  84. }]
  85. def _real_initialize(self):
  86. self._logged_in = False
  87. def _login(self):
  88. username, password = self._get_login_info()
  89. if username is None:
  90. self.raise_login_required()
  91. auth_data = {
  92. 'APIKey': self._APIKEY,
  93. 'sdk': 'js_6.1',
  94. 'format': 'json',
  95. 'loginID': username,
  96. 'password': password,
  97. }
  98. auth_info = self._download_json(
  99. 'https://accounts.eu1.gigya.com/accounts.login', None,
  100. note='Logging in', errnote='Unable to log in',
  101. data=urlencode_postdata(auth_data))
  102. error_message = auth_info.get('errorDetails') or auth_info.get('errorMessage')
  103. if error_message:
  104. raise ExtractorError(
  105. 'Unable to login: %s' % error_message, expected=True)
  106. self._uid = auth_info['UID']
  107. self._uid_signature = auth_info['UIDSignature']
  108. self._signature_timestamp = auth_info['signatureTimestamp']
  109. self._logged_in = True
  110. def _real_extract(self, url):
  111. mobj = re.match(self._VALID_URL, url)
  112. video_id, site_id = mobj.group('id', 'site_id')
  113. webpage = self._download_webpage(url, video_id)
  114. config = self._parse_json(
  115. self._search_regex(
  116. r'videoJSConfig\s*=\s*JSON\.parse\(\'({.+?})\'\);',
  117. webpage, 'config', default='{}'), video_id,
  118. transform_source=lambda s: s.replace(
  119. '\\\\', '\\').replace(r'\"', '"').replace(r"\'", "'"))
  120. vod_id = config.get('vodId') or self._search_regex(
  121. (r'\\"vodId\\"\s*:\s*\\"(.+?)\\"',
  122. r'<[^>]+id=["\']vod-(\d+)'),
  123. webpage, 'video_id', default=None)
  124. # clip, no authentication required
  125. if not vod_id:
  126. player = self._parse_json(
  127. self._search_regex(
  128. r'vmmaplayer\(({.+?})\);', webpage, 'vmma player',
  129. default=''),
  130. video_id, transform_source=lambda s: '[%s]' % s, fatal=False)
  131. if player:
  132. video = player[-1]
  133. info = {
  134. 'id': video_id,
  135. 'url': video['videoUrl'],
  136. 'title': video['title'],
  137. 'thumbnail': video.get('imageUrl'),
  138. 'timestamp': int_or_none(video.get('createdDate')),
  139. 'duration': int_or_none(video.get('duration')),
  140. }
  141. else:
  142. info = self._parse_html5_media_entries(
  143. url, webpage, video_id, m3u8_id='hls')[0]
  144. info.update({
  145. 'id': video_id,
  146. 'title': self._html_search_meta('description', webpage),
  147. 'duration': parse_duration(self._html_search_meta('duration', webpage)),
  148. })
  149. # vod, authentication required
  150. else:
  151. if not self._logged_in:
  152. self._login()
  153. settings = self._parse_json(
  154. self._search_regex(
  155. r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
  156. webpage, 'drupal settings', default='{}'),
  157. video_id)
  158. def get(container, item):
  159. return try_get(
  160. settings, lambda x: x[container][item],
  161. compat_str) or self._search_regex(
  162. r'"%s"\s*:\s*"([^"]+)' % item, webpage, item,
  163. default=None)
  164. app_id = get('vod', 'app_id') or self._SITE_TO_APP_ID.get(site_id, 'vtm_watch')
  165. sso = get('vod', 'gigyaDatabase') or 'vtm-sso'
  166. data = self._download_json(
  167. 'http://vod.medialaan.io/api/1.0/item/%s/video' % vod_id,
  168. video_id, query={
  169. 'app_id': app_id,
  170. 'user_network': sso,
  171. 'UID': self._uid,
  172. 'UIDSignature': self._uid_signature,
  173. 'signatureTimestamp': self._signature_timestamp,
  174. })
  175. formats = self._extract_m3u8_formats(
  176. data['response']['uri'], video_id, entry_protocol='m3u8_native',
  177. ext='mp4', m3u8_id='hls')
  178. self._sort_formats(formats)
  179. info = {
  180. 'id': vod_id,
  181. 'formats': formats,
  182. }
  183. api_key = get('vod', 'apiKey')
  184. channel = get('medialaanGigya', 'channel')
  185. if api_key:
  186. videos = self._download_json(
  187. 'http://vod.medialaan.io/vod/v2/videos', video_id, fatal=False,
  188. query={
  189. 'channels': channel,
  190. 'ids': vod_id,
  191. 'limit': 1,
  192. 'apikey': api_key,
  193. })
  194. if videos:
  195. video = try_get(
  196. videos, lambda x: x['response']['videos'][0], dict)
  197. if video:
  198. def get(container, item, expected_type=None):
  199. return try_get(
  200. video, lambda x: x[container][item], expected_type)
  201. def get_string(container, item):
  202. return get(container, item, compat_str)
  203. info.update({
  204. 'series': get_string('program', 'title'),
  205. 'season': get_string('season', 'title'),
  206. 'season_number': int_or_none(get('season', 'number')),
  207. 'season_id': get_string('season', 'id'),
  208. 'episode': get_string('episode', 'title'),
  209. 'episode_number': int_or_none(get('episode', 'number')),
  210. 'episode_id': get_string('episode', 'id'),
  211. 'duration': int_or_none(
  212. video.get('duration')) or int_or_none(
  213. video.get('durationMillis'), scale=1000),
  214. 'title': get_string('episode', 'title'),
  215. 'description': get_string('episode', 'text'),
  216. 'timestamp': unified_timestamp(get_string(
  217. 'publication', 'begin')),
  218. })
  219. if not info.get('title'):
  220. info['title'] = try_get(
  221. config, lambda x: x['videoConfig']['title'],
  222. compat_str) or self._html_search_regex(
  223. r'\\"title\\"\s*:\s*\\"(.+?)\\"', webpage, 'title',
  224. default=None) or self._og_search_title(webpage)
  225. if not info.get('description'):
  226. info['description'] = self._html_search_regex(
  227. r'<div[^>]+class="field-item\s+even">\s*<p>(.+?)</p>',
  228. webpage, 'description', default=None)
  229. return info