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.

429 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. 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. geo_country = self._search_regex(
  224. r'https?://[^/]+\.([a-z]{2})', url,
  225. 'geo country', default=None)
  226. if geo_country:
  227. self._initialize_geo_bypass([geo_country.upper()])
  228. video = self._download_json(
  229. 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
  230. title = video['title']
  231. try:
  232. streams = self._download_json(
  233. 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
  234. video_id, 'Downloading streams JSON')
  235. except ExtractorError as e:
  236. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  237. msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
  238. raise ExtractorError(msg['msg'], expected=True)
  239. raise
  240. quality = qualities(['hls', 'medium', 'high'])
  241. formats = []
  242. for format_id, video_url in streams.get('streams', {}).items():
  243. if not video_url or not isinstance(video_url, compat_str):
  244. continue
  245. ext = determine_ext(video_url)
  246. if ext == 'f4m':
  247. formats.extend(self._extract_f4m_formats(
  248. update_url_query(video_url, {
  249. 'hdcore': '3.5.0',
  250. 'plugin': 'aasp-3.5.0.151.81'
  251. }), video_id, f4m_id='hds', fatal=False))
  252. elif ext == 'm3u8':
  253. formats.extend(self._extract_m3u8_formats(
  254. video_url, video_id, 'mp4', 'm3u8_native',
  255. m3u8_id='hls', fatal=False))
  256. else:
  257. fmt = {
  258. 'format_id': format_id,
  259. 'quality': quality(format_id),
  260. 'ext': ext,
  261. }
  262. if video_url.startswith('rtmp'):
  263. m = re.search(
  264. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  265. if not m:
  266. continue
  267. fmt.update({
  268. 'ext': 'flv',
  269. 'url': m.group('url'),
  270. 'app': m.group('app'),
  271. 'play_path': m.group('playpath'),
  272. })
  273. else:
  274. fmt.update({
  275. 'url': video_url,
  276. })
  277. formats.append(fmt)
  278. if not formats and video.get('is_geo_blocked'):
  279. self.raise_geo_restricted(
  280. 'This content might not be available in your country due to copyright reasons')
  281. self._sort_formats(formats)
  282. # TODO: webvtt in m3u8
  283. subtitles = {}
  284. sami_path = video.get('sami_path')
  285. if sami_path:
  286. lang = self._search_regex(
  287. r'_([a-z]{2})\.xml', sami_path, 'lang',
  288. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  289. subtitles[lang] = [{
  290. 'url': sami_path,
  291. }]
  292. series = video.get('format_title')
  293. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  294. season = video.get('_embedded', {}).get('season', {}).get('title')
  295. season_number = int_or_none(video.get('format_position', {}).get('season'))
  296. return {
  297. 'id': video_id,
  298. 'title': title,
  299. 'description': video.get('description'),
  300. 'series': series,
  301. 'episode_number': episode_number,
  302. 'season': season,
  303. 'season_number': season_number,
  304. 'duration': int_or_none(video.get('duration')),
  305. 'timestamp': parse_iso8601(video.get('created_at')),
  306. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  307. 'age_limit': int_or_none(video.get('age_limit', 0)),
  308. 'formats': formats,
  309. 'subtitles': subtitles,
  310. }
  311. class ViafreeIE(InfoExtractor):
  312. _VALID_URL = r'''(?x)
  313. https?://
  314. (?:www\.)?
  315. viafree\.
  316. (?:
  317. (?:dk|no)/programmer|
  318. se/program
  319. )
  320. /(?:[^/]+/)+(?P<id>[^/?#&]+)
  321. '''
  322. _TESTS = [{
  323. 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
  324. 'info_dict': {
  325. 'id': '395375',
  326. 'ext': 'mp4',
  327. 'title': 'Husräddarna S02E02',
  328. 'description': 'md5:4db5c933e37db629b5a2f75dfb34829e',
  329. 'series': 'Husräddarna',
  330. 'season': 'Säsong 2',
  331. 'season_number': 2,
  332. 'duration': 2576,
  333. 'timestamp': 1400596321,
  334. 'upload_date': '20140520',
  335. },
  336. 'params': {
  337. 'skip_download': True,
  338. },
  339. 'add_ie': [TVPlayIE.ie_key()],
  340. }, {
  341. # with relatedClips
  342. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
  343. 'info_dict': {
  344. 'id': '758770',
  345. 'ext': 'mp4',
  346. 'title': 'Sommaren med YouTube-stjärnorna S01E01',
  347. 'description': 'md5:2bc69dce2c4bb48391e858539bbb0e3f',
  348. 'series': 'Sommaren med YouTube-stjärnorna',
  349. 'season': 'Säsong 1',
  350. 'season_number': 1,
  351. 'duration': 1326,
  352. 'timestamp': 1470905572,
  353. 'upload_date': '20160811',
  354. },
  355. 'params': {
  356. 'skip_download': True,
  357. },
  358. 'add_ie': [TVPlayIE.ie_key()],
  359. }, {
  360. # Different og:image URL schema
  361. 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
  362. 'only_matching': True,
  363. }, {
  364. 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
  365. 'only_matching': True,
  366. }, {
  367. 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
  368. 'only_matching': True,
  369. }]
  370. @classmethod
  371. def suitable(cls, url):
  372. return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
  373. def _real_extract(self, url):
  374. video_id = self._match_id(url)
  375. webpage = self._download_webpage(url, video_id)
  376. data = self._parse_json(
  377. self._search_regex(
  378. r'(?s)window\.App\s*=\s*({.+?})\s*;\s*</script',
  379. webpage, 'data', default='{}'),
  380. video_id, transform_source=lambda x: re.sub(
  381. r'(?s)function\s+[a-zA-Z_][\da-zA-Z_]*\s*\([^)]*\)\s*{[^}]*}\s*',
  382. 'null', x), fatal=False)
  383. video_id = None
  384. if data:
  385. video_id = try_get(
  386. data, lambda x: x['context']['dispatcher']['stores'][
  387. 'ContentPageProgramStore']['currentVideo']['id'],
  388. compat_str)
  389. # Fallback #1 (extract from og:image URL schema)
  390. if not video_id:
  391. thumbnail = self._og_search_thumbnail(webpage, default=None)
  392. if thumbnail:
  393. video_id = self._search_regex(
  394. # Patterns seen:
  395. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/inbox/765166/a2e95e5f1d735bab9f309fa345cc3f25.jpg
  396. # http://cdn.playapi.mtgx.tv/imagecache/600x315/cloud/content-images/seasons/15204/758770/4a5ba509ca8bc043e1ebd1a76131cdf2.jpg
  397. r'https?://[^/]+/imagecache/(?:[^/]+/)+(\d{6,})/',
  398. thumbnail, 'video id', default=None)
  399. # Fallback #2. Extract from raw JSON string.
  400. # May extract wrong video id if relatedClips is present.
  401. if not video_id:
  402. video_id = self._search_regex(
  403. r'currentVideo["\']\s*:\s*.+?["\']id["\']\s*:\s*["\'](\d{6,})',
  404. webpage, 'video id')
  405. return self.url_result('mtg:%s' % video_id, TVPlayIE.ie_key())