commit d92dadd3a00516a92c71d50512513896338d1a89 Author: ycg <3208975282@qq.com> Date: Mon Aug 25 14:25:27 2025 +0800 1 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..07ad1e9 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,9 @@ +# Python 应用示例 +FROM python:3.9-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt # 禁用缓存减小体积 +COPY . . +EXPOSE 5000 +USER appuser # 切换非 root 用户 +CMD ["gunicorn", "app:app"] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..30289ea --- /dev/null +++ b/main.py @@ -0,0 +1,56 @@ +from apscheduler.schedulers.background import BackgroundScheduler +from flask import Flask +import time +import random +import requests + +app = Flask(__name__) +cache = set() +scheduler = BackgroundScheduler() + + +def init_scheduler(): + def clear_cache(): + """清空缓存并记录日志""" + cache.clear() + print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] 缓存已清空") + + scheduler.add_job(clear_cache, 'cron', hour=0) + scheduler.start() + + +def fetch(api): + """调用百度热搜 API (示例接口,需替换实际 API Key) [6](@ref)""" + url = api[1]['url'] + hotList = api[1]['hot'] + random.shuffle(hotList) + hot = hotList[0] + try: + res = requests.get(url + hot) + if res.json()['code'] == 200: + return res.json()['data'][:10] + except Exception: + return [] + + +@app.route("/hot") +def get_random_hot_searches(): + """随机调用平台接口并过滤缓存数据""" + apis = [ + ["hot.nntool.cc", {'url': "https://hotapi.nntool.cc/", + 'hot': ["weibo", "douyin", "baidu", "toutiao", "thepaper", "qq-news", "zhihu"]}] + ] + random.shuffle(apis) # 随机打乱接口顺序 + hots = fetch(apis[0]) + for hot in hots: + if hot['title'] in cache: + continue + else: + cache.add(hot['title']) + return {'code': 200, 'data': [hot]} + return [{"title": "所有平台数据均重复或无数据", "source": "system"}] + + +if __name__ == "__main__": + # init_scheduler() + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4239b6e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests~=2.32.3 +APScheduler~=3.11.0 +Flask~=2.2.3 \ No newline at end of file