home_router.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import json
  5. from fastapi import APIRouter, Body
  6. from pathlib import Path
  7. from module.config.utils import write_file
  8. from module.logger import logger
  9. from module.server.main_manager import MainManager
  10. from module.server.updater import Updater
  11. from module.server.i18n import I18n
  12. home_app = APIRouter(
  13. prefix="/home",
  14. tags=["home"],
  15. )
  16. @home_app.get('/test')
  17. async def home_test():
  18. return {'message': 'test'}
  19. # gcc -Wall -pedantic -shared -fPIC -o group_work.so group_work.c -lwiringPi
  20. @home_app.get('/home_menu')
  21. async def home_menu():
  22. return {'Home': [], 'Updater': [], 'Tool': []}
  23. @home_app.post('/notify_test')
  24. async def notify_test(setting: str, title: str, content: str):
  25. from module.notify.notify import Notifier
  26. try:
  27. notifier = Notifier(setting, True)
  28. if notifier.push(title=title, content=content):
  29. del notifier
  30. return True
  31. else:
  32. del notifier
  33. return False
  34. except Exception as e:
  35. logger.exception(e)
  36. return str(e)
  37. @home_app.get('/kill_server')
  38. async def kill_server():
  39. MainManager.signal_kill_server = True
  40. return 'success'
  41. @home_app.get('/update_info')
  42. async def update_info():
  43. try:
  44. updater = Updater()
  45. result = {'is_update': updater.check_update(),
  46. 'branch': updater.current_branch(),
  47. 'current_commit': updater.current_commit(),
  48. 'latest_commit': updater.latest_commit(),
  49. 'commit': updater.get_commit(n=15),
  50. }
  51. return result
  52. except Exception as e:
  53. logger.error(e)
  54. return None
  55. @home_app.get('/execute_update')
  56. async def execute_update():
  57. # 下拉仓库 -> 关闭所有脚本进程 -> 最后重启oasx
  58. try:
  59. updater = Updater()
  60. updater.execute_pull()
  61. except Exception as e:
  62. logger.error(e)
  63. return '手动更新将会立即结束运行中的脚本服务, 最后你还需重启oasx'
  64. @home_app.put('/chinese_translate')
  65. async def chinese_translate(data: dict = Body(...)):
  66. try:
  67. I18n.save_zh_cn(data)
  68. except Exception as e:
  69. logger.error(e)
  70. return True