Skip to content

Commit

Permalink
add: save id3 tag in music file
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHuo committed Mar 27, 2022
1 parent bc67b4c commit 5f86f9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
27 changes: 19 additions & 8 deletions netease.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
import yaml
import logging
import eyed3

logger = logging.getLogger(__name__)

Expand All @@ -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"))
Expand All @@ -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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytelegrambotapi
requests
pyyaml
pyyaml
eyeD3
5 changes: 3 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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「<b>"+song.name+"</b>」\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「<b>"+song.name+"</b>」\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="「<b>"+song.name+"</b>」\nby "+song.artist, parse_mode='HTML')
bot.send_audio(chat_id=message.chat.id, reply_to_message_id=message.message_id, audio=music, caption="「<b>"+song.name+"</b>」\nby "+song.artist, parse_mode='HTML')
bot.delete_message(chat_id=message.chat.id, message_id=reply.id)

bot.infinity_polling()

0 comments on commit 5f86f9f

Please sign in to comment.