afreecatv.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. # adult video
  141. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/26542731',
  142. 'info_dict': {
  143. 'id': '20171001_F1AE1711_196617479_1',
  144. 'ext': 'mp4',
  145. 'title': '[생]서아 초심 찾기 방송 (part 1)',
  146. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  147. 'uploader': 'BJ서아',
  148. 'uploader_id': 'bjdyrksu',
  149. 'upload_date': '20171001',
  150. 'duration': 3600,
  151. 'age_limit': 18,
  152. },
  153. 'params': {
  154. 'skip_download': True,
  155. },
  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. video_xml = self._download_xml(
  223. 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
  224. video_id, headers={
  225. 'Referer': url,
  226. }, query={
  227. 'nTitleNo': video_id,
  228. 'nStationNo': station_id,
  229. 'nBbsNo': bbs_id,
  230. 'partialView': 'SKIP_ADULT',
  231. })
  232. flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
  233. if flag and flag != 'SUCCEED':
  234. raise ExtractorError(
  235. '%s said: %s' % (self.IE_NAME, flag), expected=True)
  236. video_element = video_xml.findall(compat_xpath('./track/video'))[-1]
  237. if video_element is None or video_element.text is None:
  238. raise ExtractorError(
  239. 'Video %s video does not exist' % video_id, expected=True)
  240. video_url = video_element.text.strip()
  241. title = xpath_text(video_xml, './track/title', 'title', fatal=True)
  242. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  243. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  244. duration = int_or_none(xpath_text(
  245. video_xml, './track/duration', 'duration'))
  246. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  247. common_entry = {
  248. 'uploader': uploader,
  249. 'uploader_id': uploader_id,
  250. 'thumbnail': thumbnail,
  251. }
  252. info = common_entry.copy()
  253. info.update({
  254. 'id': video_id,
  255. 'title': title,
  256. 'duration': duration,
  257. })
  258. if not video_url:
  259. entries = []
  260. file_elements = video_element.findall(compat_xpath('./file'))
  261. one = len(file_elements) == 1
  262. for file_num, file_element in enumerate(file_elements, start=1):
  263. file_url = file_element.text
  264. if not file_url:
  265. continue
  266. key = file_element.get('key', '')
  267. upload_date = self._search_regex(
  268. r'^(\d{8})_', key, 'upload date', default=None)
  269. file_duration = int_or_none(file_element.get('duration'))
  270. format_id = key if key else '%s_%s' % (video_id, file_num)
  271. if determine_ext(file_url) == 'm3u8':
  272. formats = self._extract_m3u8_formats(
  273. file_url, video_id, 'mp4', entry_protocol='m3u8_native',
  274. m3u8_id='hls',
  275. note='Downloading part %d m3u8 information' % file_num)
  276. else:
  277. formats = [{
  278. 'url': file_url,
  279. 'format_id': 'http',
  280. }]
  281. if not formats:
  282. continue
  283. self._sort_formats(formats)
  284. file_info = common_entry.copy()
  285. file_info.update({
  286. 'id': format_id,
  287. 'title': title if one else '%s (part %d)' % (title, file_num),
  288. 'upload_date': upload_date,
  289. 'duration': file_duration,
  290. 'formats': formats,
  291. })
  292. entries.append(file_info)
  293. entries_info = info.copy()
  294. entries_info.update({
  295. '_type': 'multi_video',
  296. 'entries': entries,
  297. })
  298. return entries_info
  299. info = {
  300. 'id': video_id,
  301. 'title': title,
  302. 'uploader': uploader,
  303. 'uploader_id': uploader_id,
  304. 'duration': duration,
  305. 'thumbnail': thumbnail,
  306. }
  307. if determine_ext(video_url) == 'm3u8':
  308. info['formats'] = self._extract_m3u8_formats(
  309. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  310. m3u8_id='hls')
  311. else:
  312. app, playpath = video_url.split('mp4:')
  313. info.update({
  314. 'url': app,
  315. 'ext': 'flv',
  316. 'play_path': 'mp4:' + playpath,
  317. 'rtmp_live': True, # downloading won't end without this
  318. })
  319. return info