2021-06-27 08:59:16 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2021-12-05 08:06:20 +00:00
|
|
|
|
from asyncio import sleep
|
2021-12-04 09:27:00 +00:00
|
|
|
|
from os import sep, remove, listdir
|
|
|
|
|
from os.path import isfile
|
|
|
|
|
from sys import executable
|
2021-12-05 08:06:20 +00:00
|
|
|
|
from time import strftime, localtime
|
2021-06-27 08:59:16 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2021-12-04 09:27:00 +00:00
|
|
|
|
from mutagen.id3 import ID3, APIC, TIT2, TPE1
|
|
|
|
|
from pyncm import GetCurrentSession, apis, DumpSessionAsString, SetCurrentSession, LoadSessionFromString
|
|
|
|
|
from pyncm.apis import LoginFailedException
|
|
|
|
|
from pyncm.apis.cloudsearch import CloudSearchType
|
2021-12-05 10:13:46 +00:00
|
|
|
|
from pyncm.apis.login import LoginLogout
|
2021-06-27 08:59:16 +00:00
|
|
|
|
|
|
|
|
|
cc_imported = True
|
|
|
|
|
except ImportError:
|
2021-12-05 10:13:46 +00:00
|
|
|
|
print(f'[!] Please run {executable} -m pip install pyncm')
|
2021-06-27 08:59:16 +00:00
|
|
|
|
cc_imported = False
|
|
|
|
|
|
|
|
|
|
|
2021-12-04 09:27:00 +00:00
|
|
|
|
from telethon.tl.types import DocumentAttributeAudio
|
2021-06-27 08:59:16 +00:00
|
|
|
|
|
2021-12-04 09:27:00 +00:00
|
|
|
|
from pagermaid.listener import listener
|
|
|
|
|
from pagermaid.utils import alias_command, execute
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def download_by_url(url, dest):
|
|
|
|
|
# Downloads generic content
|
|
|
|
|
response = GetCurrentSession().get(url, stream=True)
|
|
|
|
|
with open(dest, 'wb') as f:
|
|
|
|
|
for chunk in response.iter_content(1024 * 2 ** 10):
|
|
|
|
|
f.write(chunk) # write every 1MB read
|
|
|
|
|
return dest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_author(song_info: dict) -> str:
|
|
|
|
|
data = []
|
|
|
|
|
for i in song_info["songs"][0]["ar"]:
|
|
|
|
|
data.append(i["name"])
|
|
|
|
|
return " ".join(data)
|
|
|
|
|
|
|
|
|
|
|
2021-12-05 08:06:20 +00:00
|
|
|
|
def get_duration(song_info: dict, track_info: dict) -> int:
|
|
|
|
|
if track_info["data"][0]["freeTrialInfo"]:
|
|
|
|
|
return track_info["data"][0]["freeTrialInfo"]["end"] - track_info["data"][0]["freeTrialInfo"]["start"]
|
|
|
|
|
else:
|
|
|
|
|
return int(song_info["songs"][0]["dt"] / 1000)
|
|
|
|
|
|
|
|
|
|
|
2021-12-04 09:27:00 +00:00
|
|
|
|
async def netease_down(track_info: dict, song_info: dict) -> str:
|
|
|
|
|
if not isfile(f'data{sep}{song_info["songs"][0]["name"]}.{track_info["data"][0]["type"]}'):
|
|
|
|
|
# Downloding source audio
|
|
|
|
|
download_by_url(track_info["data"][0]["url"],
|
|
|
|
|
f'data{sep}{song_info["songs"][0]["name"]}.{track_info["data"][0]["type"]}')
|
|
|
|
|
# Downloading cover
|
|
|
|
|
download_by_url(song_info["songs"][0]["al"]["picUrl"],
|
|
|
|
|
f'data{sep}{song_info["songs"][0]["name"]}.jpg')
|
|
|
|
|
with open(f'data{sep}{song_info["songs"][0]["name"]}.jpg', 'rb') as f:
|
|
|
|
|
picData = f.read()
|
|
|
|
|
# 设置标签
|
|
|
|
|
info = {'picData': picData,
|
|
|
|
|
'title': song_info["songs"][0]["name"],
|
|
|
|
|
'artist': gen_author(song_info)}
|
|
|
|
|
songFile = ID3(f'data{sep}{song_info["songs"][0]["name"]}.{track_info["data"][0]["type"]}')
|
|
|
|
|
songFile['APIC'] = APIC( # 插入封面
|
|
|
|
|
encoding=3,
|
|
|
|
|
mime='image/jpeg',
|
|
|
|
|
type=3,
|
|
|
|
|
desc=u'Cover',
|
|
|
|
|
data=info['picData']
|
2021-06-27 08:59:16 +00:00
|
|
|
|
)
|
2021-12-04 09:27:00 +00:00
|
|
|
|
songFile['TIT2'] = TIT2( # 插入歌名
|
|
|
|
|
encoding=3,
|
|
|
|
|
text=info['title']
|
2021-06-27 08:59:16 +00:00
|
|
|
|
)
|
2021-12-04 09:27:00 +00:00
|
|
|
|
songFile['TPE1'] = TPE1( # 插入第一演奏家、歌手、等
|
|
|
|
|
encoding=3,
|
|
|
|
|
text=info['artist']
|
|
|
|
|
)
|
|
|
|
|
songFile.save()
|
|
|
|
|
# 返回
|
|
|
|
|
return f'data{sep}{song_info["songs"][0]["name"]}.{track_info["data"][0]["type"]}'
|
2021-06-27 08:59:16 +00:00
|
|
|
|
|
|
|
|
|
|
2021-12-05 08:06:20 +00:00
|
|
|
|
ned_help_msg = f"""
|
|
|
|
|
网易云搜/点歌。
|
|
|
|
|
|
|
|
|
|
i.e.
|
|
|
|
|
`-{alias_command('ned')} 失眠飞行 兔籽鲸 / 雨客Yoker` # 通过歌曲名称+歌手(可选)点歌
|
|
|
|
|
`-{alias_command('ned')} 1430702717` # 通过歌曲 ID 点歌
|
|
|
|
|
`-{alias_command('ned')} login` # 显示登录信息
|
|
|
|
|
`-{alias_command('ned')} login 手机号码 密码` # 登录账号
|
|
|
|
|
`-{alias_command('ned')} clear` # 手动清除缓存
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2021-06-27 08:59:16 +00:00
|
|
|
|
@listener(is_plugin=True, outgoing=True, command=alias_command("ned"),
|
2021-12-05 08:06:20 +00:00
|
|
|
|
description=ned_help_msg,
|
2021-12-04 09:27:00 +00:00
|
|
|
|
parameters="{关键词/id}/{login <账号> <密码>}/{clear}")
|
2021-06-27 08:59:16 +00:00
|
|
|
|
async def ned(context):
|
2021-12-04 09:27:00 +00:00
|
|
|
|
if not cc_imported:
|
2021-12-05 10:13:46 +00:00
|
|
|
|
await context.edit(f"[!] Please run `-sh {executable} -m pip install pyncm` "
|
2021-12-05 08:06:20 +00:00
|
|
|
|
f"and then restart pagermaid.")
|
2021-12-04 09:27:00 +00:00
|
|
|
|
return
|
2021-06-27 08:59:16 +00:00
|
|
|
|
if len(context.parameter) < 1:
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 使用方法
|
2021-12-05 08:06:20 +00:00
|
|
|
|
await context.edit(ned_help_msg)
|
2021-06-27 08:59:16 +00:00
|
|
|
|
return
|
2021-12-04 10:05:19 +00:00
|
|
|
|
# 加载登录信息
|
|
|
|
|
if isfile(f"data{sep}session.ncm"):
|
|
|
|
|
with open(f"data{sep}session.ncm") as f:
|
|
|
|
|
SetCurrentSession(LoadSessionFromString(f.read()))
|
2021-12-05 10:13:46 +00:00
|
|
|
|
# 海外用户
|
|
|
|
|
GetCurrentSession().headers['X-Real-IP'] = '118.88.88.88'
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 处理账号登录
|
|
|
|
|
if context.parameter[0] == "login":
|
|
|
|
|
# 显示登录信息
|
|
|
|
|
if len(context.parameter) == 1:
|
|
|
|
|
login_info = GetCurrentSession().login_info
|
|
|
|
|
if login_info["success"]:
|
2021-12-05 08:06:20 +00:00
|
|
|
|
# 获取VIP类型
|
|
|
|
|
if login_info['content']['account']['vipType'] != 0:
|
|
|
|
|
vip_type = "**VIP**"
|
|
|
|
|
else:
|
|
|
|
|
vip_type = "**普通**"
|
|
|
|
|
# 获取账号创建时间
|
|
|
|
|
time = strftime("%Y-%m-%d %H:%M:%S", localtime(login_info['content']['account']['createTime'] / 1000))
|
|
|
|
|
if context.is_group:
|
|
|
|
|
await context.edit(f"[ned] 已登录{vip_type}账号,账号创建时间:`{time}`")
|
|
|
|
|
else:
|
|
|
|
|
await context.edit(f"[ned] 已登录{vip_type}账号:`{login_info['content']['profile']['nickname']}`,"
|
|
|
|
|
f"账号创建时间:`{time}`")
|
2021-12-04 09:27:00 +00:00
|
|
|
|
else:
|
2021-12-05 08:06:20 +00:00
|
|
|
|
await context.edit(f"[ned] **未登录/登录失败**,额外信息:`{login_info['content']}`")
|
|
|
|
|
return
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 过滤空参数
|
|
|
|
|
if len(context.parameter) == 2:
|
|
|
|
|
# 登录命令格式错误
|
|
|
|
|
await context.edit(f"**使用方法:** `-{alias_command('ned')} <账号> <密码>`")
|
2021-07-15 08:12:49 +00:00
|
|
|
|
return
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 开始登录
|
2021-06-27 08:59:16 +00:00
|
|
|
|
try:
|
2021-12-04 09:27:00 +00:00
|
|
|
|
apis.login.LoginViaCellphone(context.parameter[1], context.parameter[2])
|
|
|
|
|
except LoginFailedException:
|
|
|
|
|
await context.edit("**登录失败**,请检查账号密码是否正确。")
|
2021-06-27 08:59:16 +00:00
|
|
|
|
return
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 获取登录信息
|
|
|
|
|
login_info = GetCurrentSession().login_info
|
2021-12-05 08:06:20 +00:00
|
|
|
|
# 获取VIP类型
|
|
|
|
|
if login_info['content']['account']['vipType'] != 0:
|
|
|
|
|
vip_type = "**VIP**"
|
|
|
|
|
else:
|
|
|
|
|
vip_type = "**普通**"
|
|
|
|
|
# 获取账号创建时间
|
|
|
|
|
time = strftime("%Y-%m-%d %H:%M:%S", localtime(login_info['content']['account']['createTime']/1000))
|
|
|
|
|
if context.is_group:
|
|
|
|
|
await context.edit(f"[ned] **登录成功**,已登录{vip_type}账号,账号创建时间:`{time}`")
|
|
|
|
|
else:
|
|
|
|
|
await context.edit(f"[ned] **登录成功**,已登录{vip_type}账号:`{login_info['content']['profile']['nickname']}`,"
|
|
|
|
|
f"账号创建时间:`{time}`")
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 保存登录信息
|
|
|
|
|
with open(f"data{sep}session.ncm", 'w+') as f:
|
|
|
|
|
f.write(DumpSessionAsString(GetCurrentSession()))
|
|
|
|
|
return
|
2021-12-05 10:13:46 +00:00
|
|
|
|
elif context.parameter[0] == "logout":
|
|
|
|
|
# 登出
|
|
|
|
|
LoginLogout()
|
|
|
|
|
if isfile(f"data{sep}session.ncm"):
|
|
|
|
|
remove(f"data{sep}session.ncm")
|
|
|
|
|
return await context.edit("[ned] 账号登出成功。")
|
|
|
|
|
elif context.parameter[0] == "clear":
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 清除歌曲缓存
|
|
|
|
|
for i in listdir("data"):
|
|
|
|
|
if i.find(".mp3") != -1 or i.find(".jpg") != -1:
|
|
|
|
|
remove(f"data{sep}{i}")
|
2021-12-05 10:13:46 +00:00
|
|
|
|
await context.edit("[ned] **已清除缓存**")
|
2021-12-04 09:27:00 +00:00
|
|
|
|
return
|
|
|
|
|
# 搜索歌曲
|
|
|
|
|
song_id = context.arguments
|
|
|
|
|
# id
|
|
|
|
|
if song_id.isdigit():
|
|
|
|
|
song_id = int(song_id)
|
|
|
|
|
else:
|
|
|
|
|
search_data = apis.cloudsearch.GetSearchResult(song_id, CloudSearchType(1), 1)
|
|
|
|
|
if len(search_data["result"]["songs"]) == 1:
|
|
|
|
|
song_id = search_data["result"]["songs"][0]["id"]
|
|
|
|
|
else:
|
|
|
|
|
await context.edit(f"**没有找到歌曲**,请检查歌曲名称是否正确。")
|
2021-06-27 09:16:30 +00:00
|
|
|
|
return
|
2021-12-04 09:27:00 +00:00
|
|
|
|
# 获取歌曲信息小于等于 320k HQ
|
|
|
|
|
track_info = apis.track.GetTrackAudio([song_id])
|
|
|
|
|
# 获取歌曲详情
|
|
|
|
|
song_info = apis.track.GetTrackDetail([song_id])
|
|
|
|
|
if track_info["data"][0]["code"] == 404:
|
|
|
|
|
await context.edit(f"**没有找到歌曲**,请检查歌曲id是否正确。")
|
|
|
|
|
return
|
|
|
|
|
await context.edit(f"正在下载歌曲:**{song_info['songs'][0]['name']} - {gen_author(song_info)}**")
|
|
|
|
|
# 下载歌曲并且设置歌曲标签
|
|
|
|
|
path = await netease_down(track_info, song_info)
|
|
|
|
|
await context.edit("正在上传歌曲。。。")
|
|
|
|
|
# 上传歌曲
|
2021-12-05 08:06:20 +00:00
|
|
|
|
cap_ = ""
|
|
|
|
|
# 提醒登录VIP账号
|
|
|
|
|
if track_info["data"][0]["freeTrialInfo"]:
|
|
|
|
|
cap_ = f"**非VIP,正在试听 {track_info['data'][0]['freeTrialInfo']['start']}s ~ " \
|
|
|
|
|
f"{track_info['data'][0]['freeTrialInfo']['end']}s**\n"
|
2021-12-04 09:27:00 +00:00
|
|
|
|
cap = f"「**{song_info['songs'][0]['name']}**」\n" \
|
|
|
|
|
f"{gen_author(song_info)}\n" \
|
2021-12-05 08:06:20 +00:00
|
|
|
|
f"\n{cap_}" \
|
2021-12-04 09:27:00 +00:00
|
|
|
|
f"#netease #{int(track_info['data'][0]['br'] / 1000)}kbps #{track_info['data'][0]['type']}"
|
|
|
|
|
await context.client.send_file(
|
|
|
|
|
context.chat_id,
|
|
|
|
|
path,
|
|
|
|
|
caption=cap,
|
|
|
|
|
link_preview=False,
|
|
|
|
|
force_document=False,
|
|
|
|
|
thumb=path[:-3] + 'jpg',
|
|
|
|
|
attributes=(DocumentAttributeAudio(
|
2021-12-05 08:06:20 +00:00
|
|
|
|
get_duration(song_info, track_info), False, song_info['songs'][0]['name'], gen_author(song_info)),)
|
2021-12-04 09:27:00 +00:00
|
|
|
|
)
|
|
|
|
|
await context.delete()
|
2021-12-05 08:06:20 +00:00
|
|
|
|
# 过多文件自动清理
|
|
|
|
|
if len(listdir("data")) > 100:
|
|
|
|
|
for i in listdir("data"):
|
|
|
|
|
if i.find(".mp3") != -1 or i.find(".jpg") != -1:
|
|
|
|
|
remove(f"data{sep}{i}")
|
2021-12-05 10:13:46 +00:00
|
|
|
|
msg = await context.respond("[ned] **已自动清除缓存**")
|
2021-12-05 08:06:20 +00:00
|
|
|
|
await sleep(3)
|
|
|
|
|
await msg.delete()
|