tvplay.py 16 KB

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