afreecatv.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. title = title if one else '%s (part %d)' % (title, file_num)
  197. file_info = common_entry.copy()
  198. file_info.update({
  199. 'id': format_id,
  200. 'title': title,
  201. 'upload_date': upload_date,
  202. 'duration': file_duration,
  203. 'formats': formats,
  204. })
  205. entries.append(file_info)
  206. entries_info = info.copy()
  207. entries_info.update({
  208. '_type': 'multi_video',
  209. 'entries': entries,
  210. })
  211. return entries_info
  212. info = {
  213. 'id': video_id,
  214. 'title': title,
  215. 'uploader': uploader,
  216. 'uploader_id': uploader_id,
  217. 'duration': duration,
  218. 'thumbnail': thumbnail,
  219. }
  220. if determine_ext(video_url) == 'm3u8':
  221. info['formats'] = self._extract_m3u8_formats(
  222. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  223. m3u8_id='hls')
  224. else:
  225. app, playpath = video_url.split('mp4:')
  226. info.update({
  227. 'url': app,
  228. 'ext': 'flv',
  229. 'play_path': 'mp4:' + playpath,
  230. 'rtmp_live': True, # downloading won't end without this
  231. })
  232. return info
  233. class AfreecaTVGlobalIE(AfreecaTVIE):
  234. IE_NAME = 'afreecatv:global'
  235. _VALID_URL = r'https?://(?:www\.)?afreeca\.tv/(?P<channel_id>\d+)(?:/v/(?P<video_id>\d+))?'
  236. _TESTS = [{
  237. 'url': 'http://afreeca.tv/36853014/v/58301',
  238. 'info_dict': {
  239. 'id': '58301',
  240. 'title': 'tryhard top100',
  241. 'uploader_id': '36853014',
  242. 'uploader': 'makgi Hearthstone Live!',
  243. },
  244. 'playlist_count': 3,
  245. }]
  246. def _real_extract(self, url):
  247. channel_id, video_id = re.match(self._VALID_URL, url).groups()
  248. video_type = 'video' if video_id else 'live'
  249. query = {
  250. 'pt': 'view',
  251. 'bid': channel_id,
  252. }
  253. if video_id:
  254. query['vno'] = video_id
  255. video_data = self._download_json(
  256. 'http://api.afreeca.tv/%s/view_%s.php' % (video_type, video_type),
  257. video_id or channel_id, query=query)['channel']
  258. if video_data.get('result') != 1:
  259. raise ExtractorError('%s said: %s' % (self.IE_NAME, video_data['remsg']))
  260. title = video_data['title']
  261. info = {
  262. 'thumbnail': video_data.get('thumb'),
  263. 'view_count': int_or_none(video_data.get('vcnt')),
  264. 'age_limit': int_or_none(video_data.get('grade')),
  265. 'uploader_id': channel_id,
  266. 'uploader': video_data.get('cname'),
  267. }
  268. if video_id:
  269. entries = []
  270. for i, f in enumerate(video_data.get('flist', [])):
  271. video_key = self.parse_video_key(f.get('key', ''))
  272. f_url = f.get('file')
  273. if not video_key or not f_url:
  274. continue
  275. entries.append({
  276. 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
  277. 'title': title,
  278. 'upload_date': video_key.get('upload_date'),
  279. 'duration': int_or_none(f.get('length')),
  280. 'url': f_url,
  281. 'protocol': 'm3u8_native',
  282. 'ext': 'mp4',
  283. })
  284. info.update({
  285. 'id': video_id,
  286. 'title': title,
  287. 'duration': int_or_none(video_data.get('length')),
  288. })
  289. if len(entries) > 1:
  290. info['_type'] = 'multi_video'
  291. info['entries'] = entries
  292. elif len(entries) == 1:
  293. i = entries[0].copy()
  294. i.update(info)
  295. info = i
  296. else:
  297. formats = []
  298. for s in video_data.get('strm', []):
  299. s_url = s.get('purl')
  300. if not s_url:
  301. continue
  302. stype = s.get('stype')
  303. if stype == 'HLS':
  304. formats.extend(self._extract_m3u8_formats(
  305. s_url, channel_id, 'mp4', m3u8_id=stype, fatal=False))
  306. elif stype == 'RTMP':
  307. format_id = [stype]
  308. label = s.get('label')
  309. if label:
  310. format_id.append(label)
  311. formats.append({
  312. 'format_id': '-'.join(format_id),
  313. 'url': s_url,
  314. 'tbr': int_or_none(s.get('bps')),
  315. 'height': int_or_none(s.get('brt')),
  316. 'ext': 'flv',
  317. 'rtmp_live': True,
  318. })
  319. self._sort_formats(formats)
  320. info.update({
  321. 'id': channel_id,
  322. 'title': self._live_title(title),
  323. 'is_live': True,
  324. 'formats': formats,
  325. })
  326. return info