62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
import httpx
|
|
from Music import Music
|
|
import uuid
|
|
from Extensions.Netease import NeteaseSession
|
|
from Extensions.Provider import SessionProvider
|
|
from mutagen import mp3, flac
|
|
|
|
providers = []
|
|
|
|
def downloadMusic(music: Music, path: str) -> dict:
|
|
musicUUID = str(uuid.uuid4())
|
|
filename = path + "/" + musicUUID + ".mp3"
|
|
imagefilename = path + "/" + musicUUID + ".jpg"
|
|
lyricfilename = path + "/" + musicUUID + ".lrc"
|
|
try:
|
|
with httpx.stream("GET", music.musicURL) as r:
|
|
with open(filename, "wb") as f:
|
|
for chunk in r.iter_bytes():
|
|
f.write(chunk)
|
|
with httpx.stream("GET", music.imageURL) as r:
|
|
with open(imagefilename, "wb") as f:
|
|
for chunk in r.iter_bytes():
|
|
f.write(chunk)
|
|
with open(lyricfilename, "wb") as f:
|
|
f.write(music.lyric.encode("utf-8")) # 仅测试用途
|
|
except PermissionError:
|
|
return {"status": 500, "msg": _("File saving failed, permission denied!")}, 500
|
|
except FileNotFoundError:
|
|
return {"status": 500, "msg": _("File saving failed, path doesn't exist!")}, 500
|
|
except:
|
|
return {"status": 500, "msg": _("File saving failed, internal server error!")}, 500
|
|
music.uuid = musicUUID
|
|
return {"status": 200, "msg": _("File saved."), "filename": filename, "uuid": musicUUID}, 200
|
|
|
|
def judgeProvider(provider: str, content: str, providerList: list) -> SessionProvider or None :
|
|
for i in providerList:
|
|
if provider == i.providerName:
|
|
return i
|
|
if i.checkURL(content):
|
|
return i
|
|
return None
|
|
|
|
def imagePayload(music) -> bytes or None:
|
|
with open(music, "rb") as file:
|
|
if(music.endswith("mp3")):
|
|
target = mp3.MP3(file)
|
|
return target.tags['APIC:'].data
|
|
elif(music.endswith("flac")):
|
|
target = flac.FLAC(file)
|
|
return target.pictures[0].data
|
|
else:
|
|
raise AttributeError(_("Unsupported file format."))
|
|
raise AttributeError(_("File not found."))
|
|
|
|
if __name__ == "__main__":
|
|
#ncmsession = NeteaseSession()
|
|
#print(judgeProvider(None, "https://music.163.com/song?id=114514", [ncmsession]))
|
|
#exit(0)
|
|
mu01 = Music("pig2014", None, "My Ordinary Life", "Netease", "https://p2.music.126.net/E_oSaVzztyW0yg-5IvklCg==/109951165802120160.jpg", "http://m701.music.126.net/20240402195141/3b62ce359fc591ca8857a2dd0b9c6909/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/28531063631/77f4/653d/bdf8/a43f2583f10ce9f41046586a19a19c77.mp3")
|
|
print(downloadMusic(mu01, "/tmp/ttmp"))
|
|
#print(mu01.dict())
|