1
This commit is contained in:
commit
d92dadd3a0
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
9
docker/Dockerfile
Normal file
9
docker/Dockerfile
Normal 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
56
main.py
Normal 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
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
requests~=2.32.3
|
||||||
|
APScheduler~=3.11.0
|
||||||
|
Flask~=2.2.3
|
||||||
Loading…
Reference in New Issue
Block a user