Skip to content

Instantly share code, notes, and snippets.

@paulzzh
Last active April 28, 2024 18:10
Show Gist options
  • Save paulzzh/a2c4a5382bb4e0c768373203706e843b to your computer and use it in GitHub Desktop.
Save paulzzh/a2c4a5382bb4e0c768373203706e843b to your computer and use it in GitHub Desktop.
ZMusic bilibili music backend
from flask import Flask,request,url_for,jsonify
from bs4 import BeautifulSoup
from pathlib import Path
import requests,ffmpeg,subprocess,os,threading
web_root = None
static = Path("static")
static.mkdir(exist_ok=True)
bkey_cache={}
for file in static.glob("*.mp3"):
bkey_cache[file.name.split(".")[0]]=True
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
app = Flask(__name__)
def download_au(bkey):
u2="https://m.bilibili.com/audio/"+bkey
r2=requests.get(u2,headers=headers).text
soup=BeautifulSoup(r2)
u3=soup.audio.get("src")
r3=requests.get(u3,headers=headers).content
process = (ffmpeg.input("pipe:").output("pipe:",format="mp3",acodec="libmp3lame").overwrite_output().run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True))
output_data, _ = process.communicate(input=r3)
with open("static/"+bkey+".mp3","wb") as f:
f.write(output_data)
def download_bv(bkey,cid):
u2="https://api.bilibili.com/x/player/playurl?cid="+cid+"&bvid="+bkey+"&qn=64&fnver=0&fnval=16&fourk=1"
r2=requests.get(u2,headers=headers).json()["data"]
u3=r2["dash"]["audio"][0]["baseUrl"]
headers2={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"referer":"https://www.bilibili.com/video/"+bkey}
r3=requests.get(u3,headers=headers2).content
process = (ffmpeg.input("pipe:").output("pipe:",format="mp3",acodec="libmp3lame").overwrite_output().run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True))
output_data, _ = process.communicate(input=r3)
with open("static/"+bkey+".mp3","wb") as f:
f.write(output_data)
@app.route("/info",methods=["GET","POST"])
def info():
key=request.values.get("id")
head=key[:2].lower()
aid=key[2:]
if head=="au":
bkey = "au"+str(int(aid))
u1="https://www.bilibili.com/audio/music-service-c/web/song/info?sid="+str(int(aid))
r1=requests.get(u1,headers=headers).json()["data"]
if not bkey in bkey_cache:
#bkey_cache[bkey]=threading.Thread(target=download_au, args=(bkey,))
#bkey_cache[bkey].start()
download_au(bkey)
bkey_cache[bkey]=True
return jsonify({"code":200,"data":{"title":r1["title"],"author":r1["author"],"duration":r1["duration"],"lyric":r1["lyric"],"musicUrl":(web_root if web_root else request.url_root)+"static/"+bkey+".mp3"}})
if head=="av":
u1="https://api.bilibili.com/x/web-interface/view?aid="+str(int(aid))
r1=requests.get(u1,headers=headers).json()["data"]
bkey = r1["bvid"]
if not bkey in bkey_cache:
#bkey_cache[bkey]=threading.Thread(target=download_bv, args=(bkey,str(r1["pages"][0]["cid"]),))
#bkey_cache[bkey].start()
download_bv(bkey,str(r1["pages"][0]["cid"]))
bkey_cache[bkey]=True
return jsonify({"code":200,"data":{"title":r1["title"],"author":r1["owner"]["name"],"duration":r1["pages"][0]["duration"],"lyric":"","musicUrl":(web_root if web_root else request.url_root)+"static/"+bkey+".mp3"}})
if head=="bv":
u1="https://api.bilibili.com/x/web-interface/view?bvid="+key
r1=requests.get(u1,headers=headers).json()["data"]
bkey = r1["bvid"]
if not bkey in bkey_cache:
#bkey_cache[bkey]=threading.Thread(target=download_bv, args=(bkey,str(r1["pages"][0]["cid"]),))
#bkey_cache[bkey].start()
download_bv(bkey,str(r1["pages"][0]["cid"]))
bkey_cache[bkey]=True
return jsonify({"code":200,"data":{"title":r1["title"],"author":r1["owner"]["name"],"duration":r1["pages"][0]["duration"],"lyric":"","musicUrl":(web_root if web_root else request.url_root)+"static/"+bkey+".mp3"}})
return jsonify({})
app.run(port=38383,host="0.0.0.0")
Flask
ffmpeg-python
requests
bs4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment