PagerMaid_Plugins/neteaseshuffle.py

75 lines
3.1 KiB
Python
Raw Normal View History

2020-08-25 13:16:52 +00:00
import json
import requests
from time import sleep
from pagermaid.listener import listener
from os import remove, path
2021-06-16 07:09:40 +00:00
from pagermaid.utils import alias_command
2020-08-25 13:16:52 +00:00
2021-06-16 07:09:40 +00:00
@listener(is_plugin=True, outgoing=True, command=alias_command("ns"),
2020-08-25 13:16:52 +00:00
description="随机网抑云热歌。")
async def ns(context):
await context.edit("获取中 . . .")
try:
import eyed3
imported = True
except ImportError:
imported = False
await context.edit("获取中 . . .\n(eyeD3支持库未安装歌曲文件信息将无法导入\n请使用-sh pip3 install eyed3安装或自行ssh安装)")
status = False
for _ in range(20): # 最多尝试20次
req = requests.get(
"http://api.uomg.com/api/rand.music?sort=%E7%83%AD%E6%AD%8C%E6%A6%9C&format=json")
if req.status_code == 200:
req = json.loads(req.content)
songid = req["data"]["url"][45:]
music = req['data']['url']
2021-06-16 07:09:40 +00:00
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
'like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,"
"*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}
2020-08-25 13:16:52 +00:00
music = requests.request("GET", music, headers=headers)
name = str(req['data']['name']) + ".mp3"
2020-08-25 13:16:52 +00:00
with open(name, 'wb') as f:
f.write(music.content)
if (path.getsize(name) / 1024) < 100:
remove(name)
continue
req = requests.get(
"https://api.imjad.cn/cloudmusic/?type=detail&id=" + songid)
if req.status_code == 200:
req = json.loads(req.content)
album = req['songs'][0]['al']['name']
albumpic = requests.get(req['songs'][0]['al']['picUrl'])
artist = req['songs'][0]['ar'][0]['name']
title = req['songs'][0]['name']
cap = artist + " - " + title
else:
continue
if imported is True:
tag = eyed3.load(name).tag
tag.encoding = '\x01'
tag.artist = artist
tag.title = title
tag.album = album
tag.images.set(3, albumpic.content, "image/jpeg", u'')
tag.save()
await context.client.send_file(
context.chat_id,
name,
caption=cap,
link_preview=False,
force_document=False)
try:
remove(name)
except:
pass
status = True
break
2020-08-25 13:16:52 +00:00
else:
continue
2021-06-16 07:09:40 +00:00
if not status:
2020-08-25 13:16:52 +00:00
await context.edit("出错了呜呜呜 ~ 试了好多好多次都无法访问到 API 服务器 。")
sleep(2)
await context.delete()