db.py 921 B

12345678910111213141516171819202122232425262728293031323334
  1. import os,sys,re,json
  2. # import mysql
  3. class Db():
  4. '''Db接口'''
  5. def __init__(self):
  6. pass
  7. def save(domain:str, res:bool):
  8. pass
  9. class File(Db):
  10. '''文件保存结果'''
  11. def save(self, filePath: str, res: str):
  12. # super().save(res)
  13. # 确保目录存在
  14. dir_path = os.path.dirname(filePath)
  15. if dir_path and not os.path.exists(dir_path):
  16. os.makedirs(dir_path, exist_ok=True)
  17. # 使用追加模式写入,如果文件不存在会自动创建
  18. with open(filePath, 'a+', encoding='utf-8') as file:
  19. file.write(res + "\n")
  20. file.flush() # 确保立即写入磁盘
  21. class Mysql(Db):
  22. '''mysql数据库保存数据库'''
  23. def save(domain: str, res: bool):
  24. return super().save(res)
  25. class Sqlite(Db):
  26. '''sqlite保存'''
  27. def save(domain: str, res: bool):
  28. return super().save(res)