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.

238 lines
8.6 KiB

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. ExtractorError,
  8. int_or_none,
  9. xpath_text,
  10. )
  11. class AfreecaTVIE(InfoExtractor):
  12. IE_NAME = 'afreecatv'
  13. IE_DESC = 'afreecatv.com'
  14. _VALID_URL = r'''(?x)
  15. https?://
  16. (?:
  17. (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
  18. (?:
  19. /app/(?:index|read_ucc_bbs)\.cgi|
  20. /player/[Pp]layer\.(?:swf|html)
  21. )\?.*?\bnTitleNo=|
  22. vod\.afreecatv\.com/PLAYER/STATION/
  23. )
  24. (?P<id>\d+)
  25. '''
  26. _TESTS = [{
  27. 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
  28. 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
  29. 'info_dict': {
  30. 'id': '36164052',
  31. 'ext': 'mp4',
  32. 'title': '데일리 에이프릴 요정들의 시상식!',
  33. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  34. 'uploader': 'dailyapril',
  35. 'uploader_id': 'dailyapril',
  36. 'upload_date': '20160503',
  37. },
  38. 'skip': 'Video is gone',
  39. }, {
  40. 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
  41. 'info_dict': {
  42. 'id': '36153164',
  43. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  44. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  45. 'uploader': 'dailyapril',
  46. 'uploader_id': 'dailyapril',
  47. },
  48. 'playlist_count': 2,
  49. 'playlist': [{
  50. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  51. 'info_dict': {
  52. 'id': '36153164_1',
  53. 'ext': 'mp4',
  54. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  55. 'upload_date': '20160502',
  56. },
  57. }, {
  58. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  59. 'info_dict': {
  60. 'id': '36153164_2',
  61. 'ext': 'mp4',
  62. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  63. 'upload_date': '20160502',
  64. },
  65. }],
  66. 'skip': 'Video is gone',
  67. }, {
  68. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793',
  69. 'info_dict': {
  70. 'id': '18650793',
  71. 'ext': 'flv',
  72. 'uploader': '윈아디',
  73. 'uploader_id': 'badkids',
  74. 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!',
  75. },
  76. 'params': {
  77. 'skip_download': True, # requires rtmpdump
  78. },
  79. }, {
  80. 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
  81. 'only_matching': True,
  82. }, {
  83. 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
  84. 'only_matching': True,
  85. }]
  86. @staticmethod
  87. def parse_video_key(key):
  88. video_key = {}
  89. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  90. if m:
  91. video_key['upload_date'] = m.group('upload_date')
  92. video_key['part'] = m.group('part')
  93. return video_key
  94. def _real_extract(self, url):
  95. video_id = self._match_id(url)
  96. video_xml = self._download_xml(
  97. 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
  98. video_id, query={'nTitleNo': video_id})
  99. video_element = video_xml.findall(compat_xpath('./track/video'))[1]
  100. if video_element is None or video_element.text is None:
  101. raise ExtractorError('Specified AfreecaTV video does not exist',
  102. expected=True)
  103. video_url_raw = video_element.text
  104. app, playpath = video_url_raw.split('mp4:')
  105. title = xpath_text(video_xml, './track/title', 'title', fatal=True)
  106. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  107. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  108. duration = int_or_none(xpath_text(video_xml, './track/duration',
  109. 'duration'))
  110. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  111. return {
  112. 'id': video_id,
  113. 'url': app,
  114. 'ext': 'flv',
  115. 'play_path': 'mp4:' + playpath,
  116. 'rtmp_live': True, # downloading won't end without this
  117. 'title': title,
  118. 'uploader': uploader,
  119. 'uploader_id': uploader_id,
  120. 'duration': duration,
  121. 'thumbnail': thumbnail,
  122. }
  123. class AfreecaTVGlobalIE(AfreecaTVIE):
  124. IE_NAME = 'afreecatv:global'
  125. _VALID_URL = r'https?://(?:www\.)?afreeca\.tv/(?P<channel_id>\d+)(?:/v/(?P<video_id>\d+))?'
  126. _TESTS = [{
  127. 'url': 'http://afreeca.tv/36853014/v/58301',
  128. 'info_dict': {
  129. 'id': '58301',
  130. 'title': 'tryhard top100',
  131. 'uploader_id': '36853014',
  132. 'uploader': 'makgi Hearthstone Live!',
  133. },
  134. 'playlist_count': 3,
  135. }]
  136. def _real_extract(self, url):
  137. channel_id, video_id = re.match(self._VALID_URL, url).groups()
  138. video_type = 'video' if video_id else 'live'
  139. query = {
  140. 'pt': 'view',
  141. 'bid': channel_id,
  142. }
  143. if video_id:
  144. query['vno'] = video_id
  145. video_data = self._download_json(
  146. 'http://api.afreeca.tv/%s/view_%s.php' % (video_type, video_type),
  147. video_id or channel_id, query=query)['channel']
  148. if video_data.get('result') != 1:
  149. raise ExtractorError('%s said: %s' % (self.IE_NAME, video_data['remsg']))
  150. title = video_data['title']
  151. info = {
  152. 'thumbnail': video_data.get('thumb'),
  153. 'view_count': int_or_none(video_data.get('vcnt')),
  154. 'age_limit': int_or_none(video_data.get('grade')),
  155. 'uploader_id': channel_id,
  156. 'uploader': video_data.get('cname'),
  157. }
  158. if video_id:
  159. entries = []
  160. for i, f in enumerate(video_data.get('flist', [])):
  161. video_key = self.parse_video_key(f.get('key', ''))
  162. f_url = f.get('file')
  163. if not video_key or not f_url:
  164. continue
  165. entries.append({
  166. 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
  167. 'title': title,
  168. 'upload_date': video_key.get('upload_date'),
  169. 'duration': int_or_none(f.get('length')),
  170. 'url': f_url,
  171. 'protocol': 'm3u8_native',
  172. 'ext': 'mp4',
  173. })
  174. info.update({
  175. 'id': video_id,
  176. 'title': title,
  177. 'duration': int_or_none(video_data.get('length')),
  178. })
  179. if len(entries) > 1:
  180. info['_type'] = 'multi_video'
  181. info['entries'] = entries
  182. elif len(entries) == 1:
  183. i = entries[0].copy()
  184. i.update(info)
  185. info = i
  186. else:
  187. formats = []
  188. for s in video_data.get('strm', []):
  189. s_url = s.get('purl')
  190. if not s_url:
  191. continue
  192. stype = s.get('stype')
  193. if stype == 'HLS':
  194. formats.extend(self._extract_m3u8_formats(
  195. s_url, channel_id, 'mp4', m3u8_id=stype, fatal=False))
  196. elif stype == 'RTMP':
  197. format_id = [stype]
  198. label = s.get('label')
  199. if label:
  200. format_id.append(label)
  201. formats.append({
  202. 'format_id': '-'.join(format_id),
  203. 'url': s_url,
  204. 'tbr': int_or_none(s.get('bps')),
  205. 'height': int_or_none(s.get('brt')),
  206. 'ext': 'flv',
  207. 'rtmp_live': True,
  208. })
  209. self._sort_formats(formats)
  210. info.update({
  211. 'id': channel_id,
  212. 'title': self._live_title(title),
  213. 'is_live': True,
  214. 'formats': formats,
  215. })
  216. return info