search_model.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from sqlite3 import connect
  2. from config.settings import OUTPUT_DIR, WORK_DIR
  3. from datetime import datetime
  4. from sqlmodel import Field, SQLModel, create_engine, Session, select
  5. from sqlalchemy import text # Add this import
  6. from database.sqlite_engine import engine, drop_table
  7. class SearchResult(SQLModel, table=True):
  8. id: int = Field(default=None, primary_key=True)
  9. keyword: str = Field()
  10. start: int = Field()
  11. url: str = Field()
  12. html_path: str = Field()
  13. is_last_page: bool = Field(default=False) # 新增字段,标记是否是最后一页
  14. created_at: datetime = Field(default_factory=datetime.now)
  15. class SearchDatabaseManager:
  16. def __init__(self):
  17. self.engine = engine
  18. def get_existing_result(self, keyword: str, start: int) -> SearchResult | None:
  19. """获取已存在的搜索结果"""
  20. with Session(self.engine) as session:
  21. return session.exec(
  22. select(SearchResult)
  23. .where(SearchResult.keyword == keyword)
  24. .where(SearchResult.start == start)
  25. ).first()
  26. def save_search_result(self, keyword: str, start: int, url: str, html_path: str, is_last_page: bool = False) -> SearchResult:
  27. """保存搜索结果到数据库"""
  28. # 检查是否已存在
  29. existing = self.get_existing_result(keyword, start)
  30. if existing:
  31. return existing
  32. with Session(self.engine) as session:
  33. result = SearchResult(
  34. keyword=keyword,
  35. start=start,
  36. url=url,
  37. html_path=html_path,
  38. is_last_page=is_last_page
  39. )
  40. session.add(result)
  41. session.commit()
  42. session.refresh(result)
  43. return result
  44. def get_search_results(self, keyword: str):
  45. """获取指定关键词的所有搜索结果"""
  46. with Session(self.engine) as session:
  47. results = session.exec(
  48. select(SearchResult)
  49. .where(SearchResult.keyword == keyword)
  50. .order_by(SearchResult.start)
  51. ).all()
  52. return results
  53. def main():
  54. # 先删除SearchResult表
  55. drop_table(SearchResult)
  56. # print(SearchResult.__tablename__)
  57. # 再创建新表
  58. # create_db_and_tables()
  59. if __name__ == "__main__":
  60. main()