Showing
4 changed files
with
13 additions
and
8 deletions
| @@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
| 7 | """ | 7 | """ |
| 8 | 8 | ||
| 9 | from __future__ import annotations | 9 | from __future__ import annotations |
| 10 | - | 10 | +from urllib.parse import quote_plus |
| 11 | import asyncio | 11 | import asyncio |
| 12 | import os | 12 | import os |
| 13 | from typing import Any, Dict, Iterable, List, Optional, Union | 13 | from typing import Any, Dict, Iterable, List, Optional, Union |
| @@ -36,6 +36,8 @@ def _build_database_url() -> str: | @@ -36,6 +36,8 @@ def _build_database_url() -> str: | ||
| 36 | if os.getenv("DATABASE_URL"): | 36 | if os.getenv("DATABASE_URL"): |
| 37 | return os.getenv("DATABASE_URL") # 直接使用外部提供的完整URL | 37 | return os.getenv("DATABASE_URL") # 直接使用外部提供的完整URL |
| 38 | 38 | ||
| 39 | + password = quote_plus(password) | ||
| 40 | + | ||
| 39 | if dialect in ("postgresql", "postgres"): | 41 | if dialect in ("postgresql", "postgres"): |
| 40 | # PostgreSQL 使用 asyncpg 驱动 | 42 | # PostgreSQL 使用 asyncpg 驱动 |
| 41 | return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{db_name}" | 43 | return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{db_name}" |
| @@ -18,6 +18,7 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine | @@ -18,6 +18,7 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine | ||
| 18 | from sqlalchemy import inspect, text | 18 | from sqlalchemy import inspect, text |
| 19 | from config import settings | 19 | from config import settings |
| 20 | from loguru import logger | 20 | from loguru import logger |
| 21 | +from urllib.parse import quote_plus | ||
| 21 | 22 | ||
| 22 | # 添加项目根目录到路径 | 23 | # 添加项目根目录到路径 |
| 23 | project_root = Path(__file__).parent | 24 | project_root = Path(__file__).parent |
| @@ -73,10 +74,10 @@ class MindSpider: | @@ -73,10 +74,10 @@ class MindSpider: | ||
| 73 | def build_async_url() -> str: | 74 | def build_async_url() -> str: |
| 74 | dialect = (settings.DB_DIALECT or "mysql").lower() | 75 | dialect = (settings.DB_DIALECT or "mysql").lower() |
| 75 | if dialect == "postgresql": | 76 | if dialect == "postgresql": |
| 76 | - return f"postgresql+asyncpg://{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" | 77 | + return f"postgresql+asyncpg://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" |
| 77 | # 默认使用 mysql 异步驱动 asyncmy | 78 | # 默认使用 mysql 异步驱动 asyncmy |
| 78 | return ( | 79 | return ( |
| 79 | - f"mysql+asyncmy://{settings.DB_USER}:{settings.DB_PASSWORD}" | 80 | + f"mysql+asyncmy://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}" |
| 80 | f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" | 81 | f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" |
| 81 | ) | 82 | ) |
| 82 | 83 | ||
| @@ -104,9 +105,9 @@ class MindSpider: | @@ -104,9 +105,9 @@ class MindSpider: | ||
| 104 | def build_async_url() -> str: | 105 | def build_async_url() -> str: |
| 105 | dialect = (settings.DB_DIALECT or "mysql").lower() | 106 | dialect = (settings.DB_DIALECT or "mysql").lower() |
| 106 | if dialect == "postgresql": | 107 | if dialect == "postgresql": |
| 107 | - return f"postgresql+asyncpg://{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" | 108 | + return f"postgresql+asyncpg://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" |
| 108 | return ( | 109 | return ( |
| 109 | - f"mysql+asyncmy://{settings.DB_USER}:{settings.DB_PASSWORD}" | 110 | + f"mysql+asyncmy://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}" |
| 110 | f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" | 111 | f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" |
| 111 | ) | 112 | ) |
| 112 | 113 |
| @@ -13,6 +13,7 @@ import argparse | @@ -13,6 +13,7 @@ import argparse | ||
| 13 | from pathlib import Path | 13 | from pathlib import Path |
| 14 | from datetime import datetime, timedelta | 14 | from datetime import datetime, timedelta |
| 15 | from loguru import logger | 15 | from loguru import logger |
| 16 | +from urllib.parse import quote_plus | ||
| 16 | 17 | ||
| 17 | # 添加项目根目录到路径 | 18 | # 添加项目根目录到路径 |
| 18 | project_root = Path(__file__).parent.parent | 19 | project_root = Path(__file__).parent.parent |
| @@ -36,9 +37,9 @@ class DatabaseManager: | @@ -36,9 +37,9 @@ class DatabaseManager: | ||
| 36 | try: | 37 | try: |
| 37 | dialect = (settings.DB_DIALECT or "mysql").lower() | 38 | dialect = (settings.DB_DIALECT or "mysql").lower() |
| 38 | if dialect in ("postgresql", "postgres"): | 39 | if dialect in ("postgresql", "postgres"): |
| 39 | - url = f"postgresql+psycopg://{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" | 40 | + url = f"postgresql+psycopg://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}" |
| 40 | else: | 41 | else: |
| 41 | - url = f"mysql+pymysql://{settings.DB_USER}:{settings.DB_PASSWORD}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" | 42 | + url = f"mysql+pymysql://{settings.DB_USER}:{quote_plus(settings.DB_PASSWORD)}@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}?charset={settings.DB_CHARSET}" |
| 42 | self.engine = create_engine(url, future=True) | 43 | self.engine = create_engine(url, future=True) |
| 43 | logger.info(f"成功连接到数据库: {settings.DB_NAME}") | 44 | logger.info(f"成功连接到数据库: {settings.DB_NAME}") |
| 44 | except Exception as e: | 45 | except Exception as e: |
| @@ -13,7 +13,7 @@ from __future__ import annotations | @@ -13,7 +13,7 @@ from __future__ import annotations | ||
| 13 | import asyncio | 13 | import asyncio |
| 14 | import os | 14 | import os |
| 15 | from typing import Optional | 15 | from typing import Optional |
| 16 | - | 16 | +from urllib.parse import quote_plus |
| 17 | from loguru import logger | 17 | from loguru import logger |
| 18 | 18 | ||
| 19 | from sqlalchemy.ext.asyncio import create_async_engine | 19 | from sqlalchemy.ext.asyncio import create_async_engine |
| @@ -49,6 +49,7 @@ def _build_database_url() -> str: | @@ -49,6 +49,7 @@ def _build_database_url() -> str: | ||
| 49 | port = str(settings.DB_PORT or ("3306" if dialect == "mysql" else "5432")) | 49 | port = str(settings.DB_PORT or ("3306" if dialect == "mysql" else "5432")) |
| 50 | user = settings.DB_USER or "root" | 50 | user = settings.DB_USER or "root" |
| 51 | password = settings.DB_PASSWORD or "" | 51 | password = settings.DB_PASSWORD or "" |
| 52 | + password = quote_plus(password) | ||
| 52 | db_name = settings.DB_NAME or "mindspider" | 53 | db_name = settings.DB_NAME or "mindspider" |
| 53 | 54 | ||
| 54 | if dialect in ("postgresql", "postgres"): | 55 | if dialect in ("postgresql", "postgres"): |
-
Please register or login to post a comment