refactor: avoid use youtube-dl if music is existed locally. #78
This commit is contained in:
parent
9ce98196a1
commit
7c8c1f9d9a
18
command.py
18
command.py
@ -250,16 +250,14 @@ def cmd_play_url(bot, user, text, command, parameter):
|
|||||||
'user': user,
|
'user': user,
|
||||||
'ready': 'validation'}
|
'ready': 'validation'}
|
||||||
|
|
||||||
if media.url.get_url_info(music):
|
music = bot.validate_music(music)
|
||||||
if music['duration'] > var.config.getint('bot', 'max_track_duration'):
|
if music:
|
||||||
bot.send_msg(constants.strings('too_long'), text)
|
var.playlist.append(music)
|
||||||
else:
|
logging.info("cmd: add to playlist: " + music['url'])
|
||||||
music['ready'] = "no"
|
bot.send_msg(constants.strings('file_added', item=util.format_song_string(music)), text)
|
||||||
var.playlist.append(music)
|
if var.playlist.length() == 2:
|
||||||
logging.info("cmd: add to playlist: " + music['url'])
|
# If I am the second item on the playlist. (I am the next one!)
|
||||||
if var.playlist.length() == 2:
|
bot.async_download_next()
|
||||||
# If I am the second item on the playlist. (I am the next one!)
|
|
||||||
bot.async_download_next()
|
|
||||||
else:
|
else:
|
||||||
bot.send_msg(constants.strings('unable_download'), text)
|
bot.send_msg(constants.strings('unable_download'), text)
|
||||||
|
|
||||||
|
11
interface.py
11
interface.py
@ -201,10 +201,13 @@ def post():
|
|||||||
'url': request.form['add_url'],
|
'url': request.form['add_url'],
|
||||||
'user': 'Web',
|
'user': 'Web',
|
||||||
'ready': 'validation'}
|
'ready': 'validation'}
|
||||||
media.url.get_url_info(music)
|
music = var.botamusique.validate_music(music)
|
||||||
music = var.playlist.append(music)
|
if music:
|
||||||
logging.info("web: add to playlist: " + util.format_debug_song_string(music))
|
var.playlist.append(music)
|
||||||
var.playlist.playlist[-1]['ready'] = "no"
|
logging.info("web: add to playlist: " + util.format_debug_song_string(music))
|
||||||
|
if var.playlist.length() == 2:
|
||||||
|
# If I am the second item on the playlist. (I am the next one!)
|
||||||
|
var.botamusique.async_download_next()
|
||||||
|
|
||||||
elif 'add_radio' in request.form:
|
elif 'add_radio' in request.form:
|
||||||
music = var.playlist.append({'type': 'radio',
|
music = var.playlist.append({'type': 'radio',
|
||||||
|
63
mumbleBot.py
63
mumbleBot.py
@ -367,6 +367,41 @@ class MumbleBot:
|
|||||||
self.playhead = 0
|
self.playhead = 0
|
||||||
self.last_volume_cycle_time = time.time()
|
self.last_volume_cycle_time = time.time()
|
||||||
|
|
||||||
|
def validate_music(self, music):
|
||||||
|
url = music['url']
|
||||||
|
|
||||||
|
url_hash = hashlib.md5(url.encode()).hexdigest()
|
||||||
|
|
||||||
|
path = var.config.get('bot', 'tmp_folder') + url_hash + ".%(ext)s"
|
||||||
|
mp3 = path.replace(".%(ext)s", ".mp3")
|
||||||
|
music['path'] = mp3
|
||||||
|
|
||||||
|
# Download only if music is not existed
|
||||||
|
if os.path.isfile(mp3):
|
||||||
|
logging.info("bot: file existed for url %s " % music['url'])
|
||||||
|
music['ready'] = 'yes'
|
||||||
|
return music
|
||||||
|
|
||||||
|
music = media.url.get_url_info(music)
|
||||||
|
|
||||||
|
logging.info("bot: verifying the duration of url %s " % music['url'])
|
||||||
|
|
||||||
|
if music:
|
||||||
|
if music['duration'] > var.config.getint('bot', 'max_track_duration'):
|
||||||
|
# Check the length, useful in case of playlist, it wasn't checked before)
|
||||||
|
logging.info(
|
||||||
|
"the music " + music["url"] + " has a duration of " + music['duration'] + "s -- too long")
|
||||||
|
self.send_msg(constants.strings('too_long'))
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
music['ready'] = "no"
|
||||||
|
|
||||||
|
return music
|
||||||
|
else:
|
||||||
|
logging.error("bot: error while fetching info from the URL")
|
||||||
|
self.send_msg(constants.strings('unable_download'))
|
||||||
|
return False
|
||||||
|
|
||||||
def download_music(self, index=-1):
|
def download_music(self, index=-1):
|
||||||
if index == -1:
|
if index == -1:
|
||||||
index = var.playlist.current_index
|
index = var.playlist.current_index
|
||||||
@ -386,26 +421,6 @@ class MumbleBot:
|
|||||||
|
|
||||||
# Download only if music is not existed
|
# Download only if music is not existed
|
||||||
if not os.path.isfile(mp3):
|
if not os.path.isfile(mp3):
|
||||||
if music['ready'] == "validation":
|
|
||||||
logging.info("bot: verifying the duration of url (%s) %s " % (music['title'], url))
|
|
||||||
|
|
||||||
if music:
|
|
||||||
if 'duration' not in music:
|
|
||||||
music = media.url.get_url_info(music)
|
|
||||||
|
|
||||||
if music['duration'] > var.config.getint('bot', 'max_track_duration'):
|
|
||||||
# Check the length, useful in case of playlist, it wasn't checked before)
|
|
||||||
logging.info(
|
|
||||||
"the music " + music["url"] + " has a duration of " + music['duration'] + "s -- too long")
|
|
||||||
self.send_msg(constants.strings('too_long'))
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
music['ready'] = "no"
|
|
||||||
else:
|
|
||||||
logging.error("bot: error while fetching info from the URL")
|
|
||||||
self.send_msg(constants.strings('unable_download'))
|
|
||||||
return False
|
|
||||||
|
|
||||||
# download the music
|
# download the music
|
||||||
music['ready'] = "downloading"
|
music['ready'] = "downloading"
|
||||||
|
|
||||||
@ -441,7 +456,7 @@ class MumbleBot:
|
|||||||
logging.info("bot: music file existed, skip downloading " + mp3)
|
logging.info("bot: music file existed, skip downloading " + mp3)
|
||||||
music['ready'] = "yes"
|
music['ready'] = "yes"
|
||||||
|
|
||||||
music = util.get_music_tag_info(music, music['path'])
|
music = util.get_music_tag_info(music)
|
||||||
|
|
||||||
var.playlist.update(music, index)
|
var.playlist.update(music, index)
|
||||||
return music
|
return music
|
||||||
@ -491,7 +506,7 @@ class MumbleBot:
|
|||||||
def async_download_next(self):
|
def async_download_next(self):
|
||||||
# Function start if the next music isn't ready
|
# Function start if the next music isn't ready
|
||||||
# Do nothing in case the next music is already downloaded
|
# Do nothing in case the next music is already downloaded
|
||||||
logging.info("bot: Async download next asked ")
|
logging.debug("bot: Async download next asked ")
|
||||||
if var.playlist.length() > 1 and var.playlist.next_item()['type'] == 'url' \
|
if var.playlist.length() > 1 and var.playlist.next_item()['type'] == 'url' \
|
||||||
and (var.playlist.next_item()['ready'] in ["no", "validation"]):
|
and (var.playlist.next_item()['ready'] in ["no", "validation"]):
|
||||||
th = threading.Thread(
|
th = threading.Thread(
|
||||||
@ -508,8 +523,8 @@ class MumbleBot:
|
|||||||
index = var.playlist.current_index
|
index = var.playlist.current_index
|
||||||
music = var.playlist.playlist[index]
|
music = var.playlist.playlist[index]
|
||||||
|
|
||||||
# if music['type'] == 'radio':
|
if music['type'] == 'radio':
|
||||||
# return True
|
return True
|
||||||
|
|
||||||
if not 'path' in music:
|
if not 'path' in music:
|
||||||
return False
|
return False
|
||||||
|
15
util.py
15
util.py
@ -45,12 +45,21 @@ def get_recursive_filelist_sorted(path):
|
|||||||
filelist.sort()
|
filelist.sort()
|
||||||
return filelist
|
return filelist
|
||||||
|
|
||||||
|
def get_music_path(music):
|
||||||
|
uri = ''
|
||||||
|
if music["type"] == "url":
|
||||||
|
uri = music['path']
|
||||||
|
elif music["type"] == "file":
|
||||||
|
uri = var.config.get('bot', 'music_folder') + music["path"]
|
||||||
|
elif music["type"] == "radio":
|
||||||
|
uri = music['url']
|
||||||
|
|
||||||
def get_music_tag_info(music, uri = ""):
|
return uri
|
||||||
|
|
||||||
|
|
||||||
|
def get_music_tag_info(music):
|
||||||
if "path" in music:
|
if "path" in music:
|
||||||
if not uri:
|
uri = get_music_path(music)
|
||||||
uri = var.config.get('bot', 'music_folder') + music["path"]
|
|
||||||
|
|
||||||
if os.path.isfile(uri):
|
if os.path.isfile(uri):
|
||||||
match = re.search("(.+)\.(.+)", uri)
|
match = re.search("(.+)\.(.+)", uri)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user