update
Showing
6 changed files
with
18 additions
and
34 deletions
| ... | @@ -6,8 +6,9 @@ import logging | ... | @@ -6,8 +6,9 @@ import logging |
| 6 | 6 | ||
| 7 | logger = logging.getLogger(__name__) | 7 | logger = logging.getLogger(__name__) |
| 8 | 8 | ||
| 9 | |||
| 9 | @functools.lru_cache() | 10 | @functools.lru_cache() |
| 10 | def getTortoiseConfig()->TortoiseConfig: | 11 | def getTortoiseConfig() -> TortoiseConfig: |
| 11 | logger.info('开始加载TortoiseConfig') | 12 | logger.info('开始加载TortoiseConfig') |
| 12 | # 获取当前文件的绝对路径 | 13 | # 获取当前文件的绝对路径 |
| 13 | current_file_path = os.path.abspath(__file__) | 14 | current_file_path = os.path.abspath(__file__) | ... | ... |
| 1 | import logging | ||
| 2 | from contextlib import asynccontextmanager | 1 | from contextlib import asynccontextmanager |
| 3 | from fastapi import FastAPI | 2 | |
| 3 | # noinspection PyPackageRequirements | ||
| 4 | from tortoise import Tortoise | 4 | from tortoise import Tortoise |
| 5 | from app.config.tortoise_config import getTortoiseConfig | ||
| 6 | from app.job.job import scheduler | 5 | from app.job.job import scheduler |
| 6 | from fastapi import FastAPI | ||
| 7 | from app.config.tortoise_config import getTortoiseConfig | ||
| 8 | import logging | ||
| 7 | 9 | ||
| 8 | logger = logging.getLogger(__name__) | 10 | logger = logging.getLogger(__name__) |
| 9 | 11 | ||
| ... | @@ -21,13 +23,14 @@ async def close(): | ... | @@ -21,13 +23,14 @@ async def close(): |
| 21 | await Tortoise.close_connections() | 23 | await Tortoise.close_connections() |
| 22 | 24 | ||
| 23 | 25 | ||
| 26 | # 使用asynccontextmanager装饰器定义一个异步上下文管理器函数lifespan | ||
| 27 | # noinspection PyUnusedLocal | ||
| 24 | @asynccontextmanager | 28 | @asynccontextmanager |
| 25 | async def lifespan(app: FastAPI): | 29 | async def lifespan(app: FastAPI): |
| 26 | # 开始apscheduler | 30 | # 开始apscheduler |
| 27 | scheduler.start() | 31 | scheduler.start() |
| 28 | logging.info("apscheduler启动完成") | 32 | logging.info("apscheduler启动完成") |
| 29 | await init() | 33 | await init() |
| 30 | logging.info("初始化数据库完成") | ||
| 31 | yield | 34 | yield |
| 32 | # 在异步上下文管理器中,"退出上下文"时清理机器学习模型,释放资源 | 35 | # 在异步上下文管理器中,"退出上下文"时清理机器学习模型,释放资源 |
| 33 | scheduler.shutdown() | 36 | scheduler.shutdown() | ... | ... |
| ... | @@ -132,24 +132,4 @@ class ResponseModel(BaseModel): | ... | @@ -132,24 +132,4 @@ class ResponseModel(BaseModel): |
| 132 | extra: Optional[dict] = None | 132 | extra: Optional[dict] = None |
| 133 | 133 | ||
| 134 | 134 | ||
| 135 | if __name__ == "__main__": | ||
| 136 | from datetime import datetime | ||
| 137 | 135 | ||
| 138 | # 时间戳(以毫秒为单位) | ||
| 139 | release_date_timestamp = 1573488000000 | ||
| 140 | offline_date_timestamp = 1576080000000 | ||
| 141 | |||
| 142 | |||
| 143 | # 将时间戳转换为datetime对象,并转换为'yyyy-MM-dd'格式 | ||
| 144 | def timestamp_to_date_string(timestamp_ms): | ||
| 145 | # Convert from milliseconds to seconds by dividing by 1000, then use fromtimestamp | ||
| 146 | dt_object = datetime.fromtimestamp(timestamp_ms / 1000) | ||
| 147 | return dt_object.strftime('%Y-%m-%d') | ||
| 148 | |||
| 149 | |||
| 150 | # 调用函数并打印结果 | ||
| 151 | formatted_release_date = timestamp_to_date_string(release_date_timestamp) | ||
| 152 | formatted_offline_date = timestamp_to_date_string(offline_date_timestamp) | ||
| 153 | |||
| 154 | print(f"Release Date: {formatted_release_date}") | ||
| 155 | print(f"Offline Date: {formatted_offline_date}") | ... | ... |
| 1 | import asyncio | 1 | import asyncio |
| 2 | import logging | 2 | import logging |
| 3 | import time | ||
| 4 | from datetime import datetime | 3 | from datetime import datetime |
| 5 | from typing import List, Any | 4 | from typing import List, Any |
| 5 | |||
| 6 | from app.job.job import scheduler | 6 | from app.job.job import scheduler |
| 7 | from app.model.mysql_model import SpiderModel | 7 | from app.model.mysql_model import SpiderModel |
| 8 | from app.schemas.safe_contrainer import SafeDict | 8 | from app.schemas.safe_contrainer import SafeDict |
| ... | @@ -10,8 +10,7 @@ from app.schemas.spider_schema import ( | ... | @@ -10,8 +10,7 @@ from app.schemas.spider_schema import ( |
| 10 | ApschedulerJob, | 10 | ApschedulerJob, |
| 11 | TaskInfo, | 11 | TaskInfo, |
| 12 | SpiderParams, | 12 | SpiderParams, |
| 13 | GuoDuoSpiderResult, TypeEnum, | 13 | GuoDuoSpiderResult, ) |
| 14 | ) | ||
| 15 | from app.spider.http_spider import get_score_data | 14 | from app.spider.http_spider import get_score_data |
| 16 | 15 | ||
| 17 | logger = logging.getLogger(__name__) | 16 | logger = logging.getLogger(__name__) | ... | ... |
| 1 | import asyncio | ||
| 2 | import datetime | 1 | import datetime |
| 3 | import time | 2 | import logging |
| 4 | from typing import List, Dict, Tuple | 3 | from typing import List, Dict, Tuple |
| 4 | |||
| 5 | import aiohttp | 5 | import aiohttp |
| 6 | from superstream import Stream | ||
| 6 | from tenacity import retry, stop_after_attempt, before_sleep_log, wait_exponential, after_log | 7 | from tenacity import retry, stop_after_attempt, before_sleep_log, wait_exponential, after_log |
| 7 | import logging | 8 | from tqdm.asyncio import tqdm_asyncio |
| 9 | |||
| 10 | from app.config.app_config import getAppConfig | ||
| 8 | from app.schemas.config_schema import UrlTemplateInfo | 11 | from app.schemas.config_schema import UrlTemplateInfo |
| 9 | from app.schemas.spider_schema import SpiderParams, TypeEnum, GuoDuoSpiderResult, ResponseModel | 12 | from app.schemas.spider_schema import SpiderParams, TypeEnum, GuoDuoSpiderResult, ResponseModel |
| 10 | from app.config.app_config import getAppConfig | ||
| 11 | from superstream import Stream | ||
| 12 | from tqdm.asyncio import tqdm_asyncio | ||
| 13 | 13 | ||
| 14 | logger = logging.getLogger(__name__) | 14 | logger = logging.getLogger(__name__) |
| 15 | 15 | ... | ... |
-
Please register or sign in to post a comment