diff --git a/.gitignore b/.gitignore index e393ac5..de011f5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ __pycache__/ /config.json # Ignore the virtual environment directory -venv/ +.venv/ # Ignore the build directory build/ @@ -20,3 +20,5 @@ dist/ # Ignore any .DS_Store files created by macOS .DS_Store + +fastdoi.sqlite3 \ No newline at end of file diff --git a/config.json.sample b/config.json.sample index d1e7332..ae88886 100644 --- a/config.json.sample +++ b/config.json.sample @@ -2,9 +2,6 @@ "fastgpt_host": "", "fastgpt_api": "", "fastgpt_setId": "", - "mysql_host": "", - "mysql_user": "", - "mysql_password": "", "semAPI": "semanticscholar API", "miniflux_host": "", "miniflux_api": "" diff --git a/main.py b/main.py index a8959c4..4f79ecb 100644 --- a/main.py +++ b/main.py @@ -59,4 +59,4 @@ async def load_rss(): if __name__ == '__main__': import uvicorn uvicorn.run(app, host="127.0.0.1", port=8964) - mysql_connector.end_mysql() \ No newline at end of file + mysql_connector.end_sqlalchemy() \ No newline at end of file diff --git a/mysql_connector.py b/mysql_connector.py index 0980995..a4ace80 100644 --- a/mysql_connector.py +++ b/mysql_connector.py @@ -2,35 +2,33 @@ from config import __CONFIG__ -import mysql.connector +from sqlalchemy import create_engine, Column, String, Integer +from sqlalchemy.orm import sessionmaker, declarative_base +import os -mysql_config = { - 'user': __CONFIG__['mysql_user'], - 'password': __CONFIG__['mysql_password'], - 'host': __CONFIG__['mysql_host'], # Typically 'localhost' if the database is on the same machine - 'database': 'fastdoi', - 'raise_on_warnings': True, - 'auth_plugin':'mysql_native_password' -} +# SQLite database file path (in the current directory) +db_path = os.path.join(os.path.dirname(__file__), 'fastdoi.sqlite3') +engine = create_engine(f'sqlite:///{db_path}', echo=False) +Session = sessionmaker(bind=engine) +session = Session() +Base = declarative_base() -# Establish a connection to the MySQL database -cnx = mysql.connector.connect(**mysql_config) -cursor = cnx.cursor() +class UploadedPaper(Base): + __tablename__ = 'uploaded_papers' + id = Column(Integer, primary_key=True, autoincrement=True) + paperId = Column(String, unique=True, nullable=False) -def end_mysql(): - try: - # Close the cursor and connection - cursor.close() - cnx.close() - except: - print("No Mysql Opened.") +# Create the table if it doesn't exist +Base.metadata.create_all(engine) + +def end_sqlalchemy(): + session.close() + engine.dispose() def is_loaded(paperId): - query = ("SELECT COUNT(*) FROM uploaded_papers WHERE paperId = %s") - cursor.execute(query, (paperId,)) - return cursor.fetchone()[0] > 0 + return session.query(UploadedPaper).filter_by(paperId=paperId).count() > 0 def new_load(paperId): - query = ("INSERT INTO uploaded_papers (paperId) VALUES (%s)") - cursor.execute(query, (paperId,)) - cnx.commit() + paper = UploadedPaper(paperId=paperId) + session.add(paper) + session.commit() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5d6b8c6 Binary files /dev/null and b/requirements.txt differ