afreecatv.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. _TEST = {
  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. }
  33. }
  34. @staticmethod
  35. def parse_video_key(key):
  36. video_key = {'upload_date': None, 'part': '0'}
  37. m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
  38. if m:
  39. video_key['upload_date'] = m.group('upload_date')
  40. video_key['part'] = m.group('part')
  41. return video_key
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. parsed_url = compat_urllib_parse_urlparse(url)
  45. info_url = compat_urlparse.urlunparse(parsed_url._replace(
  46. netloc='afbbs.afreecatv.com:8080',
  47. path='/api/video/get_video_info.php'))
  48. video_xml = self._download_xml(info_url, video_id)
  49. if xpath_text(video_xml, './track/flag', default='FAIL') != 'SUCCEED':
  50. raise ExtractorError('Specified AfreecaTV video does not exist',
  51. expected=True)
  52. title = xpath_text(video_xml, './track/title', 'title')
  53. uploader = xpath_text(video_xml, './track/nickname', 'uploader')
  54. uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
  55. duration = int_or_none(xpath_text(video_xml, './track/duration',
  56. 'duration'))
  57. thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
  58. entries = []
  59. for video_file in video_xml.findall('./track/video/file'):
  60. video_key = self.parse_video_key(video_file.get('key'))
  61. entries.append({
  62. 'id': '%s_%s' % (video_id, video_key['part']),
  63. 'title': title,
  64. 'upload_date': video_key['upload_date'],
  65. 'duration': int_or_none(video_file.get('duration')),
  66. 'url': video_file.text,
  67. })
  68. info = {
  69. 'id': video_id,
  70. 'title': title,
  71. 'uploader': uploader,
  72. 'uploader_id': uploader_id,
  73. 'duration': duration,
  74. 'thumbnail': thumbnail,
  75. }
  76. if len(entries) > 1:
  77. info['_type'] = 'multi_video'
  78. info['entries'] = entries
  79. elif len(entries) == 1:
  80. info['url'] = entries[0]['url']
  81. info['upload_date'] = entries[0]['upload_date']
  82. else:
  83. raise ExtractorError(
  84. 'No files found for the specified AfreecaTV video, either'
  85. ' the URL is incorrect or the video has been made private.',
  86. expected=True)
  87. return info