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.

277 lines
10 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\.|nieuws\.)?
  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. # http/s redirect
  86. 'url': 'https://vtmkzoom.be/video?aid=45724',
  87. 'info_dict': {
  88. 'id': '257136373657000',
  89. 'ext': 'mp4',
  90. 'title': 'K3 Dansstudio Ushuaia afl.6',
  91. },
  92. 'params': {
  93. 'skip_download': True,
  94. },
  95. 'skip': 'Requires account credentials',
  96. }, {
  97. # nieuws.vtm.be
  98. 'url': 'https://nieuws.vtm.be/stadion/stadion/genk-nog-moeilijk-programma',
  99. 'only_matching': True,
  100. }]
  101. def _real_initialize(self):
  102. self._logged_in = False
  103. def _login(self):
  104. username, password = self._get_login_info()
  105. if username is None:
  106. self.raise_login_required()
  107. auth_data = {
  108. 'APIKey': self._APIKEY,
  109. 'sdk': 'js_6.1',
  110. 'format': 'json',
  111. 'loginID': username,
  112. 'password': password,
  113. }
  114. auth_info = self._download_json(
  115. 'https://accounts.eu1.gigya.com/accounts.login', None,
  116. note='Logging in', errnote='Unable to log in',
  117. data=urlencode_postdata(auth_data))
  118. error_message = auth_info.get('errorDetails') or auth_info.get('errorMessage')
  119. if error_message:
  120. raise ExtractorError(
  121. 'Unable to login: %s' % error_message, expected=True)
  122. self._uid = auth_info['UID']
  123. self._uid_signature = auth_info['UIDSignature']
  124. self._signature_timestamp = auth_info['signatureTimestamp']
  125. self._logged_in = True
  126. def _real_extract(self, url):
  127. mobj = re.match(self._VALID_URL, url)
  128. video_id, site_id = mobj.group('id', 'site_id')
  129. webpage = self._download_webpage(url, video_id)
  130. config = self._parse_json(
  131. self._search_regex(
  132. r'videoJSConfig\s*=\s*JSON\.parse\(\'({.+?})\'\);',
  133. webpage, 'config', default='{}'), video_id,
  134. transform_source=lambda s: s.replace(
  135. '\\\\', '\\').replace(r'\"', '"').replace(r"\'", "'"))
  136. vod_id = config.get('vodId') or self._search_regex(
  137. (r'\\"vodId\\"\s*:\s*\\"(.+?)\\"',
  138. r'<[^>]+id=["\']vod-(\d+)'),
  139. webpage, 'video_id', default=None)
  140. # clip, no authentication required
  141. if not vod_id:
  142. player = self._parse_json(
  143. self._search_regex(
  144. r'vmmaplayer\(({.+?})\);', webpage, 'vmma player',
  145. default=''),
  146. video_id, transform_source=lambda s: '[%s]' % s, fatal=False)
  147. if player:
  148. video = player[-1]
  149. if video['videoUrl'] in ('http', 'https'):
  150. return self.url_result(video['url'], MedialaanIE.ie_key())
  151. info = {
  152. 'id': video_id,
  153. 'url': video['videoUrl'],
  154. 'title': video['title'],
  155. 'thumbnail': video.get('imageUrl'),
  156. 'timestamp': int_or_none(video.get('createdDate')),
  157. 'duration': int_or_none(video.get('duration')),
  158. }
  159. else:
  160. info = self._parse_html5_media_entries(
  161. url, webpage, video_id, m3u8_id='hls')[0]
  162. info.update({
  163. 'id': video_id,
  164. 'title': self._html_search_meta('description', webpage),
  165. 'duration': parse_duration(self._html_search_meta('duration', webpage)),
  166. })
  167. # vod, authentication required
  168. else:
  169. if not self._logged_in:
  170. self._login()
  171. settings = self._parse_json(
  172. self._search_regex(
  173. r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
  174. webpage, 'drupal settings', default='{}'),
  175. video_id)
  176. def get(container, item):
  177. return try_get(
  178. settings, lambda x: x[container][item],
  179. compat_str) or self._search_regex(
  180. r'"%s"\s*:\s*"([^"]+)' % item, webpage, item,
  181. default=None)
  182. app_id = get('vod', 'app_id') or self._SITE_TO_APP_ID.get(site_id, 'vtm_watch')
  183. sso = get('vod', 'gigyaDatabase') or 'vtm-sso'
  184. data = self._download_json(
  185. 'http://vod.medialaan.io/api/1.0/item/%s/video' % vod_id,
  186. video_id, query={
  187. 'app_id': app_id,
  188. 'user_network': sso,
  189. 'UID': self._uid,
  190. 'UIDSignature': self._uid_signature,
  191. 'signatureTimestamp': self._signature_timestamp,
  192. })
  193. formats = self._extract_m3u8_formats(
  194. data['response']['uri'], video_id, entry_protocol='m3u8_native',
  195. ext='mp4', m3u8_id='hls')
  196. self._sort_formats(formats)
  197. info = {
  198. 'id': vod_id,
  199. 'formats': formats,
  200. }
  201. api_key = get('vod', 'apiKey')
  202. channel = get('medialaanGigya', 'channel')
  203. if api_key:
  204. videos = self._download_json(
  205. 'http://vod.medialaan.io/vod/v2/videos', video_id, fatal=False,
  206. query={
  207. 'channels': channel,
  208. 'ids': vod_id,
  209. 'limit': 1,
  210. 'apikey': api_key,
  211. })
  212. if videos:
  213. video = try_get(
  214. videos, lambda x: x['response']['videos'][0], dict)
  215. if video:
  216. def get(container, item, expected_type=None):
  217. return try_get(
  218. video, lambda x: x[container][item], expected_type)
  219. def get_string(container, item):
  220. return get(container, item, compat_str)
  221. info.update({
  222. 'series': get_string('program', 'title'),
  223. 'season': get_string('season', 'title'),
  224. 'season_number': int_or_none(get('season', 'number')),
  225. 'season_id': get_string('season', 'id'),
  226. 'episode': get_string('episode', 'title'),
  227. 'episode_number': int_or_none(get('episode', 'number')),
  228. 'episode_id': get_string('episode', 'id'),
  229. 'duration': int_or_none(
  230. video.get('duration')) or int_or_none(
  231. video.get('durationMillis'), scale=1000),
  232. 'title': get_string('episode', 'title'),
  233. 'description': get_string('episode', 'text'),
  234. 'timestamp': unified_timestamp(get_string(
  235. 'publication', 'begin')),
  236. })
  237. if not info.get('title'):
  238. info['title'] = try_get(
  239. config, lambda x: x['videoConfig']['title'],
  240. compat_str) or self._html_search_regex(
  241. r'\\"title\\"\s*:\s*\\"(.+?)\\"', webpage, 'title',
  242. default=None) or self._og_search_title(webpage)
  243. if not info.get('description'):
  244. info['description'] = self._html_search_regex(
  245. r'<div[^>]+class="field-item\s+even">\s*<p>(.+?)</p>',
  246. webpage, 'description', default=None)
  247. return info