shared.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import toml, redis
  16. from aioflask import request, render_template, Flask
  17. from flask_caching import Cache
  18. from bilibili_api import Credential
  19. from refresher import renew_cookies
  20. try:
  21. appconf = toml.load('config.toml')
  22. except FileNotFoundError:
  23. print('Configuration file not found, maybe you forgot to copy `config.toml.sample\' to `config.toml\'?')
  24. # Connect to our nice redis database.
  25. appredis = redis.Redis()
  26. # Initialize the flask app.
  27. app = Flask('app')
  28. app.config.from_mapping(appconf['flask'])
  29. # And also configure the flask_cache module.
  30. appcache = Cache(app, config={'CACHE_TYPE': 'RedisCache'})
  31. # Initilize credentials for bilibili API.
  32. if appconf['credential']['use_cred']:
  33. credstore = appconf['updatedcred'] if 'updatedcred' in appconf else \
  34. appconf['credential']
  35. appcred = Credential(sessdata=credstore['sessdata'],
  36. bili_jct=credstore['bili_jct'],
  37. buvid3=credstore['buvid3'],
  38. dedeuserid=credstore['dedeuserid'],
  39. ac_time_value=credstore['ac_time_value'])
  40. if renew_cookies(appcred):
  41. appconf = toml.load('config.toml')
  42. credstore = appconf['updatedcred']
  43. appcred = Credential(sessdata=credstore['sessdata'],
  44. bili_jct=credstore['bili_jct'],
  45. buvid3=credstore['buvid3'],
  46. dedeuserid=credstore['dedeuserid'],
  47. ac_time_value=credstore['ac_time_value'])
  48. else:
  49. appcred = None
  50. ##########################################
  51. # Util functions
  52. ##########################################
  53. def detect_theme():
  54. """Determine the theme of the users' request."""
  55. if theme := request.args.get('theme'):
  56. return theme
  57. elif theme := request.cookies.get('theme'):
  58. return theme
  59. else:
  60. return 'default'
  61. def render_template_with_theme(fp, **kwargs):
  62. """Render a template with theming support."""
  63. t = detect_theme()
  64. if dark_theme := request.cookies.get('dark-theme'):
  65. dark_theme = int(dark_theme)
  66. else:
  67. dark_theme = False
  68. if t == 'default':
  69. t = appconf['display']['default_theme']
  70. return render_template(f'themes/{t}/{fp}', dark_mode=dark_theme,
  71. **appconf['site'],
  72. **kwargs)