tvplay.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import re
  2. import urllib.parse
  3. from .common import InfoExtractor
  4. from ..networking.exceptions import HTTPError
  5. from ..utils import (
  6. ExtractorError,
  7. determine_ext,
  8. int_or_none,
  9. parse_iso8601,
  10. qualities,
  11. traverse_obj,
  12. try_get,
  13. update_url_query,
  14. url_or_none,
  15. urljoin,
  16. )
  17. class TVPlayIE(InfoExtractor):
  18. IE_NAME = 'mtg'
  19. IE_DESC = 'MTG services'
  20. _VALID_URL = r'''(?x)
  21. (?:
  22. mtg:|
  23. https?://
  24. (?:www\.)?
  25. (?:
  26. tvplay(?:\.skaties)?\.lv(?:/parraides)?|
  27. (?:tv3play|play\.tv3)\.lt(?:/programos)?|
  28. tv3play(?:\.tv3)?\.ee/sisu
  29. )
  30. /(?:[^/]+/)+
  31. )
  32. (?P<id>\d+)
  33. '''
  34. _TESTS = [
  35. {
  36. 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
  37. 'md5': 'a1612fe0849455423ad8718fe049be21',
  38. 'info_dict': {
  39. 'id': '418113',
  40. 'ext': 'mp4',
  41. 'title': 'Kādi ir īri? - Viņas melo labāk',
  42. 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
  43. 'series': 'Viņas melo labāk',
  44. 'season': '2.sezona',
  45. 'season_number': 2,
  46. 'duration': 25,
  47. 'timestamp': 1406097056,
  48. 'upload_date': '20140723',
  49. },
  50. },
  51. {
  52. 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
  53. 'info_dict': {
  54. 'id': '409229',
  55. 'ext': 'flv',
  56. 'title': 'Moterys meluoja geriau',
  57. 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
  58. 'series': 'Moterys meluoja geriau',
  59. 'episode_number': 47,
  60. 'season': '1 sezonas',
  61. 'season_number': 1,
  62. 'duration': 1330,
  63. 'timestamp': 1403769181,
  64. 'upload_date': '20140626',
  65. },
  66. 'params': {
  67. # rtmp download
  68. 'skip_download': True,
  69. },
  70. },
  71. {
  72. 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
  73. 'info_dict': {
  74. 'id': '238551',
  75. 'ext': 'flv',
  76. 'title': 'Kodu keset linna 398537',
  77. 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
  78. 'duration': 1257,
  79. 'timestamp': 1292449761,
  80. 'upload_date': '20101215',
  81. },
  82. 'params': {
  83. # rtmp download
  84. 'skip_download': True,
  85. },
  86. },
  87. {
  88. 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
  89. 'only_matching': True,
  90. },
  91. {
  92. 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/418113/?autostart=true',
  93. 'only_matching': True,
  94. },
  95. {
  96. # views is null
  97. 'url': 'http://tvplay.skaties.lv/parraides/tv3-zinas/760183',
  98. 'only_matching': True,
  99. },
  100. {
  101. 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
  102. 'only_matching': True,
  103. },
  104. {
  105. 'url': 'mtg:418113',
  106. 'only_matching': True,
  107. },
  108. ]
  109. def _real_extract(self, url):
  110. video_id = self._match_id(url)
  111. geo_country = self._search_regex(
  112. r'https?://[^/]+\.([a-z]{2})', url,
  113. 'geo country', default=None)
  114. if geo_country:
  115. self._initialize_geo_bypass({'countries': [geo_country.upper()]})
  116. video = self._download_json(
  117. f'http://playapi.mtgx.tv/v3/videos/{video_id}', video_id, 'Downloading video JSON')
  118. title = video['title']
  119. try:
  120. streams = self._download_json(
  121. f'http://playapi.mtgx.tv/v3/videos/stream/{video_id}',
  122. video_id, 'Downloading streams JSON')
  123. except ExtractorError as e:
  124. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  125. msg = self._parse_json(e.cause.response.read().decode('utf-8'), video_id)
  126. raise ExtractorError(msg['msg'], expected=True)
  127. raise
  128. quality = qualities(['hls', 'medium', 'high'])
  129. formats = []
  130. for format_id, video_url in streams.get('streams', {}).items():
  131. video_url = url_or_none(video_url)
  132. if not video_url:
  133. continue
  134. ext = determine_ext(video_url)
  135. if ext == 'f4m':
  136. formats.extend(self._extract_f4m_formats(
  137. update_url_query(video_url, {
  138. 'hdcore': '3.5.0',
  139. 'plugin': 'aasp-3.5.0.151.81',
  140. }), video_id, f4m_id='hds', fatal=False))
  141. elif ext == 'm3u8':
  142. formats.extend(self._extract_m3u8_formats(
  143. video_url, video_id, 'mp4', 'm3u8_native',
  144. m3u8_id='hls', fatal=False))
  145. else:
  146. fmt = {
  147. 'format_id': format_id,
  148. 'quality': quality(format_id),
  149. 'ext': ext,
  150. }
  151. if video_url.startswith('rtmp'):
  152. m = re.search(
  153. r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
  154. if not m:
  155. continue
  156. fmt.update({
  157. 'ext': 'flv',
  158. 'url': m.group('url'),
  159. 'app': m.group('app'),
  160. 'play_path': m.group('playpath'),
  161. 'preference': -1,
  162. })
  163. else:
  164. fmt.update({
  165. 'url': video_url,
  166. })
  167. formats.append(fmt)
  168. if not formats and video.get('is_geo_blocked'):
  169. self.raise_geo_restricted(
  170. 'This content might not be available in your country due to copyright reasons',
  171. metadata_available=True)
  172. # TODO: webvtt in m3u8
  173. subtitles = {}
  174. sami_path = video.get('sami_path')
  175. if sami_path:
  176. lang = self._search_regex(
  177. r'_([a-z]{2})\.xml', sami_path, 'lang',
  178. default=urllib.parse.urlparse(url).netloc.rsplit('.', 1)[-1])
  179. subtitles[lang] = [{
  180. 'url': sami_path,
  181. }]
  182. series = video.get('format_title')
  183. episode_number = int_or_none(video.get('format_position', {}).get('episode'))
  184. season = video.get('_embedded', {}).get('season', {}).get('title')
  185. season_number = int_or_none(video.get('format_position', {}).get('season'))
  186. return {
  187. 'id': video_id,
  188. 'title': title,
  189. 'description': video.get('description'),
  190. 'series': series,
  191. 'episode_number': episode_number,
  192. 'season': season,
  193. 'season_number': season_number,
  194. 'duration': int_or_none(video.get('duration')),
  195. 'timestamp': parse_iso8601(video.get('created_at')),
  196. 'view_count': try_get(video, lambda x: x['views']['total'], int),
  197. 'age_limit': int_or_none(video.get('age_limit', 0)),
  198. 'formats': formats,
  199. 'subtitles': subtitles,
  200. }
  201. class TVPlayHomeIE(InfoExtractor):
  202. _VALID_URL = r'''(?x)
  203. https?://
  204. (?:tv3?)?
  205. play\.(?:tv3|skaties)\.(?P<country>lv|lt|ee)/
  206. (?P<live>lives/)?
  207. [^?#&]+(?:episode|programme|clip)-(?P<id>\d+)
  208. '''
  209. _TESTS = [{
  210. 'url': 'https://play.tv3.lt/series/gauju-karai-karveliai,serial-2343791/serija-8,episode-2343828',
  211. 'info_dict': {
  212. 'id': '2343828',
  213. 'ext': 'mp4',
  214. 'title': 'Gaujų karai. Karveliai (2021) | S01E08: Serija 8',
  215. 'description': 'md5:f6fcfbb236429f05531131640dfa7c81',
  216. 'duration': 2710,
  217. 'season': 'Gaujų karai. Karveliai',
  218. 'season_number': 1,
  219. 'release_year': 2021,
  220. 'episode': 'Serija 8',
  221. 'episode_number': 8,
  222. },
  223. 'params': {
  224. 'skip_download': 'm3u8',
  225. },
  226. }, {
  227. 'url': 'https://play.tv3.lt/series/moterys-meluoja-geriau-n-7,serial-2574652/serija-25,episode-3284937',
  228. 'info_dict': {
  229. 'id': '3284937',
  230. 'ext': 'mp4',
  231. 'season': 'Moterys meluoja geriau [N-7]',
  232. 'season_number': 14,
  233. 'release_year': 2021,
  234. 'episode': 'Serija 25',
  235. 'episode_number': 25,
  236. 'title': 'Moterys meluoja geriau [N-7] (2021) | S14|E25: Serija 25',
  237. 'description': 'md5:c6926e9710f1a126f028fbe121eddb79',
  238. 'duration': 2440,
  239. },
  240. 'skip': '404',
  241. }, {
  242. 'url': 'https://play.tv3.lt/lives/tv6-lt,live-2838694/optibet-a-lygos-rungtynes-marijampoles-suduva--vilniaus-riteriai,programme-3422014',
  243. 'only_matching': True,
  244. }, {
  245. 'url': 'https://tv3play.skaties.lv/series/women-lie-better-lv,serial-1024464/women-lie-better-lv,episode-1038762',
  246. 'only_matching': True,
  247. }, {
  248. 'url': 'https://play.tv3.ee/series/_,serial-2654462/_,episode-2654474',
  249. 'only_matching': True,
  250. }, {
  251. 'url': 'https://tv3play.skaties.lv/clips/tv3-zinas-valsti-lidz-15novembrim-bus-majsede,clip-3464509',
  252. 'only_matching': True,
  253. }]
  254. def _real_extract(self, url):
  255. country, is_live, video_id = self._match_valid_url(url).groups()
  256. api_path = 'lives/programmes' if is_live else 'vods'
  257. data = self._download_json(
  258. urljoin(url, f'/api/products/{api_path}/{video_id}?platform=BROWSER&lang={country.upper()}'),
  259. video_id)
  260. video_type = 'CATCHUP' if is_live else 'MOVIE'
  261. stream_id = data['programRecordingId'] if is_live else video_id
  262. stream = self._download_json(
  263. urljoin(url, f'/api/products/{stream_id}/videos/playlist?videoType={video_type}&platform=BROWSER'), video_id)
  264. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  265. stream['sources']['HLS'][0]['src'], video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
  266. thumbnails = set(traverse_obj(
  267. data, (('galary', 'images', 'artworks'), ..., ..., ('miniUrl', 'mainUrl')), expected_type=url_or_none))
  268. return {
  269. 'id': video_id,
  270. 'title': self._resolve_title(data),
  271. 'description': traverse_obj(data, 'description', 'lead'),
  272. 'duration': int_or_none(data.get('duration')),
  273. 'season': traverse_obj(data, ('season', 'serial', 'title')),
  274. 'season_number': int_or_none(traverse_obj(data, ('season', 'number'))),
  275. 'episode': data.get('title'),
  276. 'episode_number': int_or_none(data.get('episode')),
  277. 'release_year': int_or_none(traverse_obj(data, ('season', 'serial', 'year'))),
  278. 'thumbnails': [{'url': url, 'ext': 'jpg'} for url in thumbnails],
  279. 'formats': formats,
  280. 'subtitles': subtitles,
  281. }
  282. @staticmethod
  283. def _resolve_title(data):
  284. return try_get(data, lambda x: (
  285. f'{data["season"]["serial"]["title"]} ({data["season"]["serial"]["year"]}) | '
  286. f'S{data["season"]["number"]:02d}E{data["episode"]:02d}: {data["title"]}'
  287. )) or data.get('title')