tvplay.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. update_url_query,
  17. )
  18. class TVPlayIE(InfoExtractor):
  19. IE_NAME = 'mtg'
  20. IE_DESC = 'MTG services'
  21. _VALID_URL = r'''(?x)
  22. (?:
  23. mtg:|
  24. https?://
  25. (?:www\.)?
  26. (?:
  27. tvplay(?:\.skaties)?\.lv/parraides|
  28. (?:tv3play|play\.tv3)\.lt/programos|
  29. tv3play(?:\.tv3)?\.ee/sisu|
  30. (?:tv(?:3|6|8|10)play|viafree)\.se/program|
  31. (?:(?:tv3play|viasat4play|tv6play|viafree)\.no|(?:tv3play|viafree)\.dk)/programmer|
  32. play\.novatv\.bg/programi
  33. )
  34. /(?:[^/]+/)+
  35. )
  36. (?P<id>\d+)
  37. '''
  38. _TESTS = [
  39. {
  40. 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
  41. 'md5': 'a1612fe0849455423ad8718fe049be21',
  42. 'info_dict': {
  43. 'id': '418113',
  44. 'ext': 'mp4',
  45. 'title': 'Kādi ir īri? - Viņas melo labāk',
  46. 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
  47. 'series': 'Viņas melo labāk',
  48. 'season': '2.sezona',
  49. 'season_number': 2,
  50. 'duration': 25,
  51. 'timestamp': 1406097056,
  52. 'upload_date': '20140723',
  53. },
  54. },
  55. {
  56. 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
  57. 'info_dict': {
  58. 'id': '409229',
  59. 'ext': 'flv',
  60. 'title': 'Moterys meluoja geriau',
  61. 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
  62. 'series': 'Moterys meluoja geriau',
  63. 'episode_number': 47,
  64. 'season': '1 sezonas',
  65. 'season_number': 1,
  66. 'duration': 1330,
  67. 'timestamp': 1403769181,
  68. 'upload_date': '20140626',
  69. },
  70. 'params': {
  71. # rtmp download
  72. 'skip_download': True,
  73. },
  74. },
  75. {
  76. 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
  77. 'info_dict': {
  78. 'id': '238551',
  79. 'ext': 'flv',
  80. 'title': 'Kodu keset linna 398537',
  81. 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
  82. 'duration': 1257,
  83. 'timestamp': 1292449761,
  84. 'upload_date': '20101215',
  85. },
  86. 'params': {
  87. # rtmp download
  88. 'skip_download': True,
  89. },
  90. },
  91. {
  92. 'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
  93. 'info_dict': {
  94. 'id': '395385',
  95. 'ext': 'mp4',
  96. 'title': 'Husräddarna S02E07',
  97. 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
  98. 'duration': 2574,
  99. 'timestamp': 1400596321,
  100. 'upload_date': '20140520',
  101. },
  102. 'params': {
  103. 'skip_download': True,
  104. },
  105. },
  106. {
  107. 'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
  108. 'info_dict': {
  109. 'id': '266636',
  110. 'ext': 'mp4',
  111. 'title': 'Den sista dokusåpan S01E08',
  112. 'description': 'md5:295be39c872520221b933830f660b110',
  113. 'duration': 1492,
  114. 'timestamp': 1330522854,
  115. 'upload_date': '20120229',
  116. 'age_limit': 18,
  117. },
  118. 'params': {
  119. 'skip_download': True,
  120. },
  121. },
  122. {
  123. 'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
  124. 'info_dict': {
  125. 'id': '282756',
  126. 'ext': 'mp4',
  127. 'title': 'Antikjakten S01E10',
  128. 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
  129. 'duration': 2646,
  130. 'timestamp': 1348575868,
  131. 'upload_date': '20120925',
  132. },
  133. 'params': {
  134. 'skip_download': True,
  135. },
  136. },
  137. {
  138. 'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
  139. 'info_dict': {
  140. 'id': '230898',
  141. 'ext': 'mp4',
  142. 'title': 'Anna Anka søker assistent - Ep. 8',
  143. 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
  144. 'duration': 2656,
  145. 'timestamp': 1277720005,
  146. 'upload_date': '20100628',
  147. },
  148. 'params': {
  149. 'skip_download': True,
  150. },
  151. },
  152. {
  153. 'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
  154. 'info_dict': {
  155. 'id': '21873',
  156. 'ext': 'mp4',
  157. 'title': 'Budbringerne program 10',
  158. 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
  159. 'duration': 1297,
  160. 'timestamp': 1254205102,
  161. 'upload_date': '20090929',
  162. },
  163. 'params': {
  164. 'skip_download': True,
  165. },
  166. },
  167. {
  168. 'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
  169. 'info_dict': {
  170. 'id': '361883',
  171. 'ext': 'mp4',
  172. 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
  173. 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
  174. 'duration': 2594,
  175. 'timestamp': 1393236292,
  176. 'upload_date': '20140224',
  177. },
  178. 'params': {
  179. 'skip_download': True,
  180. },
  181. },
  182. {
  183. 'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
  184. 'info_dict': {
  185. 'id': '624952',
  186. 'ext': 'flv',
  187. 'title': 'Здравей, България (12.06.2015 г.) ',
  188. 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
  189. 'duration': 8838,
  190. 'timestamp': 1434100372,
  191. 'upload_date': '20150612',
  192. },
  193. 'params': {
  194. # rtmp download
  195. 'skip_download': True,
  196. },
  197. },
  198. {
  199. 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
  200. 'only_matching': True,
  201. },
  202. {
  203. 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
  204. 'only_matching': True,
  205. },
  206. {
  207. 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
  208. 'only_matching': True,
  209. },
  210. {
  211. 'url': 'mtg:418113',
  212. 'only_matching': True,
  213. }
  214. ]
  215. def _real_extract(self, url):
  216. video_id = self._match_id(url)
  217. video = self._download_json(
  218. 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
  219. title = video['title']
  220. try:
  221. streams = self._download_json(
  222. 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
  223. video_id, 'Downloading streams JSON')
  224. except ExtractorError as e:
  225. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  226. msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
  227. raise ExtractorError(msg['msg'], expected=True)
  228. raise
  229. quality = qualities(['hls', 'medium', 'high'])
  230. formats = []
  231. for format_id, video_url in streams.get('streams', {}).items():
  232. if not video_url or not isinstance(video_url, compat_str):
  233. continue
  234. ext = determine_ext(video_url)
  235. if ext == 'f4m':
  236. formats.extend(self._extract_f4m_formats(
  237. update_url_query(video_url, {
  238. 'hdcore': '3.5.0',
  239. 'plugin': 'aasp-3.5.0.151.81'
  240. }), video_id, f4m_id='hds', fatal=False))
  241. elif ext == 'm3u8':
  242. formats.extend(self._extract_m3u8_formats(
  243. video_url, video_id, 'mp4', 'm3u8_native',
  244. m3u8_id='hls', fatal=False))
  245. else:
  246. fmt = {
  247. 'format_id': format_id,
  248. 'quality': quality(format_id),
  249. 'ext': ext,
  250. }
  251. if video_url.startswith('rtmp'):
  252. m = re.search(
  253. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  254. if not m:
  255. continue
  256. fmt.update({
  257. 'ext': 'flv',
  258. 'url': m.group('url'),
  259. 'app': m.group('app'),
  260. 'play_path': m.group('playpath'),
  261. })
  262. else:
  263. fmt.update({
  264. 'url': video_url,
  265. })
  266. formats.append(fmt)
  267. if not formats and video.get('is_geo_blocked'):
  268. self.raise_geo_restricted(
  269. 'This content might not be available in your country due to copyright reasons')
  270. self._sort_formats(formats)
  271. # TODO: webvtt in m3u8
  272. subtitles = {}
  273. sami_path = video.get('sami_path')
  274. if sami_path:
  275. lang = self._search_regex(
  276. r'_([a-z]{2})\.xml', sami_path, 'lang',
  277. default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
  278. subtitles[lang] = [{
  279. 'url': sami_path,
  280. }]
  281. series = video.get('format_title')
  282. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  283. season = video.get('_embedded', {}).get('season', {}).get('title')
  284. season_number = int_or_none(video.get('format_position', {}).get('season'))
  285. return {
  286. 'id': video_id,
  287. 'title': title,
  288. 'description': video.get('description'),
  289. 'series': series,
  290. 'episode_number': episode_number,
  291. 'season': season,
  292. 'season_number': season_number,
  293. 'duration': int_or_none(video.get('duration')),
  294. 'timestamp': parse_iso8601(video.get('created_at')),
  295. 'view_count': int_or_none(video.get('views', {}).get('total')),
  296. 'age_limit': int_or_none(video.get('age_limit', 0)),
  297. 'formats': formats,
  298. 'subtitles': subtitles,
  299. }