From 42ee489d4817817a33d61021178573adb5850376 Mon Sep 17 00:00:00 2001 From: Holger Huo Date: Sat, 26 Mar 2022 23:22:11 +0800 Subject: [PATCH] fix: regex unable to parse file type and trial music is played --- netease.py | 15 ++++++++------- run.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/netease.py b/netease.py index 4373843..64a9442 100644 --- a/netease.py +++ b/netease.py @@ -1,6 +1,5 @@ import requests import os.path -import re import yaml import logging @@ -12,12 +11,14 @@ class Song(object): artist = "" id = 0 url = "" + format = "" - def __init__(self, name, artist, id, url): + def __init__(self, name, artist, id, url, format): self.name = name self.artist = artist self.id = id self.url = url + self.format = format # Load API config config = yaml.safe_load(open("config.yml")) @@ -36,17 +37,17 @@ def get_song_info(keyword): for song in songs.json()["result"]["songs"]: availability = request_api(api+"/check/music?id="+str(song["id"])) if availability.json()["success"]: - url = request_api(api+"/song/url?id="+str(song["id"])).json()["data"][0]["url"] - if url is not None: + song_meta = request_api(api+"/song/url?id="+str(song["id"])).json()["data"][0] + if song_meta["url"] is not None and song_meta["freeTrialInfo"] is None: artists = [] for artist in song['artists']: artists.append(artist['name']) - return Song(song["name"], '&'.join(artists), song["id"], url) + return Song(song["name"], '&'.join(artists), song["id"], song_meta["url"], song_meta["type"]) return False # Download song -def cache_song(id, url): - location = tmp_dir+str(id)+re.search('(\.[^.\\/:*?"<>|\r\n]+$)', url).group(1) +def cache_song(id, url, format): + location = tmp_dir+str(id)+'.'+format if not os.path.isfile(location): data = requests.get(url) logger.warning("Song "+str(id)+" has been cached") diff --git a/run.py b/run.py index 6213225..672aea1 100644 --- a/run.py +++ b/run.py @@ -46,7 +46,7 @@ def handle_netease(message): logger.warning(keyword+" is not found!") else: # Send Music bot.edit_message_text(chat_id=message.chat.id, message_id=reply.id, text="正在缓存\n「"+song.name+"」\nby "+song.artist, parse_mode='HTML') - location = netease.cache_song(song.id, song.url) + location = netease.cache_song(song.id, song.url, song.format) with open(location, 'rb') as music: bot.send_chat_action(message.chat.id, "upload_audio") bot.send_voice(chat_id=message.chat.id, reply_to_message_id=message.message_id, voice=music, caption="「"+song.name+"」\nby "+song.artist, parse_mode='HTML')