logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

afreecatv.py (17971B)


  1. import functools
  2. from .common import InfoExtractor
  3. from ..networking import Request
  4. from ..utils import (
  5. ExtractorError,
  6. OnDemandPagedList,
  7. UserNotLive,
  8. determine_ext,
  9. filter_dict,
  10. int_or_none,
  11. orderedSet,
  12. unified_timestamp,
  13. url_or_none,
  14. urlencode_postdata,
  15. urljoin,
  16. )
  17. from ..utils.traversal import traverse_obj
  18. class AfreecaTVBaseIE(InfoExtractor):
  19. _NETRC_MACHINE = 'afreecatv'
  20. def _perform_login(self, username, password):
  21. login_form = {
  22. 'szWork': 'login',
  23. 'szType': 'json',
  24. 'szUid': username,
  25. 'szPassword': password,
  26. 'isSaveId': 'false',
  27. 'szScriptVar': 'oLoginRet',
  28. 'szAction': '',
  29. }
  30. response = self._download_json(
  31. 'https://login.sooplive.co.kr/app/LoginAction.php', None,
  32. 'Logging in', data=urlencode_postdata(login_form))
  33. _ERRORS = {
  34. -4: 'Your account has been suspended due to a violation of our terms and policies.',
  35. -5: 'https://member.sooplive.co.kr/app/user_delete_progress.php',
  36. -6: 'https://login.sooplive.co.kr/membership/changeMember.php',
  37. -8: "Hello! Soop here.\nThe username you have entered belongs to \n an account that requires a legal guardian's consent. \nIf you wish to use our services without restriction, \nplease make sure to go through the necessary verification process.",
  38. -9: 'https://member.sooplive.co.kr/app/pop_login_block.php',
  39. -11: 'https://login.sooplive.co.kr/afreeca/second_login.php',
  40. -12: 'https://member.sooplive.co.kr/app/user_security.php',
  41. 0: 'The username does not exist or you have entered the wrong password.',
  42. -1: 'The username does not exist or you have entered the wrong password.',
  43. -3: 'You have entered your username/password incorrectly.',
  44. -7: 'You cannot use your Global Soop account to access Korean Soop.',
  45. -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
  46. -32008: 'You have failed to log in. Please contact our Help Center.',
  47. }
  48. result = int_or_none(response.get('RESULT'))
  49. if result != 1:
  50. error = _ERRORS.get(result, 'You have failed to log in.')
  51. raise ExtractorError(
  52. f'Unable to login: {self.IE_NAME} said: {error}',
  53. expected=True)
  54. def _call_api(self, endpoint, display_id, data=None, headers=None, query=None):
  55. return self._download_json(Request(
  56. f'https://api.m.sooplive.co.kr/{endpoint}',
  57. data=data, headers=headers, query=query,
  58. extensions={'legacy_ssl': True}), display_id,
  59. 'Downloading API JSON', 'Unable to download API JSON')
  60. @staticmethod
  61. def _fixup_thumb(thumb_url):
  62. if not url_or_none(thumb_url):
  63. return None
  64. # Core would determine_ext as 'php' from the url, so we need to provide the real ext
  65. # See: https://github.com/yt-dlp/yt-dlp/issues/11537
  66. return [{'url': thumb_url, 'ext': 'jpg'}]
  67. class AfreecaTVIE(AfreecaTVBaseIE):
  68. IE_NAME = 'soop'
  69. IE_DESC = 'sooplive.co.kr'
  70. _VALID_URL = r'https?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/(?:PLAYER/STATION|player)/(?P<id>\d+)/?(?:$|[?#&])'
  71. _TESTS = [{
  72. 'url': 'https://vod.sooplive.co.kr/player/96753363',
  73. 'info_dict': {
  74. 'id': '20230108_9FF5BEE1_244432674_1',
  75. 'ext': 'mp4',
  76. 'uploader_id': 'rlantnghks',
  77. 'uploader': '페이즈으',
  78. 'duration': 10840,
  79. 'thumbnail': r're:https?://videoimg\.sooplive\.co/.kr/.+',
  80. 'upload_date': '20230108',
  81. 'timestamp': 1673218805,
  82. 'title': '젠지 페이즈',
  83. },
  84. 'params': {
  85. 'skip_download': True,
  86. },
  87. }, {
  88. # non standard key
  89. 'url': 'http://vod.sooplive.co.kr/PLAYER/STATION/20515605',
  90. 'info_dict': {
  91. 'id': '20170411_BE689A0E_190960999_1_2_h',
  92. 'ext': 'mp4',
  93. 'title': '혼자사는여자집',
  94. 'thumbnail': r're:https?://(?:video|st)img\.sooplive\.co\.kr/.+',
  95. 'uploader': '♥이슬이',
  96. 'uploader_id': 'dasl8121',
  97. 'upload_date': '20170411',
  98. 'timestamp': 1491929865,
  99. 'duration': 213,
  100. },
  101. 'params': {
  102. 'skip_download': True,
  103. },
  104. }, {
  105. # adult content
  106. 'url': 'https://vod.sooplive.co.kr/player/97267690',
  107. 'info_dict': {
  108. 'id': '20180327_27901457_202289533_1',
  109. 'ext': 'mp4',
  110. 'title': '[생]빨개요♥ (part 1)',
  111. 'thumbnail': r're:https?://(?:video|st)img\.sooplive\.co\.kr/.+',
  112. 'uploader': '[SA]서아',
  113. 'uploader_id': 'bjdyrksu',
  114. 'upload_date': '20180327',
  115. 'duration': 3601,
  116. },
  117. 'params': {
  118. 'skip_download': True,
  119. },
  120. 'skip': 'The VOD does not exist',
  121. }, {
  122. # adult content
  123. 'url': 'https://vod.sooplive.co.kr/player/70395877',
  124. 'only_matching': True,
  125. }, {
  126. # subscribers only
  127. 'url': 'https://vod.sooplive.co.kr/player/104647403',
  128. 'only_matching': True,
  129. }, {
  130. # private
  131. 'url': 'https://vod.sooplive.co.kr/player/81669846',
  132. 'only_matching': True,
  133. }]
  134. def _real_extract(self, url):
  135. video_id = self._match_id(url)
  136. data = self._call_api(
  137. 'station/video/a/view', video_id, headers={'Referer': url},
  138. data=urlencode_postdata({
  139. 'nTitleNo': video_id,
  140. 'nApiLevel': 10,
  141. }))['data']
  142. error_code = traverse_obj(data, ('code', {int}))
  143. if error_code == -6221:
  144. raise ExtractorError('The VOD does not exist', expected=True)
  145. elif error_code == -6205:
  146. raise ExtractorError('This VOD is private', expected=True)
  147. common_info = traverse_obj(data, {
  148. 'title': ('title', {str}),
  149. 'uploader': ('writer_nick', {str}),
  150. 'uploader_id': ('bj_id', {str}),
  151. 'duration': ('total_file_duration', {int_or_none(scale=1000)}),
  152. 'thumbnails': ('thumb', {self._fixup_thumb}),
  153. })
  154. entries = []
  155. for file_num, file_element in enumerate(
  156. traverse_obj(data, ('files', lambda _, v: url_or_none(v['file']))), start=1):
  157. file_url = file_element['file']
  158. if determine_ext(file_url) == 'm3u8':
  159. formats = self._extract_m3u8_formats(
  160. file_url, video_id, 'mp4', m3u8_id='hls',
  161. note=f'Downloading part {file_num} m3u8 information')
  162. else:
  163. formats = [{
  164. 'url': file_url,
  165. 'format_id': 'http',
  166. }]
  167. entries.append({
  168. **common_info,
  169. 'id': file_element.get('file_info_key') or f'{video_id}_{file_num}',
  170. 'title': f'{common_info.get("title") or "Untitled"} (part {file_num})',
  171. 'formats': formats,
  172. **traverse_obj(file_element, {
  173. 'duration': ('duration', {int_or_none(scale=1000)}),
  174. 'timestamp': ('file_start', {unified_timestamp}),
  175. }),
  176. })
  177. if traverse_obj(data, ('adult_status', {str})) == 'notLogin':
  178. if not entries:
  179. self.raise_login_required(
  180. 'Only users older than 19 are able to watch this video', method='password')
  181. self.report_warning(
  182. 'In accordance with local laws and regulations, underage users are '
  183. 'restricted from watching adult content. Only content suitable for all '
  184. f'ages will be downloaded. {self._login_hint("password")}')
  185. if not entries and traverse_obj(data, ('sub_upload_type', {str})):
  186. self.raise_login_required('This VOD is for subscribers only', method='password')
  187. if len(entries) == 1:
  188. return {
  189. **entries[0],
  190. 'title': common_info.get('title'),
  191. }
  192. common_info['timestamp'] = traverse_obj(entries, (..., 'timestamp'), get_all=False)
  193. return self.playlist_result(entries, video_id, multi_video=True, **common_info)
  194. class AfreecaTVCatchStoryIE(AfreecaTVBaseIE):
  195. IE_NAME = 'soop:catchstory'
  196. IE_DESC = 'sooplive.co.kr catch story'
  197. _VALID_URL = r'https?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/player/(?P<id>\d+)/catchstory'
  198. _TESTS = [{
  199. 'url': 'https://vod.sooplive.co.kr/player/103247/catchstory',
  200. 'info_dict': {
  201. 'id': '103247',
  202. },
  203. 'playlist_count': 2,
  204. }]
  205. def _real_extract(self, url):
  206. video_id = self._match_id(url)
  207. data = self._call_api(
  208. 'catchstory/a/view', video_id, headers={'Referer': url},
  209. query={'aStoryListIdx': '', 'nStoryIdx': video_id})
  210. return self.playlist_result(self._entries(data), video_id)
  211. def _entries(self, data):
  212. # 'files' is always a list with 1 element
  213. yield from traverse_obj(data, (
  214. 'data', lambda _, v: v['story_type'] == 'catch',
  215. 'catch_list', lambda _, v: v['files'][0]['file'], {
  216. 'id': ('files', 0, 'file_info_key', {str}),
  217. 'url': ('files', 0, 'file', {url_or_none}),
  218. 'duration': ('files', 0, 'duration', {int_or_none(scale=1000)}),
  219. 'title': ('title', {str}),
  220. 'uploader': ('writer_nick', {str}),
  221. 'uploader_id': ('writer_id', {str}),
  222. 'thumbnails': ('thumb', {self._fixup_thumb}),
  223. 'timestamp': ('write_timestamp', {int_or_none}),
  224. }))
  225. class AfreecaTVLiveIE(AfreecaTVBaseIE):
  226. IE_NAME = 'soop:live'
  227. IE_DESC = 'sooplive.co.kr livestreams'
  228. _VALID_URL = r'https?://play\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)(?:/(?P<bno>\d+))?'
  229. _TESTS = [{
  230. 'url': 'https://play.sooplive.co.kr/pyh3646/237852185',
  231. 'info_dict': {
  232. 'id': '237852185',
  233. 'ext': 'mp4',
  234. 'title': '【 우루과이 오늘은 무슨일이? 】',
  235. 'uploader': '박진우[JINU]',
  236. 'uploader_id': 'pyh3646',
  237. 'timestamp': 1640661495,
  238. 'is_live': True,
  239. },
  240. 'skip': 'Livestream has ended',
  241. }, {
  242. 'url': 'https://play.sooplive.co.kr/pyh3646/237852185',
  243. 'only_matching': True,
  244. }, {
  245. 'url': 'https://play.sooplive.co.kr/pyh3646',
  246. 'only_matching': True,
  247. }]
  248. _LIVE_API_URL = 'https://live.sooplive.co.kr/afreeca/player_live_api.php'
  249. _WORKING_CDNS = [
  250. 'gcp_cdn', # live-global-cdn-v02.sooplive.co.kr
  251. 'gs_cdn_pc_app', # pc-app.stream.sooplive.co.kr
  252. 'gs_cdn_mobile_web', # mobile-web.stream.sooplive.co.kr
  253. 'gs_cdn_pc_web', # pc-web.stream.sooplive.co.kr
  254. ]
  255. _BAD_CDNS = [
  256. 'gs_cdn', # chromecast.afreeca.gscdn.com (cannot resolve)
  257. 'gs_cdn_chromecast', # chromecast.stream.sooplive.co.kr (HTTP Error 400)
  258. 'azure_cdn', # live-global-cdn-v01.sooplive.co.kr (cannot resolve)
  259. 'aws_cf', # live-global-cdn-v03.sooplive.co.kr (cannot resolve)
  260. 'kt_cdn', # kt.stream.sooplive.co.kr (HTTP Error 400)
  261. ]
  262. def _extract_formats(self, channel_info, broadcast_no, aid):
  263. stream_base_url = channel_info.get('RMD') or 'https://livestream-manager.sooplive.co.kr'
  264. # If user has not passed CDN IDs, try API-provided CDN ID followed by other working CDN IDs
  265. default_cdn_ids = orderedSet([
  266. *traverse_obj(channel_info, ('CDN', {str}, all, lambda _, v: v not in self._BAD_CDNS)),
  267. *self._WORKING_CDNS,
  268. ])
  269. cdn_ids = self._configuration_arg('cdn', default_cdn_ids)
  270. for attempt, cdn_id in enumerate(cdn_ids, start=1):
  271. m3u8_url = traverse_obj(self._download_json(
  272. urljoin(stream_base_url, 'broad_stream_assign.html'), broadcast_no,
  273. f'Downloading {cdn_id} stream info', f'Unable to download {cdn_id} stream info',
  274. fatal=False, query={
  275. 'return_type': cdn_id,
  276. 'broad_key': f'{broadcast_no}-common-master-hls',
  277. }), ('view_url', {url_or_none}))
  278. try:
  279. return self._extract_m3u8_formats(
  280. m3u8_url, broadcast_no, 'mp4', m3u8_id='hls', query={'aid': aid},
  281. headers={'Referer': 'https://play.sooplive.co.kr/'})
  282. except ExtractorError as e:
  283. if attempt == len(cdn_ids):
  284. raise
  285. self.report_warning(
  286. f'{e.cause or e.msg}. Retrying... (attempt {attempt} of {len(cdn_ids)})')
  287. def _real_extract(self, url):
  288. broadcaster_id, broadcast_no = self._match_valid_url(url).group('id', 'bno')
  289. channel_info = traverse_obj(self._download_json(
  290. self._LIVE_API_URL, broadcaster_id, data=urlencode_postdata({'bid': broadcaster_id})),
  291. ('CHANNEL', {dict})) or {}
  292. broadcaster_id = channel_info.get('BJID') or broadcaster_id
  293. broadcast_no = channel_info.get('BNO') or broadcast_no
  294. if not broadcast_no:
  295. result = channel_info.get('RESULT')
  296. if result == 0:
  297. raise UserNotLive(video_id=broadcaster_id)
  298. elif result == -6:
  299. self.raise_login_required(
  300. 'This channel is streaming for subscribers only', method='password')
  301. raise ExtractorError('Unable to extract broadcast number')
  302. password = self.get_param('videopassword')
  303. if channel_info.get('BPWD') == 'Y' and password is None:
  304. raise ExtractorError(
  305. 'This livestream is protected by a password, use the --video-password option',
  306. expected=True)
  307. token_info = traverse_obj(self._download_json(
  308. self._LIVE_API_URL, broadcast_no, 'Downloading access token for stream',
  309. 'Unable to download access token for stream', data=urlencode_postdata(filter_dict({
  310. 'bno': broadcast_no,
  311. 'stream_type': 'common',
  312. 'type': 'aid',
  313. 'quality': 'master',
  314. 'pwd': password,
  315. }))), ('CHANNEL', {dict})) or {}
  316. aid = token_info.get('AID')
  317. if not aid:
  318. result = token_info.get('RESULT')
  319. if result == 0:
  320. raise ExtractorError('This livestream has ended', expected=True)
  321. elif result == -6:
  322. self.raise_login_required('This livestream is for subscribers only', method='password')
  323. raise ExtractorError('Unable to extract access token')
  324. formats = self._extract_formats(channel_info, broadcast_no, aid)
  325. station_info = traverse_obj(self._download_json(
  326. 'https://st.sooplive.co.kr/api/get_station_status.php', broadcast_no,
  327. 'Downloading channel metadata', 'Unable to download channel metadata',
  328. query={'szBjId': broadcaster_id}, fatal=False), {dict}) or {}
  329. return {
  330. 'id': broadcast_no,
  331. 'title': channel_info.get('TITLE') or station_info.get('station_title'),
  332. 'uploader': channel_info.get('BJNICK') or station_info.get('station_name'),
  333. 'uploader_id': broadcaster_id,
  334. 'timestamp': unified_timestamp(station_info.get('broad_start')),
  335. 'formats': formats,
  336. 'is_live': True,
  337. 'http_headers': {'Referer': url},
  338. }
  339. class AfreecaTVUserIE(AfreecaTVBaseIE):
  340. IE_NAME = 'soop:user'
  341. _VALID_URL = r'https?://ch\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)/vods/?(?P<slug_type>[^/?#]+)?'
  342. _TESTS = [{
  343. 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods/review',
  344. 'info_dict': {
  345. '_type': 'playlist',
  346. 'id': 'ryuryu24',
  347. 'title': 'ryuryu24 - review',
  348. },
  349. 'playlist_count': 218,
  350. }, {
  351. 'url': 'https://ch.sooplive.co.kr/parang1995/vods/highlight',
  352. 'info_dict': {
  353. '_type': 'playlist',
  354. 'id': 'parang1995',
  355. 'title': 'parang1995 - highlight',
  356. },
  357. 'playlist_count': 997,
  358. }, {
  359. 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods',
  360. 'info_dict': {
  361. '_type': 'playlist',
  362. 'id': 'ryuryu24',
  363. 'title': 'ryuryu24 - all',
  364. },
  365. 'playlist_count': 221,
  366. }, {
  367. 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods/balloonclip',
  368. 'info_dict': {
  369. '_type': 'playlist',
  370. 'id': 'ryuryu24',
  371. 'title': 'ryuryu24 - balloonclip',
  372. },
  373. 'playlist_count': 0,
  374. }]
  375. _PER_PAGE = 60
  376. def _fetch_page(self, user_id, user_type, page):
  377. page += 1
  378. info = self._download_json(f'https://chapi.sooplive.co.kr/api/{user_id}/vods/{user_type}', user_id,
  379. query={'page': page, 'per_page': self._PER_PAGE, 'orderby': 'reg_date'},
  380. note=f'Downloading {user_type} video page {page}')
  381. for item in info['data']:
  382. yield self.url_result(
  383. f'https://vod.sooplive.co.kr/player/{item["title_no"]}/', AfreecaTVIE, item['title_no'])
  384. def _real_extract(self, url):
  385. user_id, user_type = self._match_valid_url(url).group('id', 'slug_type')
  386. user_type = user_type or 'all'
  387. entries = OnDemandPagedList(functools.partial(self._fetch_page, user_id, user_type), self._PER_PAGE)
  388. return self.playlist_result(entries, user_id, f'{user_id} - {user_type}')