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.

269 lines
9.8 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'"vodId"\s*:\s*"(.+?)"',
  130. r'<[^>]+id=["\']vod-(\d+)'),
  131. webpage, 'video_id', default=None)
  132. # clip, no authentication required
  133. if not vod_id:
  134. player = self._parse_json(
  135. self._search_regex(
  136. r'vmmaplayer\(({.+?})\);', webpage, 'vmma player',
  137. default=''),
  138. video_id, transform_source=lambda s: '[%s]' % s, fatal=False)
  139. if player:
  140. video = player[-1]
  141. if video['videoUrl'] in ('http', 'https'):
  142. return self.url_result(video['url'], MedialaanIE.ie_key())
  143. info = {
  144. 'id': video_id,
  145. 'url': video['videoUrl'],
  146. 'title': video['title'],
  147. 'thumbnail': video.get('imageUrl'),
  148. 'timestamp': int_or_none(video.get('createdDate')),
  149. 'duration': int_or_none(video.get('duration')),
  150. }
  151. else:
  152. info = self._parse_html5_media_entries(
  153. url, webpage, video_id, m3u8_id='hls')[0]
  154. info.update({
  155. 'id': video_id,
  156. 'title': self._html_search_meta('description', webpage),
  157. 'duration': parse_duration(self._html_search_meta('duration', webpage)),
  158. })
  159. # vod, authentication required
  160. else:
  161. if not self._logged_in:
  162. self._login()
  163. settings = self._parse_json(
  164. self._search_regex(
  165. r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
  166. webpage, 'drupal settings', default='{}'),
  167. video_id)
  168. def get(container, item):
  169. return try_get(
  170. settings, lambda x: x[container][item],
  171. compat_str) or self._search_regex(
  172. r'"%s"\s*:\s*"([^"]+)' % item, webpage, item,
  173. default=None)
  174. app_id = get('vod', 'app_id') or self._SITE_TO_APP_ID.get(site_id, 'vtm_watch')
  175. sso = get('vod', 'gigyaDatabase') or 'vtm-sso'
  176. data = self._download_json(
  177. 'http://vod.medialaan.io/api/1.0/item/%s/video' % vod_id,
  178. video_id, query={
  179. 'app_id': app_id,
  180. 'user_network': sso,
  181. 'UID': self._uid,
  182. 'UIDSignature': self._uid_signature,
  183. 'signatureTimestamp': self._signature_timestamp,
  184. })
  185. formats = self._extract_m3u8_formats(
  186. data['response']['uri'], video_id, entry_protocol='m3u8_native',
  187. ext='mp4', m3u8_id='hls')
  188. self._sort_formats(formats)
  189. info = {
  190. 'id': vod_id,
  191. 'formats': formats,
  192. }
  193. api_key = get('vod', 'apiKey')
  194. channel = get('medialaanGigya', 'channel')
  195. if api_key:
  196. videos = self._download_json(
  197. 'http://vod.medialaan.io/vod/v2/videos', video_id, fatal=False,
  198. query={
  199. 'channels': channel,
  200. 'ids': vod_id,
  201. 'limit': 1,
  202. 'apikey': api_key,
  203. })
  204. if videos:
  205. video = try_get(
  206. videos, lambda x: x['response']['videos'][0], dict)
  207. if video:
  208. def get(container, item, expected_type=None):
  209. return try_get(
  210. video, lambda x: x[container][item], expected_type)
  211. def get_string(container, item):
  212. return get(container, item, compat_str)
  213. info.update({
  214. 'series': get_string('program', 'title'),
  215. 'season': get_string('season', 'title'),
  216. 'season_number': int_or_none(get('season', 'number')),
  217. 'season_id': get_string('season', 'id'),
  218. 'episode': get_string('episode', 'title'),
  219. 'episode_number': int_or_none(get('episode', 'number')),
  220. 'episode_id': get_string('episode', 'id'),
  221. 'duration': int_or_none(
  222. video.get('duration')) or int_or_none(
  223. video.get('durationMillis'), scale=1000),
  224. 'title': get_string('episode', 'title'),
  225. 'description': get_string('episode', 'text'),
  226. 'timestamp': unified_timestamp(get_string(
  227. 'publication', 'begin')),
  228. })
  229. if not info.get('title'):
  230. info['title'] = try_get(
  231. config, lambda x: x['videoConfig']['title'],
  232. compat_str) or self._html_search_regex(
  233. r'\\"title\\"\s*:\s*\\"(.+?)\\"', webpage, 'title',
  234. default=None) or self._og_search_title(webpage)
  235. if not info.get('description'):
  236. info['description'] = self._html_search_regex(
  237. r'<div[^>]+class="field-item\s+even">\s*<p>(.+?)</p>',
  238. webpage, 'description', default=None)
  239. return info