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.

237 lines
8.5 KiB

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