filters.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. from shared import app, appconf
  16. from datetime import datetime, timedelta
  17. # Convert a timestamp to a humand readable date format.
  18. @app.template_filter('date')
  19. def _jinja2_filter_datetime(ts, fmt='%Y年%m月%d日 %H点%m分'):
  20. return datetime.fromtimestamp(ts).strftime(fmt)
  21. # Convert a integer to the one with separator like 1,000,000.
  22. @app.template_filter('intsep')
  23. def _jinja2_filter_intsep(i):
  24. return f'{int(i):,}'
  25. # Convert a duration in seconds to human readable duration.
  26. @app.template_filter('secdur')
  27. def __jinja2_filter_secdur(delta_t):
  28. return str(timedelta(seconds=int(delta_t)))
  29. # Convert a url of a photo asset to MikuInvidious proxy url.
  30. @app.template_filter('pic')
  31. def __jinja2_filter_pic(url):
  32. if appconf['proxy']['image']:
  33. return '/proxy/pic/' + url.split('//')[1]
  34. else:
  35. return url.replace('http://', 'https://')