Files
2024-05-26 13:25:28 +08:00

161 lines
6.0 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pyncm import apis as ncm
import pyncm
try:
from Provider import SessionProvider, SongStatus, AccountType
except:
from Extensions.Provider import SessionProvider, SongStatus, AccountType
import httpx
from functools import lru_cache
import re
from enum import Enum
class NeteaseSession(SessionProvider):
session = None
providerName = "Netease"
URLKeyword = ["music.163.com", "163cn.tv", "网易云", "netease"]
accountType = AccountType.ACCOUNT_NOLOGIN
def updateAccountType(self) -> AccountType:
response = ncm.login.GetCurrentLoginStatus()["account"]
if not response:
self.accountType = AccountType.ACCOUNT_NOLOGIN
return
if response["paidFee"]:
self.accountType = AccountType.ACCOUNT_VIP
else:
self.accountType = AccountType.ACCOUNT_USER
def login(self) -> dict:
self.status.cache_clear()
status = ncm.login.LoginQrcodeCheck(self.session)
if status["code"] == 803:
ncm.login.WriteLoginInfo(ncm.login.GetCurrentLoginStatus())
self.updateAccountType()
return {"status": 200, "msg": _("Netease account logged in.")}, 200
else:
return {"status": 408, "msg": _("Netease account login failed.")}, 408
def qrcode(self):
self.session = ncm.login.LoginQrcodeUnikey()["unikey"]
loginURL = f"https://music.163.com/login?codekey={self.session}"
#return self._qrcode(loginURL)
return loginURL
def logout(self) -> dict:
self.status.cache_clear()
ncm.login.LoginLogout()
self.accountType = AccountType.ACCOUNT_NOLOGIN
return {"status": 200, "msg": _("Netease account logged out.")}, 200
@lru_cache(maxsize=8)
def status(self) -> dict:
return ncm.login.GetCurrentLoginStatus()
def userStatus(self) -> dict:
try:
status = self.status()
if self.accountType == AccountType.ACCOUNT_NOLOGIN:
return {"status": 200, "accountType": self.accountType, "username": "未登录"}
if self.accountType == AccountType.ACCOUNT_ANONYMOUS:
return {"status": 200, "accountType": self.accountType, "username": "匿名用户"}
return {"status": 200, "accountType": self.accountType, "username": status["profile"]["nickname"]}
except Exception as e:
print(e)
return {"status": 500, "msg": _("Get status failed, internal server error!")}
def loginAnonymous(self) -> dict:
self.status.cache_clear()
try:
ncm.login.LoginViaAnonymousAccount()
self.accountType = AccountType.ACCOUNT_ANONYMOUS
return {"status": 200, "msg": _("Netease anonymous account logged in.")}, 200
except:
return {"status": 408, "msg": _("Netease anonymous account login failed, timed out.")}, 408
def checkURL(self, content: str) -> bool:
for i in self.URLKeyword:
if i in content:
return True
return False
def parseURL(self, content: str) -> int or None:
uid = None
try:
if "music.163.com" in content:
uid = re.search(r"song\?id=[0-9]+",content).group()
uid = int(uid[8:])
elif "网易云" in content:
uid = re.search(r"song\?id=[0-9]+",content).group()
uid = int(uid[8:])
elif "163cn.tv" in content:
uid = httpx.get(content).headers.get("location")
uid = re.search(r"song\?id=[0-9]+",uid).group()
uid = int(uid[8:])
elif type(eval(content)) == type(1):
uid = eval(content)
except Exception as e:
print(e)
return None
return uid
def checkSong(self, content: int) -> SongStatus:
response = self._getSongStatus(content)
try:
if len(response["songs"]) == 0:
return SongStatus.SONG_NOTEXIST
if response["songs"][0]["fee"] == 1:
return SongStatus.SONG_ISVIP
return SongStatus.SONG_EXIST
except:
return SongStatus.SONG_NOTEXIST
return SongStatus.SONG_NOTEXIST
def getSongURL(self, content: int) -> str:
response = ncm.track.GetTrackAudio(content)
return response["data"][0]["url"]
def getSongImage(self, content: int) -> str:
response = self._getSongStatus(content)
return response["songs"][0]["al"]["picUrl"]
def getSongName(self, content: int) -> str:
response = self._getSongStatus(content)
name = response["songs"][0]["name"] + "-"
for i in response["songs"][0]["ar"]:
name += i["name"]
name += "/"
return name[:-1]
def getSongLyric(self, content: int) -> str:
#print(self._getSongStatus(content))
response = ncm.track.GetTrackLyrics(content)
return response['lrc']['lyric']
def dumpSession(self) -> str:
return pyncm.DumpSessionAsString(pyncm.GetCurrentSession())
def restoreSession(self, session: str):
session = pyncm.LoadSessionFromString(session)
pyncm.SetCurrentSession(session)
@lru_cache(maxsize=256)
def _getSongStatus(self, content: int) -> dict:
response = ncm.track.GetTrackDetail(content)
return response
if __name__ == "__main__":
ncmsess = NeteaseSession()
print(ncmsess.parseURL("分享心华的单曲《一人行者》: https://y.music.163.com/m/song?id=526116050&userid=1578177131&dlt=0846 (来自@网易云音乐)"))
# print(ncmsess.loginAnonymous())
# print(ncmsess.dumpSession())
# print(ncmsess.checkSong(1828241226))
print(ncmsess.getSongLyric(30953009))
# print(ncmsess.checkSong(2014291207))
# print(ncmsess.checkSong(1901371647))
# print(ncmsess.checkSong(1828241226))
# print(ncmsess.getSongName(1828241226))
# print(ncmsess.getSongImage(1828241226))
# print(ncmsess.getSongURL(1828241226))
# print(ncmsess.status())
# print(ncmsess._getSongStatus.cache_info())