afreecatv.py 12 KB

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