Skip to content

Commit

Permalink
fix: regex unable to parse file type and trial music is played
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHuo committed Mar 26, 2022
1 parent ef5bd99 commit 42ee489
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions netease.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
import os.path
import re
import yaml
import logging

Expand All @@ -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"))
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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「<b>"+song.name+"</b>」\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="「<b>"+song.name+"</b>」\nby "+song.artist, parse_mode='HTML')
Expand Down

0 comments on commit 42ee489

Please sign in to comment.