afreecatv.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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?://videoimg.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?://videoimg.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. @staticmethod
  63. def parse_video_key(key):
  64. video_key = {}
  65. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  66. if m:
  67. video_key['upload_date'] = m.group('upload_date')
  68. video_key['part'] = m.group('part')
  69. return video_key
  70. def _real_extract(self, url):
  71. video_id = self._match_id(url)
  72. parsed_url = compat_urllib_parse_urlparse(url)
  73. info_url = compat_urlparse.urlunparse(parsed_url._replace(
  74. netloc='afbbs.afreecatv.com:8080',
  75. path='/api/video/get_video_info.php'))
  76. video_xml = self._download_xml(info_url, video_id)
  77. if xpath_text(video_xml, './track/flag', default='FAIL') != 'SUCCEED':
  78. raise ExtractorError('Specified AfreecaTV video does not exist',
  79. expected=True)
  80. title = xpath_text(video_xml, './track/title', 'title')
  81. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  82. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  83. duration = int_or_none(xpath_text(video_xml, './track/duration',
  84. 'duration'))
  85. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  86. entries = []
  87. for i, video_file in enumerate(video_xml.findall('./track/video/file')):
  88. video_key = self.parse_video_key(video_file.get('key'))
  89. entries.append({
  90. 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)),
  91. 'title': title,
  92. 'upload_date': video_key.get('upload_date'),
  93. 'duration': int_or_none(video_file.get('duration')),
  94. 'url': video_file.text,
  95. })
  96. info = {
  97. 'id': video_id,
  98. 'title': title,
  99. 'uploader': uploader,
  100. 'uploader_id': uploader_id,
  101. 'duration': duration,
  102. 'thumbnail': thumbnail,
  103. }
  104. if len(entries) > 1:
  105. info['_type'] = 'multi_video'
  106. info['entries'] = entries
  107. elif len(entries) == 1:
  108. info['url'] = entries[0]['url']
  109. info['upload_date'] = entries[0]['upload_date']
  110. else:
  111. raise ExtractorError(
  112. 'No files found for the specified AfreecaTV video, either'
  113. ' the URL is incorrect or the video has been made private.',
  114. expected=True)
  115. return info