from dotenv import loaddotenv loaddotenv(verbose=True) import os import time import…
- User
from dotenv import load_dotenv load_dotenv(verbose=True)
import os import time import signal import shutil import json import traceback from datetime import datetime, timedelta
print('✨ 正在初始化 INF-SJTU ...')
from modules.core.spider_manage import spmanage from modules.core.news_manage import newsmanage
CONFIG
from config import *
MAIN
class Worker: def init(self) -> None:
# 根目录 self.root_path = os.path.dirname(os.path.abspath(__file__)) # 检测缓存目录 os.makedirs(f'{self.root_path}/cache', exist_ok=True) self.get_cache_path = lambda path: os.path.join(self.root_path, f'cache/{path}') self.get_data_path = lambda path: os.path.join(self.root_path, f'data/{path}') # 当前日期 self.current_date = datetime.now().strftime("%Y-%m-%d") # Modules # 爬虫 self.spmanage = spmanage(self.root_path, self.get_cache_path, self.current_date) # 新闻管理 self.newsmanage = newsmanage(self.root_path, self.get_cache_path, self.current_date) pass def run(self): print('🧐 等待执行中!') count = 0 while True: try: count += 1 print(f'✨ 第 {count} 次任务执行中...\n') self.check_news_updated() self.clear_old_news() print('\n🧐 任务执行完毕!正在等待下一轮任务...') time.sleep(EXECUTION_CYCLE) # 等待 1 小时 # 截获异常 except: print('\n🚨 INF-SJTU 运行异常!\n') traceback.print_exc() time.sleep(1) continue """ MAIN | 定时触发 """ def check_news_updated(self): """ 检测是否有新的新闻 """ print('🔖 正在检测是否有新的新闻...') # 检测缓存目录中是否有对应日期的新闻 # 解放日报 jfdaily_news_path = self.get_cache_path(f'news/{self.current_date}/jfdaily/news.json') if not os.path.exists(jfdaily_news_path): # 触发爬取 raw_news = self.spmanage.jfdaily() if raw_news: # 预处理新闻 self.newsmanage.jfdaily_news_convert(raw_news) # 新闻分类 self.newsmanage.news_classify('jfdaily') # 索引新闻 # self.newsmanage.news_index('jfdaily') # 生成问题 self.newsmanage.generate_qa('jfdaily') # 阅读分级 self.newsmanage.generate_reading_level('jfdaily') else: # 检测补全缺少的信息 print('🔖 检测补全缺少的信息...') with open(jfdaily_news_path, 'r', encoding='utf-8') as f: raw_news = json.loads(f.read()) for news in raw_news: # 检查 questions 是否存在 if 'questions' not in raw_news[news]: print(f'🚧 侦测到「解放日报」的 {news} 新闻缺少问题!正在补全...') # 生成问题 self.newsmanage.generate_qa('jfdaily', news) # 检查 reading_level 是否存在 if 'reading_level' not in raw_news[news]: print(f'🚧 侦测到「解放日报」的 {news} 新闻缺少阅读分级!正在补全...') # 阅读分级 self.newsmanage.generate_reading_level('jfdaily') # Nature Brief naturebrief_news_path = self.get_cache_path(f'news/{self.current_date}/naturebrief/news.json') if not os.path.exists(naturebrief_news_path): # 触发爬取 raw_news = self.spmanage.naturebrief() if raw_news: # 预处理新闻 self.newsmanage.naturebrief_news_convert(raw_news) # 新闻分类 self.newsmanage.news_classify('naturebrief') # 索引新闻 # self.newsmanage.news_index('naturebrief') # 生成问题 self.newsmanage.generate_qa('naturebrief') # 阅读分级 self.newsmanage.generate_reading_level('naturebrief') else: # 检测补全缺少的信息 with open(naturebrief_news_path, 'r', encoding='utf-8') as f: raw_news = json.loads(f.read()) for news in raw_news: # 检查 questions 是否存在 if 'questions' not in raw_news[news]: print(f'🚧 侦测到「Nature Brief」的 {news} 新闻缺少问题!正在补全...') # 生成问题 self.newsmanage.generate_qa('naturebrief') # 检查 reading_level 是否存在 if 'reading_level' not in raw_news[news]: print(f'🚧 侦测到「Nature Brief」的 {news} 新闻缺少阅读分级!正在补全...') # 阅读分级 self.newsmanage.generate_reading_level('naturebrief') print('🔖 检测完毕!') def clear_old_news(self): """ 清理缓存目录下旧的新闻 | 三天前 """ print('🔖 正在清理缓存目录下旧的新闻...') # 校验新闻目录 if not os.path.exists(self.get_cache_path('news')): os.makedirs(self.get_cache_path(f'news/{self.current_date}')) return # 获取缓存目录下的第一级目录名列表 cache_dir_list = os.listdir(self.get_cache_path('news')) # 获取近三天的日期列表 date_list = [] for i in range(3): date_list.append((datetime.now() - timedelta(days=i)).strftime("%Y-%m-%d")) # 清理 count = 0 for cache_dir in cache_dir_list: if cache_dir not in date_list: del_dir_path = self.get_cache_path(f'news/{cache_dir}') # 解放日报向量数据库映射 jfdaily_db_mapping_path = self.get_cache_path(f'news/{cache_dir}/jfdaily/db_mapping.json') # 删除对应的向量数据库 # 解析映射 json with open(jfdaily_db_mapping_path, 'r', encoding='utf-8') as f: db_mappings = json.loads(f.read()) # 删除对应的向量数据库 for db_mapping in db_mappings: shutil.rmtree(self.get_data_path(f'vectordb/{db_mapping["dbName"]}')) # # 递归删除缓存 shutil.rmtree(del_dir_path) count += 1 if count == 0: print('🔖 无旧新闻可清理!') else: print(f'🔖 清理完毕!共清理 {count} 天的旧新闻') - Assistant
您遇到了一个名为"dotenv"的模块缺失错误。这个模块通常用于从文件中加载环境变量。要解决这个问题,您可以尝试使用以下步骤之一:
-
确保您已经安装了"dotenv"模块。您可以在终端或命令行中运行以下命令进行安装:
pip install python-dotenv -
检查您的Python环境是否正确配置。您可以通过运行以下命令来验证您的Python版本和模块安装路径:
python --version pip show dotenv如果发现问题,请重新配置您的Python环境或使用虚拟环境。
-
如果使用的是Anaconda环境,请确保已使用Anaconda Navigator或Anaconda Prompt打开了正确的环境,并且尝试运行您的代码。
-
如果您在IDE(如PyCharm或VS Code)中使用,请确保您的IDE已正确配置Python解释器,并且您正在使用正确的环境。
如果以上步骤没有解决您的问题,请提供更多关于您的开发环境和错误信息的详细信息,以便我们能够更好地帮助您解决问题。
-
- User
PS F:\tpyrced_inf-delivery-sjtu-main\inf-delivery-sjtu-main> pip install dotenv Defaulting to user installation because normal site-packages is not writeable Collecting dotenv Using cached dotenv-0.0.5.tar.gz (2.4 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... error error: subprocess-exited-with-error
× pip subprocess to install backend dependencies did not run successfully. │ exit code: 1 ╰─> [31 lines of output] Collecting distribute Using cached distribute-0.7.3.zip (145 kB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'error' error: subprocess-exited-with-error
- Assistant
这个错误显示在安装"dotenv"时遇到了问题。根据错误消息,可能是与"setuptools"或"distutils"模块有关的问题。您可以尝试以下步骤解决问题:
-
更新pip:运行以下命令以确保您使用的是最新版本的pip。
python -m pip install --upgrade pip -
更新setuptools:运行以下命令以更新setuptools模块。
python -m pip install --upgrade setuptools -
安装"python-dotenv":运行以下命令来安装"python-dotenv"模块。
python -m pip install python-dotenv
如果以上步骤没有解决问题,您可以尝试以下替代解决方法:
- 使用较新的Python版本。
- 在虚拟环境中安装"python-dotenv"。
- 在另一个干净的Python环境中尝试安装。
如果问题仍然存在,请提供完整的错误消息和更多关于您的开发环境(包括Python版本和操作系统)的信息,以便我们能够更好地帮助您解决问题。
-