This is a backup of https://github.com/ytdl-org/youtube-dl since it was taken down by the RIAA.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

afreecatv.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_xpath
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. int_or_none,
  10. url_or_none,
  11. urlencode_postdata,
  12. xpath_text,
  13. )
  14. class AfreecaTVIE(InfoExtractor):
  15. IE_NAME = 'afreecatv'
  16. IE_DESC = 'afreecatv.com'
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. (?:
  20. (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
  21. (?:
  22. /app/(?:index|read_ucc_bbs)\.cgi|
  23. /player/[Pp]layer\.(?:swf|html)
  24. )\?.*?\bnTitleNo=|
  25. vod\.afreecatv\.com/PLAYER/STATION/
  26. )
  27. (?P<id>\d+)
  28. '''
  29. _NETRC_MACHINE = 'afreecatv'
  30. _TESTS = [{
  31. 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
  32. 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
  33. 'info_dict': {
  34. 'id': '36164052',
  35. 'ext': 'mp4',
  36. 'title': '데일리 에이프릴 요정들의 시상식!',
  37. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  38. 'uploader': 'dailyapril',
  39. 'uploader_id': 'dailyapril',
  40. 'upload_date': '20160503',
  41. },
  42. 'skip': 'Video is gone',
  43. }, {
  44. 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
  45. 'info_dict': {
  46. 'id': '36153164',
  47. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  48. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  49. 'uploader': 'dailyapril',
  50. 'uploader_id': 'dailyapril',
  51. },
  52. 'playlist_count': 2,
  53. 'playlist': [{
  54. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  55. 'info_dict': {
  56. 'id': '36153164_1',
  57. 'ext': 'mp4',
  58. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  59. 'upload_date': '20160502',
  60. },
  61. }, {
  62. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  63. 'info_dict': {
  64. 'id': '36153164_2',
  65. 'ext': 'mp4',
  66. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  67. 'upload_date': '20160502',
  68. },
  69. }],
  70. 'skip': 'Video is gone',
  71. }, {
  72. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793',
  73. 'info_dict': {
  74. 'id': '18650793',
  75. 'ext': 'mp4',
  76. 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!',
  77. 'thumbnail': r're:^https?://.*\.jpg$',
  78. 'uploader': '윈아디',
  79. 'uploader_id': 'badkids',
  80. 'duration': 107,
  81. },
  82. 'params': {
  83. 'skip_download': True,
  84. },
  85. }, {
  86. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/10481652',
  87. 'info_dict': {
  88. 'id': '10481652',
  89. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  90. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  91. 'uploader': 'dailyapril',
  92. 'uploader_id': 'dailyapril',
  93. 'duration': 6492,
  94. },
  95. 'playlist_count': 2,
  96. 'playlist': [{
  97. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  98. 'info_dict': {
  99. 'id': '20160502_c4c62b9d_174361386_1',
  100. 'ext': 'mp4',
  101. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 1)",
  102. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  103. 'uploader': 'dailyapril',
  104. 'uploader_id': 'dailyapril',
  105. 'upload_date': '20160502',
  106. 'duration': 3601,
  107. },
  108. }, {
  109. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  110. 'info_dict': {
  111. 'id': '20160502_39e739bb_174361386_2',
  112. 'ext': 'mp4',
  113. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 2)",
  114. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  115. 'uploader': 'dailyapril',
  116. 'uploader_id': 'dailyapril',
  117. 'upload_date': '20160502',
  118. 'duration': 2891,
  119. },
  120. }],
  121. 'params': {
  122. 'skip_download': True,
  123. },
  124. }, {
  125. # non standard key
  126. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605',
  127. 'info_dict': {
  128. 'id': '20170411_BE689A0E_190960999_1_2_h',
  129. 'ext': 'mp4',
  130. 'title': '혼자사는여자집',
  131. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  132. 'uploader': '♥이슬이',
  133. 'uploader_id': 'dasl8121',
  134. 'upload_date': '20170411',
  135. 'duration': 213,
  136. },
  137. 'params': {
  138. 'skip_download': True,
  139. },
  140. }, {
  141. # PARTIAL_ADULT
  142. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/32028439',
  143. 'info_dict': {
  144. 'id': '20180327_27901457_202289533_1',
  145. 'ext': 'mp4',
  146. 'title': '[생]빨개요♥ (part 1)',
  147. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  148. 'uploader': '[SA]서아',
  149. 'uploader_id': 'bjdyrksu',
  150. 'upload_date': '20180327',
  151. 'duration': 3601,
  152. },
  153. 'params': {
  154. 'skip_download': True,
  155. },
  156. 'expected_warnings': ['adult content'],
  157. }, {
  158. 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
  159. 'only_matching': True,
  160. }, {
  161. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
  162. 'only_matching': True,
  163. }]
  164. @staticmethod
  165. def parse_video_key(key):
  166. video_key = {}
  167. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  168. if m:
  169. video_key['upload_date'] = m.group('upload_date')
  170. video_key['part'] = int(m.group('part'))
  171. return video_key
  172. def _real_initialize(self):
  173. self._login()
  174. def _login(self):
  175. username, password = self._get_login_info()
  176. if username is None:
  177. return
  178. login_form = {
  179. 'szWork': 'login',
  180. 'szType': 'json',
  181. 'szUid': username,
  182. 'szPassword': password,
  183. 'isSaveId': 'false',
  184. 'szScriptVar': 'oLoginRet',
  185. 'szAction': '',
  186. }
  187. response = self._download_json(
  188. 'https://login.afreecatv.com/app/LoginAction.php', None,
  189. 'Logging in', data=urlencode_postdata(login_form))
  190. _ERRORS = {
  191. -4: 'Your account has been suspended due to a violation of our terms and policies.',
  192. -5: 'https://member.afreecatv.com/app/user_delete_progress.php',
  193. -6: 'https://login.afreecatv.com/membership/changeMember.php',
  194. -8: "Hello! AfreecaTV 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.",
  195. -9: 'https://member.afreecatv.com/app/pop_login_block.php',
  196. -11: 'https://login.afreecatv.com/afreeca/second_login.php',
  197. -12: 'https://member.afreecatv.com/app/user_security.php',
  198. 0: 'The username does not exist or you have entered the wrong password.',
  199. -1: 'The username does not exist or you have entered the wrong password.',
  200. -3: 'You have entered your username/password incorrectly.',
  201. -7: 'You cannot use your Global AfreecaTV account to access Korean AfreecaTV.',
  202. -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
  203. -32008: 'You have failed to log in. Please contact our Help Center.',
  204. }
  205. result = int_or_none(response.get('RESULT'))
  206. if result != 1:
  207. error = _ERRORS.get(result, 'You have failed to log in.')
  208. raise ExtractorError(
  209. 'Unable to login: %s said: %s' % (self.IE_NAME, error),
  210. expected=True)
  211. def _real_extract(self, url):
  212. video_id = self._match_id(url)
  213. webpage = self._download_webpage(url, video_id)
  214. if re.search(r'alert\(["\']This video has been deleted', webpage):
  215. raise ExtractorError(
  216. 'Video %s has been deleted' % video_id, expected=True)
  217. station_id = self._search_regex(
  218. r'nStationNo\s*=\s*(\d+)', webpage, 'station')
  219. bbs_id = self._search_regex(
  220. r'nBbsNo\s*=\s*(\d+)', webpage, 'bbs')
  221. video_id = self._search_regex(
  222. r'nTitleNo\s*=\s*(\d+)', webpage, 'title', default=video_id)
  223. partial_view = False
  224. for _ in range(2):
  225. query = {
  226. 'nTitleNo': video_id,
  227. 'nStationNo': station_id,
  228. 'nBbsNo': bbs_id,
  229. }
  230. if partial_view:
  231. query['partialView'] = 'SKIP_ADULT'
  232. video_xml = self._download_xml(
  233. 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
  234. video_id, 'Downloading video info XML%s'
  235. % (' (skipping adult)' if partial_view else ''),
  236. video_id, headers={
  237. 'Referer': url,
  238. }, query=query)
  239. flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
  240. if flag and flag == 'SUCCEED':
  241. break
  242. if flag == 'PARTIAL_ADULT':
  243. self._downloader.report_warning(
  244. 'In accordance with local laws and regulations, underage users are restricted from watching adult content. '
  245. 'Only content suitable for all ages will be downloaded. '
  246. 'Provide account credentials if you wish to download restricted content.')
  247. partial_view = True
  248. continue
  249. elif flag == 'ADULT':
  250. error = 'Only users older than 19 are able to watch this video. Provide account credentials to download this content.'
  251. else:
  252. error = flag
  253. raise ExtractorError(
  254. '%s said: %s' % (self.IE_NAME, error), expected=True)
  255. else:
  256. raise ExtractorError('Unable to download video info')
  257. video_element = video_xml.findall(compat_xpath('./track/video'))[-1]
  258. if video_element is None or video_element.text is None:
  259. raise ExtractorError(
  260. 'Video %s does not exist' % video_id, expected=True)
  261. video_url = video_element.text.strip()
  262. title = xpath_text(video_xml, './track/title', 'title', fatal=True)
  263. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  264. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  265. duration = int_or_none(xpath_text(
  266. video_xml, './track/duration', 'duration'))
  267. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  268. common_entry = {
  269. 'uploader': uploader,
  270. 'uploader_id': uploader_id,
  271. 'thumbnail': thumbnail,
  272. }
  273. info = common_entry.copy()
  274. info.update({
  275. 'id': video_id,
  276. 'title': title,
  277. 'duration': duration,
  278. })
  279. if not video_url:
  280. entries = []
  281. file_elements = video_element.findall(compat_xpath('./file'))
  282. one = len(file_elements) == 1
  283. for file_num, file_element in enumerate(file_elements, start=1):
  284. file_url = url_or_none(file_element.text)
  285. if not file_url:
  286. continue
  287. key = file_element.get('key', '')
  288. upload_date = self._search_regex(
  289. r'^(\d{8})_', key, 'upload date', default=None)
  290. file_duration = int_or_none(file_element.get('duration'))
  291. format_id = key if key else '%s_%s' % (video_id, file_num)
  292. if determine_ext(file_url) == 'm3u8':
  293. formats = self._extract_m3u8_formats(
  294. file_url, video_id, 'mp4', entry_protocol='m3u8_native',
  295. m3u8_id='hls',
  296. note='Downloading part %d m3u8 information' % file_num)
  297. else:
  298. formats = [{
  299. 'url': file_url,
  300. 'format_id': 'http',
  301. }]
  302. if not formats:
  303. continue
  304. self._sort_formats(formats)
  305. file_info = common_entry.copy()
  306. file_info.update({
  307. 'id': format_id,
  308. 'title': title if one else '%s (part %d)' % (title, file_num),
  309. 'upload_date': upload_date,
  310. 'duration': file_duration,
  311. 'formats': formats,
  312. })
  313. entries.append(file_info)
  314. entries_info = info.copy()
  315. entries_info.update({
  316. '_type': 'multi_video',
  317. 'entries': entries,
  318. })
  319. return entries_info
  320. info = {
  321. 'id': video_id,
  322. 'title': title,
  323. 'uploader': uploader,
  324. 'uploader_id': uploader_id,
  325. 'duration': duration,
  326. 'thumbnail': thumbnail,
  327. }
  328. if determine_ext(video_url) == 'm3u8':
  329. info['formats'] = self._extract_m3u8_formats(
  330. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  331. m3u8_id='hls')
  332. else:
  333. app, playpath = video_url.split('mp4:')
  334. info.update({
  335. 'url': app,
  336. 'ext': 'flv',
  337. 'play_path': 'mp4:' + playpath,
  338. 'rtmp_live': True, # downloading won't end without this
  339. })
  340. return info