|
from bs4 import BeautifulSoup |
|
from flask import Flask, request, jsonify |
|
import requests |
|
|
|
from flask_cors import CORS |
|
from apscheduler.schedulers.background import BackgroundScheduler |
|
from datetime import datetime |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
|
|
|
|
cached_results_soaring = [] |
|
cached_results_hot = [] |
|
cached_results_newSongs = [] |
|
cached_results_popular = [] |
|
last_update_time = None |
|
|
|
def song(url): |
|
"""从 QQ 音乐榜单页面爬取歌曲信息""" |
|
|
|
headers = { |
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
|
"Referer": "https://y.qq.com/", |
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", |
|
} |
|
|
|
results = [] |
|
try: |
|
|
|
|
|
response = requests.get(url, headers=headers) |
|
response.encoding = 'utf-8' |
|
|
|
|
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
|
|
|
|
song_items = soup.select("ul.songlist__list li") |
|
|
|
for item in song_items[:10]: |
|
try: |
|
|
|
song_name_span = item.select_one("span.songlist__songname_txt") |
|
song_name = song_name_span.text.strip() |
|
|
|
|
|
artist_div = item.select_one("div.songlist__artist") |
|
artist_name = artist_div.text.strip() |
|
|
|
|
|
song_link = song_name_span.find('a', class_='') |
|
|
|
mid = "default_mid" |
|
if song_link and song_link.get('href'): |
|
mid = song_link['href'].split('/')[-1] |
|
|
|
|
|
img_link = song_name_span.find('a', class_='songlist__cover') |
|
if img_link and img_link.get('href'): |
|
imgsrc = img_link['href'].split('/')[-1] |
|
img_url = f"https://y.qq.com/music/photo_new/T002R300x300M000{imgsrc}_1.jpg?max_age=2592000" |
|
|
|
if song_name and artist_name: |
|
results.append({ |
|
"muName": song_name, |
|
"cover": img_url if img_url else "", |
|
"mid": mid, |
|
"id": mid, |
|
"song": song_name, |
|
"singer": artist_name, |
|
"muLink": 'qqdg/?word', |
|
"uname": 'QQ音乐', |
|
"muId": 1 |
|
}) |
|
except Exception as e: |
|
print(f"处理歌曲项时出错: {e}") |
|
continue |
|
|
|
except Exception as e: |
|
print(f"爬取过程中出错: {e}") |
|
return [] |
|
|
|
|
|
if results: |
|
print(f"数据已更新:{last_update_time}") |
|
return results |
|
|
|
|
|
def schedule_music_fetch(): |
|
"""设置每天早上 8 点定时任务""" |
|
scheduler = BackgroundScheduler() |
|
|
|
|
|
def run_tasks(): |
|
global cached_results_soaring, cached_results_hot, cached_results_newSongs, cached_results_popular, last_update_time |
|
|
|
cached_results_soaring = song("https://y.qq.com/n/ryqq/toplist/62") |
|
cached_results_hot = song("https://y.qq.com/n/ryqq/toplist/26") |
|
cached_results_newSongs = song("https://y.qq.com/n/ryqq/toplist/27") |
|
cached_results_popular = song("https://y.qq.com/n/ryqq/toplist/4") |
|
last_update_time = datetime.now() |
|
|
|
|
|
|
|
scheduler.add_job(run_tasks, 'cron', hour=8, minute=0) |
|
|
|
scheduler.start() |
|
print("定时任务已启动,爬取时间为每天早上 8 点。") |
|
|
|
@app.route('/star') |
|
def star(): |
|
global cached_results_soaring, cached_results_hot, cached_results_newSongs, cached_results_popular, last_update_time |
|
cached_results_soaring = song("https://y.qq.com/n/ryqq/toplist/62") |
|
cached_results_hot = song("https://y.qq.com/n/ryqq/toplist/26") |
|
cached_results_newSongs = song("https://y.qq.com/n/ryqq/toplist/27") |
|
cached_results_popular = song("https://y.qq.com/n/ryqq/toplist/4") |
|
last_update_time = datetime.now() |
|
return jsonify({ |
|
"data": "数据已更新", |
|
"updata time": last_update_time |
|
}) |
|
|
|
@app.route('/fetch_music_soaring', methods=['GET']) |
|
def fetch_music_route_soaring(): |
|
"""提供缓存的 QQ 音乐榜单数据""" |
|
if not cached_results_soaring: |
|
return jsonify({"message": "数据尚未准备好,请先访问/star 更新数据"}), 503 |
|
|
|
return jsonify({ |
|
"last_update_time": last_update_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"data": cached_results_soaring |
|
}) |
|
|
|
@app.route('/fetch_music_hot', methods=['GET']) |
|
def fetch_music_route_hot(): |
|
"""提供缓存的 QQ 音乐榜单数据""" |
|
if not cached_results_hot: |
|
return jsonify({"message": "数据尚未准备好,请先访问/star 更新数据"}), 503 |
|
|
|
return jsonify({ |
|
"last_update_time": last_update_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"data": cached_results_hot |
|
}) |
|
|
|
@app.route('/fetch_music_newSongs', methods=['GET']) |
|
def fetch_music_route_newSongs(): |
|
"""提供缓存的 QQ 音乐榜单数据""" |
|
if not cached_results_newSongs: |
|
return jsonify({"message": "数据尚未准备好,请先访问/star 更新数据"}), 503 |
|
|
|
return jsonify({ |
|
"last_update_time": last_update_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"data": cached_results_newSongs |
|
}) |
|
|
|
@app.route('/fetch_music_popular', methods=['GET']) |
|
def fetch_music_route_popular(): |
|
"""提供缓存的 QQ 音乐榜单数据""" |
|
if not cached_results_popular: |
|
return jsonify({"message": "数据尚未准备好,请先访问/star 更新数据"}), 503 |
|
|
|
return jsonify({ |
|
"last_update_time": last_update_time.strftime("%Y-%m-%d %H:%M:%S"), |
|
"data": cached_results_popular |
|
}) |
|
if __name__ == '__main__': |
|
|
|
schedule_music_fetch() |
|
app.run(debug=True,host='0.0.0.0',port=5734) |