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.

299 lines
11 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. xpath_text,
  11. )
  12. class AfreecaTVIE(InfoExtractor):
  13. IE_NAME = 'afreecatv'
  14. IE_DESC = 'afreecatv.com'
  15. _VALID_URL = r'''(?x)
  16. https?://
  17. (?:
  18. (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
  19. (?:
  20. /app/(?:index|read_ucc_bbs)\.cgi|
  21. /player/[Pp]layer\.(?:swf|html)
  22. )\?.*?\bnTitleNo=|
  23. vod\.afreecatv\.com/PLAYER/STATION/
  24. )
  25. (?P<id>\d+)
  26. '''
  27. _TESTS = [{
  28. 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
  29. 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
  30. 'info_dict': {
  31. 'id': '36164052',
  32. 'ext': 'mp4',
  33. 'title': '데일리 에이프릴 요정들의 시상식!',
  34. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  35. 'uploader': 'dailyapril',
  36. 'uploader_id': 'dailyapril',
  37. 'upload_date': '20160503',
  38. },
  39. 'skip': 'Video is gone',
  40. }, {
  41. 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
  42. 'info_dict': {
  43. 'id': '36153164',
  44. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  45. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  46. 'uploader': 'dailyapril',
  47. 'uploader_id': 'dailyapril',
  48. },
  49. 'playlist_count': 2,
  50. 'playlist': [{
  51. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  52. 'info_dict': {
  53. 'id': '36153164_1',
  54. 'ext': 'mp4',
  55. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  56. 'upload_date': '20160502',
  57. },
  58. }, {
  59. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  60. 'info_dict': {
  61. 'id': '36153164_2',
  62. 'ext': 'mp4',
  63. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  64. 'upload_date': '20160502',
  65. },
  66. }],
  67. 'skip': 'Video is gone',
  68. }, {
  69. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793',
  70. 'info_dict': {
  71. 'id': '18650793',
  72. 'ext': 'mp4',
  73. 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!',
  74. 'thumbnail': r're:^https?://.*\.jpg$',
  75. 'uploader': '윈아디',
  76. 'uploader_id': 'badkids',
  77. 'duration': 107,
  78. },
  79. 'params': {
  80. 'skip_download': True,
  81. },
  82. }, {
  83. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/10481652',
  84. 'info_dict': {
  85. 'id': '10481652',
  86. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  87. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  88. 'uploader': 'dailyapril',
  89. 'uploader_id': 'dailyapril',
  90. 'duration': 6492,
  91. },
  92. 'playlist_count': 2,
  93. 'playlist': [{
  94. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  95. 'info_dict': {
  96. 'id': '20160502_c4c62b9d_174361386_1',
  97. 'ext': 'mp4',
  98. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 1)",
  99. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  100. 'uploader': 'dailyapril',
  101. 'uploader_id': 'dailyapril',
  102. 'upload_date': '20160502',
  103. 'duration': 3601,
  104. },
  105. }, {
  106. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  107. 'info_dict': {
  108. 'id': '20160502_39e739bb_174361386_2',
  109. 'ext': 'mp4',
  110. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 2)",
  111. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  112. 'uploader': 'dailyapril',
  113. 'uploader_id': 'dailyapril',
  114. 'upload_date': '20160502',
  115. 'duration': 2891,
  116. },
  117. }],
  118. 'params': {
  119. 'skip_download': True,
  120. },
  121. }, {
  122. # non standard key
  123. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605',
  124. 'info_dict': {
  125. 'id': '20170411_BE689A0E_190960999_1_2_h',
  126. 'ext': 'mp4',
  127. 'title': '혼자사는여자집',
  128. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  129. 'uploader': '♥이슬이',
  130. 'uploader_id': 'dasl8121',
  131. 'upload_date': '20170411',
  132. 'duration': 213,
  133. },
  134. 'params': {
  135. 'skip_download': True,
  136. },
  137. }, {
  138. # adult video
  139. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/26542731',
  140. 'info_dict': {
  141. 'id': '20171001_F1AE1711_196617479_1',
  142. 'ext': 'mp4',
  143. 'title': '[생]서아 초심 찾기 방송 (part 1)',
  144. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  145. 'uploader': 'BJ서아',
  146. 'uploader_id': 'bjdyrksu',
  147. 'upload_date': '20171001',
  148. 'duration': 3600,
  149. 'age_limit': 18,
  150. },
  151. 'params': {
  152. 'skip_download': True,
  153. },
  154. }, {
  155. 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
  156. 'only_matching': True,
  157. }, {
  158. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
  159. 'only_matching': True,
  160. }]
  161. @staticmethod
  162. def parse_video_key(key):
  163. video_key = {}
  164. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  165. if m:
  166. video_key['upload_date'] = m.group('upload_date')
  167. video_key['part'] = int(m.group('part'))
  168. return video_key
  169. def _real_extract(self, url):
  170. video_id = self._match_id(url)
  171. webpage = self._download_webpage(url, video_id)
  172. if re.search(r'alert\(["\']This video has been deleted', webpage):
  173. raise ExtractorError(
  174. 'Video %s has been deleted' % video_id, expected=True)
  175. station_id = self._search_regex(
  176. r'nStationNo\s*=\s*(\d+)', webpage, 'station')
  177. bbs_id = self._search_regex(
  178. r'nBbsNo\s*=\s*(\d+)', webpage, 'bbs')
  179. video_id = self._search_regex(
  180. r'nTitleNo\s*=\s*(\d+)', webpage, 'title', default=video_id)
  181. video_xml = self._download_xml(
  182. 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
  183. video_id, headers={
  184. 'Referer': 'http://vod.afreecatv.com/embed.php',
  185. }, query={
  186. 'nTitleNo': video_id,
  187. 'nStationNo': station_id,
  188. 'nBbsNo': bbs_id,
  189. 'partialView': 'SKIP_ADULT',
  190. })
  191. flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
  192. if flag and flag != 'SUCCEED':
  193. raise ExtractorError(
  194. '%s said: %s' % (self.IE_NAME, flag), expected=True)
  195. video_element = video_xml.findall(compat_xpath('./track/video'))[-1]
  196. if video_element is None or video_element.text is None:
  197. raise ExtractorError(
  198. 'Video %s video does not exist' % video_id, expected=True)
  199. video_url = video_element.text.strip()
  200. title = xpath_text(video_xml, './track/title', 'title', fatal=True)
  201. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  202. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  203. duration = int_or_none(xpath_text(
  204. video_xml, './track/duration', 'duration'))
  205. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  206. common_entry = {
  207. 'uploader': uploader,
  208. 'uploader_id': uploader_id,
  209. 'thumbnail': thumbnail,
  210. }
  211. info = common_entry.copy()
  212. info.update({
  213. 'id': video_id,
  214. 'title': title,
  215. 'duration': duration,
  216. })
  217. if not video_url:
  218. entries = []
  219. file_elements = video_element.findall(compat_xpath('./file'))
  220. one = len(file_elements) == 1
  221. for file_num, file_element in enumerate(file_elements, start=1):
  222. file_url = file_element.text
  223. if not file_url:
  224. continue
  225. key = file_element.get('key', '')
  226. upload_date = self._search_regex(
  227. r'^(\d{8})_', key, 'upload date', default=None)
  228. file_duration = int_or_none(file_element.get('duration'))
  229. format_id = key if key else '%s_%s' % (video_id, file_num)
  230. if determine_ext(file_url) == 'm3u8':
  231. formats = self._extract_m3u8_formats(
  232. file_url, video_id, 'mp4', entry_protocol='m3u8_native',
  233. m3u8_id='hls',
  234. note='Downloading part %d m3u8 information' % file_num)
  235. else:
  236. formats = [{
  237. 'url': file_url,
  238. 'format_id': 'http',
  239. }]
  240. if not formats:
  241. continue
  242. self._sort_formats(formats)
  243. file_info = common_entry.copy()
  244. file_info.update({
  245. 'id': format_id,
  246. 'title': title if one else '%s (part %d)' % (title, file_num),
  247. 'upload_date': upload_date,
  248. 'duration': file_duration,
  249. 'formats': formats,
  250. })
  251. entries.append(file_info)
  252. entries_info = info.copy()
  253. entries_info.update({
  254. '_type': 'multi_video',
  255. 'entries': entries,
  256. })
  257. return entries_info
  258. info = {
  259. 'id': video_id,
  260. 'title': title,
  261. 'uploader': uploader,
  262. 'uploader_id': uploader_id,
  263. 'duration': duration,
  264. 'thumbnail': thumbnail,
  265. }
  266. if determine_ext(video_url) == 'm3u8':
  267. info['formats'] = self._extract_m3u8_formats(
  268. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  269. m3u8_id='hls')
  270. else:
  271. app, playpath = video_url.split('mp4:')
  272. info.update({
  273. 'url': app,
  274. 'ext': 'flv',
  275. 'play_path': 'mp4:' + playpath,
  276. 'rtmp_live': True, # downloading won't end without this
  277. })
  278. return info