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.

268 lines
9.7 KiB

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