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.

445 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. if smuggled_data.get('skip_rtmp'):
  268. continue
  269. m = re.search(
  270. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  271. if not m:
  272. continue
  273. fmt.update({
  274. 'ext': 'flv',
  275. 'url': m.group('url'),
  276. 'app': m.group('app'),
  277. 'play_path': m.group('playpath'),
  278. })
  279. else:
  280. fmt.update({
  281. 'url': video_url,
  282. })
  283. formats.append(fmt)
  284. if not formats and video.get('is_geo_blocked'):
  285. self.raise_geo_restricted(
  286. 'This content might not be available in your country due to copyright reasons')
  287. self._sort_formats(formats)
  288. # TODO: webvtt in m3u8
  289. subtitles = {}
  290. sami_path = video.get('sami_path')
  291. if sami_path:
  292. lang = self._search_regex(
  293. r'_([a-z]{2})\.xml', sami_path, 'lang',
  294. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  295. subtitles[lang] = [{
  296. 'url': sami_path,
  297. }]
  298. series = video.get('format_title')
  299. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  300. season = video.get('_embedded', {}).get('season', {}).get('title')
  301. season_number = int_or_none(video.get('format_position', {}).get('season'))
  302. return {
  303. 'id': video_id,
  304. 'title': title,
  305. 'description': video.get('description'),
  306. 'series': series,
  307. 'episode_number': episode_number,
  308. 'season': season,
  309. 'season_number': season_number,
  310. 'duration': int_or_none(video.get('duration')),
  311. 'timestamp': parse_iso8601(video.get('created_at')),
  312. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  313. 'age_limit': int_or_none(video.get('age_limit', 0)),
  314. 'formats': formats,
  315. 'subtitles': subtitles,
  316. }
  317. class ViafreeIE(InfoExtractor):
  318. _VALID_URL = r'''(?x)
  319. https?://
  320. (?:www\.)?
  321. viafree\.
  322. (?:
  323. (?:dk|no)/programmer|
  324. se/program
  325. )
  326. /(?:[^/]+/)+(?P<id>[^/?#&]+)
  327. '''
  328. _TESTS = [{
  329. 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
  330. 'info_dict': {
  331. 'id': '395375',
  332. 'ext': 'mp4',
  333. 'title': 'Husräddarna S02E02',
  334. 'description': 'md5:4db5c933e37db629b5a2f75dfb34829e',
  335. 'series': 'Husräddarna',
  336. 'season': 'Säsong 2',
  337. 'season_number': 2,
  338. 'duration': 2576,
  339. 'timestamp': 1400596321,
  340. 'upload_date': '20140520',
  341. },
  342. 'params': {
  343. 'skip_download': True,
  344. },
  345. 'add_ie': [TVPlayIE.ie_key()],
  346. }, {
  347. # with relatedClips
  348. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
  349. 'info_dict': {
  350. 'id': '758770',
  351. 'ext': 'mp4',
  352. 'title': 'Sommaren med YouTube-stjärnorna S01E01',
  353. 'description': 'md5:2bc69dce2c4bb48391e858539bbb0e3f',
  354. 'series': 'Sommaren med YouTube-stjärnorna',
  355. 'season': 'Säsong 1',
  356. 'season_number': 1,
  357. 'duration': 1326,
  358. 'timestamp': 1470905572,
  359. 'upload_date': '20160811',
  360. },
  361. 'params': {
  362. 'skip_download': True,
  363. },
  364. 'add_ie': [TVPlayIE.ie_key()],
  365. }, {
  366. # Different og:image URL schema
  367. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
  368. 'only_matching': True,
  369. }, {
  370. 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
  371. 'only_matching': True,
  372. }, {
  373. 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
  374. 'only_matching': True,
  375. }]
  376. @classmethod
  377. def suitable(cls, url):
  378. return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
  379. def _real_extract(self, url):
  380. video_id = self._match_id(url)
  381. webpage = self._download_webpage(url, video_id)
  382. data = self._parse_json(
  383. self._search_regex(
  384. r'(?s)window\.App\s*=\s*({.+?})\s*;\s*</script',
  385. webpage, 'data', default='{}'),
  386. video_id, transform_source=lambda x: re.sub(
  387. r'(?s)function\s+[a-zA-Z_][\da-zA-Z_]*\s*\([^)]*\)\s*{[^}]*}\s*',
  388. 'null', x), fatal=False)
  389. video_id = None
  390. if data:
  391. video_id = try_get(
  392. data, lambda x: x['context']['dispatcher']['stores'][
  393. 'ContentPageProgramStore']['currentVideo']['id'],
  394. compat_str)
  395. # Fallback #1 (extract from og:image URL schema)
  396. if not video_id:
  397. thumbnail = self._og_search_thumbnail(webpage, default=None)
  398. if thumbnail:
  399. video_id = self._search_regex(
  400. # Patterns seen:
  401. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/inbox/765166/a2e95e5f1d735bab9f309fa345cc3f25.jpg
  402. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/seasons/15204/758770/4a5ba509ca8bc043e1ebd1a76131cdf2.jpg
  403. r'https?://[^/]+/imagecache/(?:[^/]+/)+(\d{6,})/',
  404. thumbnail, 'video id', default=None)
  405. # Fallback #2. Extract from raw JSON string.
  406. # May extract wrong video id if relatedClips is present.
  407. if not video_id:
  408. video_id = self._search_regex(
  409. r'currentVideo["\']\s*:\s*.+?["\']id["\']\s*:\s*["\'](\d{6,})',
  410. webpage, 'video id')
  411. return self.url_result(
  412. smuggle_url(
  413. 'mtg:%s' % video_id,
  414. {
  415. 'geo_countries': [
  416. compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1]],
  417. # rtmp host mtgfs.fplive.net for viafree is unresolvable
  418. 'skip_rtmp': True,
  419. }),
  420. ie=TVPlayIE.ie_key(), video_id=video_id)