This commit is contained in:
ycg 2025-08-25 14:25:27 +08:00
commit d92dadd3a0
4 changed files with 76 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
docker/Dockerfile Normal file
View File

@ -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"]

56
main.py Normal file
View File

@ -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)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
requests~=2.32.3
APScheduler~=3.11.0
Flask~=2.2.3