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.

439 lines
16 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_str,
  8. compat_urlparse,
  9. )
  10. from ..utils import (
  11. determine_ext,
  12. ExtractorError,
  13. int_or_none,
  14. parse_iso8601,
  15. qualities,
  16. smuggle_url,
  17. try_get,
  18. unsmuggle_url,
  19. update_url_query,
  20. )
  21. class TVPlayIE(InfoExtractor):
  22. IE_NAME = 'mtg'
  23. IE_DESC = 'MTG services'
  24. _VALID_URL = r'''(?x)
  25. (?:
  26. mtg:|
  27. https?://
  28. (?:www\.)?
  29. (?:
  30. tvplay(?:\.skaties)?\.lv/parraides|
  31. (?:tv3play|play\.tv3)\.lt/programos|
  32. tv3play(?:\.tv3)?\.ee/sisu|
  33. (?:tv(?:3|6|8|10)play|viafree)\.se/program|
  34. (?:(?:tv3play|viasat4play|tv6play|viafree)\.no|(?:tv3play|viafree)\.dk)/programmer|
  35. play\.novatv\.bg/programi
  36. )
  37. /(?:[^/]+/)+
  38. )
  39. (?P<id>\d+)
  40. '''
  41. _TESTS = [
  42. {
  43. 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
  44. 'md5': 'a1612fe0849455423ad8718fe049be21',
  45. 'info_dict': {
  46. 'id': '418113',
  47. 'ext': 'mp4',
  48. 'title': 'Kādi ir īri? - Viņas melo labāk',
  49. 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
  50. 'series': 'Viņas melo labāk',
  51. 'season': '2.sezona',
  52. 'season_number': 2,
  53. 'duration': 25,
  54. 'timestamp': 1406097056,
  55. 'upload_date': '20140723',
  56. },
  57. },
  58. {
  59. 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
  60. 'info_dict': {
  61. 'id': '409229',
  62. 'ext': 'flv',
  63. 'title': 'Moterys meluoja geriau',
  64. 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
  65. 'series': 'Moterys meluoja geriau',
  66. 'episode_number': 47,
  67. 'season': '1 sezonas',
  68. 'season_number': 1,
  69. 'duration': 1330,
  70. 'timestamp': 1403769181,
  71. 'upload_date': '20140626',
  72. },
  73. 'params': {
  74. # rtmp download
  75. 'skip_download': True,
  76. },
  77. },
  78. {
  79. 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
  80. 'info_dict': {
  81. 'id': '238551',
  82. 'ext': 'flv',
  83. 'title': 'Kodu keset linna 398537',
  84. 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
  85. 'duration': 1257,
  86. 'timestamp': 1292449761,
  87. 'upload_date': '20101215',
  88. },
  89. 'params': {
  90. # rtmp download
  91. 'skip_download': True,
  92. },
  93. },
  94. {
  95. 'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
  96. 'info_dict': {
  97. 'id': '395385',
  98. 'ext': 'mp4',
  99. 'title': 'Husräddarna S02E07',
  100. 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
  101. 'duration': 2574,
  102. 'timestamp': 1400596321,
  103. 'upload_date': '20140520',
  104. },
  105. 'params': {
  106. 'skip_download': True,
  107. },
  108. },
  109. {
  110. 'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
  111. 'info_dict': {
  112. 'id': '266636',
  113. 'ext': 'mp4',
  114. 'title': 'Den sista dokusåpan S01E08',
  115. 'description': 'md5:295be39c872520221b933830f660b110',
  116. 'duration': 1492,
  117. 'timestamp': 1330522854,
  118. 'upload_date': '20120229',
  119. 'age_limit': 18,
  120. },
  121. 'params': {
  122. 'skip_download': True,
  123. },
  124. },
  125. {
  126. 'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
  127. 'info_dict': {
  128. 'id': '282756',
  129. 'ext': 'mp4',
  130. 'title': 'Antikjakten S01E10',
  131. 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
  132. 'duration': 2646,
  133. 'timestamp': 1348575868,
  134. 'upload_date': '20120925',
  135. },
  136. 'params': {
  137. 'skip_download': True,
  138. },
  139. },
  140. {
  141. 'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
  142. 'info_dict': {
  143. 'id': '230898',
  144. 'ext': 'mp4',
  145. 'title': 'Anna Anka søker assistent - Ep. 8',
  146. 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
  147. 'duration': 2656,
  148. 'timestamp': 1277720005,
  149. 'upload_date': '20100628',
  150. },
  151. 'params': {
  152. 'skip_download': True,
  153. },
  154. },
  155. {
  156. 'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
  157. 'info_dict': {
  158. 'id': '21873',
  159. 'ext': 'mp4',
  160. 'title': 'Budbringerne program 10',
  161. 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
  162. 'duration': 1297,
  163. 'timestamp': 1254205102,
  164. 'upload_date': '20090929',
  165. },
  166. 'params': {
  167. 'skip_download': True,
  168. },
  169. },
  170. {
  171. 'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
  172. 'info_dict': {
  173. 'id': '361883',
  174. 'ext': 'mp4',
  175. 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
  176. 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
  177. 'duration': 2594,
  178. 'timestamp': 1393236292,
  179. 'upload_date': '20140224',
  180. },
  181. 'params': {
  182. 'skip_download': True,
  183. },
  184. },
  185. {
  186. 'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
  187. 'info_dict': {
  188. 'id': '624952',
  189. 'ext': 'flv',
  190. 'title': 'Здравей, България (12.06.2015 г.) ',
  191. 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
  192. 'duration': 8838,
  193. 'timestamp': 1434100372,
  194. 'upload_date': '20150612',
  195. },
  196. 'params': {
  197. # rtmp download
  198. 'skip_download': True,
  199. },
  200. },
  201. {
  202. 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
  203. 'only_matching': True,
  204. },
  205. {
  206. # views is null
  207. 'url': 'http://tvplay.skaties.lv/parraides/tv3-zinas/760183',
  208. 'only_matching': True,
  209. },
  210. {
  211. 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
  212. 'only_matching': True,
  213. },
  214. {
  215. 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
  216. 'only_matching': True,
  217. },
  218. {
  219. 'url': 'mtg:418113',
  220. 'only_matching': True,
  221. }
  222. ]
  223. def _real_extract(self, url):
  224. url, smuggled_data = unsmuggle_url(url, {})
  225. self._initialize_geo_bypass(smuggled_data.get('geo_countries'))
  226. video_id = self._match_id(url)
  227. geo_country = self._search_regex(
  228. r'https?://[^/]+\.([a-z]{2})', url,
  229. 'geo country', default=None)
  230. if geo_country:
  231. self._initialize_geo_bypass([geo_country.upper()])
  232. video = self._download_json(
  233. 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
  234. title = video['title']
  235. try:
  236. streams = self._download_json(
  237. 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
  238. video_id, 'Downloading streams JSON')
  239. except ExtractorError as e:
  240. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  241. msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
  242. raise ExtractorError(msg['msg'], expected=True)
  243. raise
  244. quality = qualities(['hls', 'medium', 'high'])
  245. formats = []
  246. for format_id, video_url in streams.get('streams', {}).items():
  247. if not video_url or not isinstance(video_url, compat_str):
  248. continue
  249. ext = determine_ext(video_url)
  250. if ext == 'f4m':
  251. formats.extend(self._extract_f4m_formats(
  252. update_url_query(video_url, {
  253. 'hdcore': '3.5.0',
  254. 'plugin': 'aasp-3.5.0.151.81'
  255. }), video_id, f4m_id='hds', fatal=False))
  256. elif ext == 'm3u8':
  257. formats.extend(self._extract_m3u8_formats(
  258. video_url, video_id, 'mp4', 'm3u8_native',
  259. m3u8_id='hls', fatal=False))
  260. else:
  261. fmt = {
  262. 'format_id': format_id,
  263. 'quality': quality(format_id),
  264. 'ext': ext,
  265. }
  266. if video_url.startswith('rtmp'):
  267. m = re.search(
  268. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  269. if not m:
  270. continue
  271. fmt.update({
  272. 'ext': 'flv',
  273. 'url': m.group('url'),
  274. 'app': m.group('app'),
  275. 'play_path': m.group('playpath'),
  276. })
  277. else:
  278. fmt.update({
  279. 'url': video_url,
  280. })
  281. formats.append(fmt)
  282. if not formats and video.get('is_geo_blocked'):
  283. self.raise_geo_restricted(
  284. 'This content might not be available in your country due to copyright reasons')
  285. self._sort_formats(formats)
  286. # TODO: webvtt in m3u8
  287. subtitles = {}
  288. sami_path = video.get('sami_path')
  289. if sami_path:
  290. lang = self._search_regex(
  291. r'_([a-z]{2})\.xml', sami_path, 'lang',
  292. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  293. subtitles[lang] = [{
  294. 'url': sami_path,
  295. }]
  296. series = video.get('format_title')
  297. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  298. season = video.get('_embedded', {}).get('season', {}).get('title')
  299. season_number = int_or_none(video.get('format_position', {}).get('season'))
  300. return {
  301. 'id': video_id,
  302. 'title': title,
  303. 'description': video.get('description'),
  304. 'series': series,
  305. 'episode_number': episode_number,
  306. 'season': season,
  307. 'season_number': season_number,
  308. 'duration': int_or_none(video.get('duration')),
  309. 'timestamp': parse_iso8601(video.get('created_at')),
  310. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  311. 'age_limit': int_or_none(video.get('age_limit', 0)),
  312. 'formats': formats,
  313. 'subtitles': subtitles,
  314. }
  315. class ViafreeIE(InfoExtractor):
  316. _VALID_URL = r'''(?x)
  317. https?://
  318. (?:www\.)?
  319. viafree\.
  320. (?:
  321. (?:dk|no)/programmer|
  322. se/program
  323. )
  324. /(?:[^/]+/)+(?P<id>[^/?#&]+)
  325. '''
  326. _TESTS = [{
  327. 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
  328. 'info_dict': {
  329. 'id': '395375',
  330. 'ext': 'mp4',
  331. 'title': 'Husräddarna S02E02',
  332. 'description': 'md5:4db5c933e37db629b5a2f75dfb34829e',
  333. 'series': 'Husräddarna',
  334. 'season': 'Säsong 2',
  335. 'season_number': 2,
  336. 'duration': 2576,
  337. 'timestamp': 1400596321,
  338. 'upload_date': '20140520',
  339. },
  340. 'params': {
  341. 'skip_download': True,
  342. },
  343. 'add_ie': [TVPlayIE.ie_key()],
  344. }, {
  345. # with relatedClips
  346. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
  347. 'info_dict': {
  348. 'id': '758770',
  349. 'ext': 'mp4',
  350. 'title': 'Sommaren med YouTube-stjärnorna S01E01',
  351. 'description': 'md5:2bc69dce2c4bb48391e858539bbb0e3f',
  352. 'series': 'Sommaren med YouTube-stjärnorna',
  353. 'season': 'Säsong 1',
  354. 'season_number': 1,
  355. 'duration': 1326,
  356. 'timestamp': 1470905572,
  357. 'upload_date': '20160811',
  358. },
  359. 'params': {
  360. 'skip_download': True,
  361. },
  362. 'add_ie': [TVPlayIE.ie_key()],
  363. }, {
  364. # Different og:image URL schema
  365. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
  366. 'only_matching': True,
  367. }, {
  368. 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
  369. 'only_matching': True,
  370. }, {
  371. 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
  372. 'only_matching': True,
  373. }]
  374. @classmethod
  375. def suitable(cls, url):
  376. return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
  377. def _real_extract(self, url):
  378. video_id = self._match_id(url)
  379. webpage = self._download_webpage(url, video_id)
  380. data = self._parse_json(
  381. self._search_regex(
  382. r'(?s)window\.App\s*=\s*({.+?})\s*;\s*</script',
  383. webpage, 'data', default='{}'),
  384. video_id, transform_source=lambda x: re.sub(
  385. r'(?s)function\s+[a-zA-Z_][\da-zA-Z_]*\s*\([^)]*\)\s*{[^}]*}\s*',
  386. 'null', x), fatal=False)
  387. video_id = None
  388. if data:
  389. video_id = try_get(
  390. data, lambda x: x['context']['dispatcher']['stores'][
  391. 'ContentPageProgramStore']['currentVideo']['id'],
  392. compat_str)
  393. # Fallback #1 (extract from og:image URL schema)
  394. if not video_id:
  395. thumbnail = self._og_search_thumbnail(webpage, default=None)
  396. if thumbnail:
  397. video_id = self._search_regex(
  398. # Patterns seen:
  399. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/inbox/765166/a2e95e5f1d735bab9f309fa345cc3f25.jpg
  400. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/seasons/15204/758770/4a5ba509ca8bc043e1ebd1a76131cdf2.jpg
  401. r'https?://[^/]+/imagecache/(?:[^/]+/)+(\d{6,})/',
  402. thumbnail, 'video id', default=None)
  403. # Fallback #2. Extract from raw JSON string.
  404. # May extract wrong video id if relatedClips is present.
  405. if not video_id:
  406. video_id = self._search_regex(
  407. r'currentVideo["\']\s*:\s*.+?["\']id["\']\s*:\s*["\'](\d{6,})',
  408. webpage, 'video id')
  409. return self.url_result(
  410. smuggle_url(
  411. 'mtg:%s' % video_id,
  412. {'geo_countries': [
  413. compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1]]}),
  414. ie=TVPlayIE.ie_key(), video_id=video_id)