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.

366 lines
14 KiB

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