1
0

tvplay.py 15 KB

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