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.

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