danmaku.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2023 MikuInvidious Team
  2. #
  3. # MikuInvidious is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License as
  5. # published by the Free Software Foundation; either version 3 of
  6. # the License, or (at your option) any later version.
  7. #
  8. # MikuInvidious is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MikuInvidious. If not, see <http://www.gnu.org/licenses/>.
  15. '''Server-Side danmaku translation'''
  16. from xml.dom import minidom
  17. def danmaku_xml_conv(domtree):
  18. return list(map(danmaku_elem_conv, domtree.getElementsByTagName('d')))
  19. def danmaku_elem_conv(d):
  20. p = d.getAttribute('p').split(',')
  21. try:
  22. m = ({ '6': 'ltr', '1': 'rtl', '5': 'top', '4': 'bottom' })[p[1]]
  23. except:
  24. return {}
  25. ftsize = int(p[2]) or 25
  26. ftcolor = hex(int(p[3]))[2:]
  27. return {
  28. 'text': d.firstChild.data,
  29. 'mode': m,
  30. 'time': float(p[0]),
  31. 'style': {
  32. 'fontSize': f'{ftsize}px',
  33. 'color': f'#{ftcolor}',
  34. 'textShadow': '-1px -1px #fff, -1px 1px #fff, 1px -1px #fff, 1px 1px #fff' \
  35. if ftcolor == '000000' else '-1px -1px #000, -1px 1px #000, 1px -1px #000, 1px 1px #000',
  36. 'font': f'{ftsize}px sans-serif',
  37. 'fillStyle': f'#{ftcolor}',
  38. 'strokeStyle': '#fff' if ftcolor == '000000' else '#000',
  39. 'lineWidth': 2.0,
  40. }
  41. }