afreecatv.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. xpath_text,
  13. )
  14. class AfreecaTVIE(InfoExtractor):
  15. IE_DESC = 'afreecatv.com'
  16. _VALID_URL = r'''(?x)^
  17. https?://(?:(live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
  18. (?:
  19. /app/(?:index|read_ucc_bbs)\.cgi|
  20. /player/[Pp]layer\.(?:swf|html))
  21. \?.*?\bnTitleNo=(?P<id>\d+)'''
  22. _TESTS = [{
  23. 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
  24. 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
  25. 'info_dict': {
  26. 'id': '36164052',
  27. 'ext': 'mp4',
  28. 'title': '데일리 에이프릴 요정들의 시상식!',
  29. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  30. 'uploader': 'dailyapril',
  31. 'uploader_id': 'dailyapril',
  32. 'upload_date': '20160503',
  33. }
  34. }, {
  35. 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
  36. 'info_dict': {
  37. 'id': '36153164',
  38. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  39. 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
  40. 'uploader': 'dailyapril',
  41. 'uploader_id': 'dailyapril',
  42. },
  43. 'playlist_count': 2,
  44. 'playlist': [{
  45. 'md5': 'd8b7c174568da61d774ef0203159bf97',
  46. 'info_dict': {
  47. 'id': '36153164_1',
  48. 'ext': 'mp4',
  49. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  50. 'upload_date': '20160502',
  51. },
  52. }, {
  53. 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
  54. 'info_dict': {
  55. 'id': '36153164_2',
  56. 'ext': 'mp4',
  57. 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
  58. 'upload_date': '20160502',
  59. },
  60. }],
  61. }, {
  62. 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
  63. 'only_matching': True,
  64. }]
  65. @staticmethod
  66. def parse_video_key(key):
  67. video_key = {}
  68. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  69. if m:
  70. video_key['upload_date'] = m.group('upload_date')
  71. video_key['part'] = m.group('part')
  72. return video_key
  73. def _real_extract(self, url):
  74. video_id = self._match_id(url)
  75. parsed_url = compat_urllib_parse_urlparse(url)
  76. info_url = compat_urlparse.urlunparse(parsed_url._replace(
  77. netloc='afbbs.afreecatv.com:8080',
  78. path='/api/video/get_video_info.php'))
  79. video_xml = self._download_xml(info_url, video_id)
  80. if xpath_text(video_xml, './track/flag', default='FAIL') != 'SUCCEED':
  81. raise ExtractorError('Specified AfreecaTV video does not exist',
  82. expected=True)
  83. title = xpath_text(video_xml, './track/title', 'title')
  84. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  85. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  86. duration = int_or_none(xpath_text(video_xml, './track/duration',
  87. 'duration'))
  88. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  89. entries = []
  90. for i, video_file in enumerate(video_xml.findall('./track/video/file')):
  91. video_key = self.parse_video_key(video_file.get('key', ''))
  92. if not video_key:
  93. continue
  94. entries.append({
  95. 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
  96. 'title': title,
  97. 'upload_date': video_key.get('upload_date'),
  98. 'duration': int_or_none(video_file.get('duration')),
  99. 'url': video_file.text,
  100. })
  101. info = {
  102. 'id': video_id,
  103. 'title': title,
  104. 'uploader': uploader,
  105. 'uploader_id': uploader_id,
  106. 'duration': duration,
  107. 'thumbnail': thumbnail,
  108. }
  109. if len(entries) > 1:
  110. info['_type'] = 'multi_video'
  111. info['entries'] = entries
  112. elif len(entries) == 1:
  113. info['url'] = entries[0]['url']
  114. info['upload_date'] = entries[0].get('upload_date')
  115. else:
  116. raise ExtractorError(
  117. 'No files found for the specified AfreecaTV video, either'
  118. ' the URL is incorrect or the video has been made private.',
  119. expected=True)
  120. return info