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.

352 lines
13 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. 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
  139. 'only_matching': True,
  140. }, {
  141. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
  142. 'only_matching': True,
  143. }]
  144. @staticmethod
  145. def parse_video_key(key):
  146. video_key = {}
  147. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  148. if m:
  149. video_key['upload_date'] = m.group('upload_date')
  150. video_key['part'] = int(m.group('part'))
  151. return video_key
  152. def _real_extract(self, url):
  153. video_id = self._match_id(url)
  154. video_xml = self._download_xml(
  155. 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
  156. video_id, query={'nTitleNo': video_id})
  157. video_element = video_xml.findall(compat_xpath('./track/video'))[1]
  158. if video_element is None or video_element.text is None:
  159. raise ExtractorError('Specified AfreecaTV video does not exist',
  160. expected=True)
  161. video_url = video_element.text.strip()
  162. title = xpath_text(video_xml, './track/title', 'title', fatal=True)
  163. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  164. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  165. duration = int_or_none(xpath_text(
  166. video_xml, './track/duration', 'duration'))
  167. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  168. common_entry = {
  169. 'uploader': uploader,
  170. 'uploader_id': uploader_id,
  171. 'thumbnail': thumbnail,
  172. }
  173. info = common_entry.copy()
  174. info.update({
  175. 'id': video_id,
  176. 'title': title,
  177. 'duration': duration,
  178. })
  179. if not video_url:
  180. entries = []
  181. file_elements = video_element.findall(compat_xpath('./file'))
  182. one = len(file_elements) == 1
  183. for file_num, file_element in enumerate(file_elements, start=1):
  184. file_url = file_element.text
  185. if not file_url:
  186. continue
  187. key = file_element.get('key', '')
  188. upload_date = self._search_regex(
  189. r'^(\d{8})_', key, 'upload date', default=None)
  190. file_duration = int_or_none(file_element.get('duration'))
  191. format_id = key if key else '%s_%s' % (video_id, file_num)
  192. formats = self._extract_m3u8_formats(
  193. file_url, video_id, 'mp4', entry_protocol='m3u8_native',
  194. m3u8_id='hls',
  195. note='Downloading part %d m3u8 information' % file_num)
  196. file_info = common_entry.copy()
  197. file_info.update({
  198. 'id': format_id,
  199. 'title': title if one else '%s (part %d)' % (title, file_num),
  200. 'upload_date': upload_date,
  201. 'duration': file_duration,
  202. 'formats': formats,
  203. })
  204. entries.append(file_info)
  205. entries_info = info.copy()
  206. entries_info.update({
  207. '_type': 'multi_video',
  208. 'entries': entries,
  209. })
  210. return entries_info
  211. info = {
  212. 'id': video_id,
  213. 'title': title,
  214. 'uploader': uploader,
  215. 'uploader_id': uploader_id,
  216. 'duration': duration,
  217. 'thumbnail': thumbnail,
  218. }
  219. if determine_ext(video_url) == 'm3u8':
  220. info['formats'] = self._extract_m3u8_formats(
  221. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  222. m3u8_id='hls')
  223. else:
  224. app, playpath = video_url.split('mp4:')
  225. info.update({
  226. 'url': app,
  227. 'ext': 'flv',
  228. 'play_path': 'mp4:' + playpath,
  229. 'rtmp_live': True, # downloading won't end without this
  230. })
  231. return info
  232. class AfreecaTVGlobalIE(AfreecaTVIE):
  233. IE_NAME = 'afreecatv:global'
  234. _VALID_URL = r'https?://(?:www\.)?afreeca\.tv/(?P<channel_id>\d+)(?:/v/(?P<video_id>\d+))?'
  235. _TESTS = [{
  236. 'url': 'http://afreeca.tv/36853014/v/58301',
  237. 'info_dict': {
  238. 'id': '58301',
  239. 'title': 'tryhard top100',
  240. 'uploader_id': '36853014',
  241. 'uploader': 'makgi Hearthstone Live!',
  242. },
  243. 'playlist_count': 3,
  244. }]
  245. def _real_extract(self, url):
  246. channel_id, video_id = re.match(self._VALID_URL, url).groups()
  247. video_type = 'video' if video_id else 'live'
  248. query = {
  249. 'pt': 'view',
  250. 'bid': channel_id,
  251. }
  252. if video_id:
  253. query['vno'] = video_id
  254. video_data = self._download_json(
  255. 'http://api.afreeca.tv/%s/view_%s.php' % (video_type, video_type),
  256. video_id or channel_id, query=query)['channel']
  257. if video_data.get('result') != 1:
  258. raise ExtractorError('%s said: %s' % (self.IE_NAME, video_data['remsg']))
  259. title = video_data['title']
  260. info = {
  261. 'thumbnail': video_data.get('thumb'),
  262. 'view_count': int_or_none(video_data.get('vcnt')),
  263. 'age_limit': int_or_none(video_data.get('grade')),
  264. 'uploader_id': channel_id,
  265. 'uploader': video_data.get('cname'),
  266. }
  267. if video_id:
  268. entries = []
  269. for i, f in enumerate(video_data.get('flist', [])):
  270. video_key = self.parse_video_key(f.get('key', ''))
  271. f_url = f.get('file')
  272. if not video_key or not f_url:
  273. continue
  274. entries.append({
  275. 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
  276. 'title': title,
  277. 'upload_date': video_key.get('upload_date'),
  278. 'duration': int_or_none(f.get('length')),
  279. 'url': f_url,
  280. 'protocol': 'm3u8_native',
  281. 'ext': 'mp4',
  282. })
  283. info.update({
  284. 'id': video_id,
  285. 'title': title,
  286. 'duration': int_or_none(video_data.get('length')),
  287. })
  288. if len(entries) > 1:
  289. info['_type'] = 'multi_video'
  290. info['entries'] = entries
  291. elif len(entries) == 1:
  292. i = entries[0].copy()
  293. i.update(info)
  294. info = i
  295. else:
  296. formats = []
  297. for s in video_data.get('strm', []):
  298. s_url = s.get('purl')
  299. if not s_url:
  300. continue
  301. stype = s.get('stype')
  302. if stype == 'HLS':
  303. formats.extend(self._extract_m3u8_formats(
  304. s_url, channel_id, 'mp4', m3u8_id=stype, fatal=False))
  305. elif stype == 'RTMP':
  306. format_id = [stype]
  307. label = s.get('label')
  308. if label:
  309. format_id.append(label)
  310. formats.append({
  311. 'format_id': '-'.join(format_id),
  312. 'url': s_url,
  313. 'tbr': int_or_none(s.get('bps')),
  314. 'height': int_or_none(s.get('brt')),
  315. 'ext': 'flv',
  316. 'rtmp_live': True,
  317. })
  318. self._sort_formats(formats)
  319. info.update({
  320. 'id': channel_id,
  321. 'title': self._live_title(title),
  322. 'is_live': True,
  323. 'formats': formats,
  324. })
  325. return info