diff --git a/netease.py b/netease.py
index 64a9442..b9531b3 100644
--- a/netease.py
+++ b/netease.py
@@ -2,6 +2,7 @@
import os.path
import yaml
import logging
+import eyed3
logger = logging.getLogger(__name__)
@@ -12,13 +13,15 @@ class Song(object):
id = 0
url = ""
format = ""
+ album = ""
- def __init__(self, name, artist, id, url, format):
+ def __init__(self, name, artist, id, url, format, album):
self.name = name
self.artist = artist
self.id = id
self.url = url
self.format = format
+ self.album = album
# Load API config
config = yaml.safe_load(open("config.yml"))
@@ -33,24 +36,32 @@ def request_api(url):
# Search for songs
def get_song_info(keyword):
- songs = request_api(api+"/search?keywords="+keyword+"&type=1")
- for song in songs.json()["result"]["songs"]:
- availability = request_api(api+"/check/music?id="+str(song["id"]))
- if availability.json()["success"]:
+ songs = request_api(api+"/search?keywords="+keyword+"&type=1").json()["result"]["songs"]
+ for song in songs:
+ availability = request_api(api+"/check/music?id="+str(song["id"])).json()["success"]
+ if availability:
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"], song_meta["url"], song_meta["type"])
+ return Song(song["name"], '&'.join(artists), song["id"], song_meta["url"], song_meta["type"], song['album']['name'])
return False
# Download song
-def cache_song(id, url, format):
+def cache_song(id, url, format, name, artist, album):
location = tmp_dir+str(id)+'.'+format
if not os.path.isfile(location):
data = requests.get(url)
- logger.warning("Song "+str(id)+" has been cached")
with open(location, 'wb')as file:
file.write(data.content)
+ img_url = requests.get(api+"/song/detail?ids="+str(id)).json()["songs"][0]['al']['picUrl']
+ image = requests.get(img_url)
+ audio = eyed3.load(location)
+ audio.tag.artist = artist
+ audio.tag.album = album
+ audio.tag.title = name
+ audio.tag.images.set(3, image.content, image.headers['content-type'])
+ audio.tag.save()
+ logger.warning("Song "+str(id)+" has been cached")
return location
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index 2a12ff0..f9573fe 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
pytelegrambotapi
requests
-pyyaml
\ No newline at end of file
+pyyaml
+eyeD3
\ No newline at end of file
diff --git a/run.py b/run.py
index 5c885bc..4dd11ff 100644
--- a/run.py
+++ b/run.py
@@ -49,10 +49,11 @@ 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, song.format)
+ location = netease.cache_song(song.id, song.url, song.format, song.name, song.artist, song.album)
with open(location, 'rb') as music:
+ bot.edit_message_text(chat_id=message.chat.id, message_id=reply.id, text="正在发送\n「"+song.name+"」\nby "+song.artist, parse_mode='HTML')
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')
+ bot.send_audio(chat_id=message.chat.id, reply_to_message_id=message.message_id, audio=music, caption="「"+song.name+"」\nby "+song.artist, parse_mode='HTML')
bot.delete_message(chat_id=message.chat.id, message_id=reply.id)
bot.infinity_polling()
\ No newline at end of file