]> git.0d.be Git - botaradio.git/commitdiff
refactor: avoid use youtube-dl if music is existed locally. #78
authorTerry Geng <gengyanda@gmail.com>
Wed, 26 Feb 2020 09:46:14 +0000 (17:46 +0800)
committerTerry Geng <gengyanda@gmail.com>
Wed, 26 Feb 2020 09:46:14 +0000 (17:46 +0800)
command.py
interface.py
mumbleBot.py
util.py

index 16ca8f9da687a18f57df7e17a802931e5a329d7d..a46ad021e7e25c80a00732e819276a26b90e1ef2 100644 (file)
@@ -250,16 +250,14 @@ def cmd_play_url(bot, user, text, command, parameter):
              'user': user,
              'ready': 'validation'}
 
-    if media.url.get_url_info(music):
-        if music['duration'] > var.config.getint('bot', 'max_track_duration'):
-            bot.send_msg(constants.strings('too_long'), text)
-        else:
-            music['ready'] = "no"
-            var.playlist.append(music)
-            logging.info("cmd: add to playlist: " + music['url'])
-            if var.playlist.length() == 2:
-                # If I am the second item on the playlist. (I am the next one!)
-                bot.async_download_next()
+    music = bot.validate_music(music)
+    if music:
+        var.playlist.append(music)
+        logging.info("cmd: add to playlist: " + music['url'])
+        bot.send_msg(constants.strings('file_added', item=util.format_song_string(music)), text)
+        if var.playlist.length() == 2:
+            # If I am the second item on the playlist. (I am the next one!)
+            bot.async_download_next()
     else:
         bot.send_msg(constants.strings('unable_download'), text)
 
index 77630958769858832d2bb27adf134e2ef0c04bdb..bc091910bdac50a0d32e601b6e5363613debbe3e 100644 (file)
@@ -201,10 +201,13 @@ def post():
                                  'url': request.form['add_url'],
                                  'user': 'Web',
                                  'ready': 'validation'}
-            media.url.get_url_info(music)
-            music = var.playlist.append(music)
-            logging.info("web: add to playlist: " + util.format_debug_song_string(music))
-            var.playlist.playlist[-1]['ready'] = "no"
+            music = var.botamusique.validate_music(music)
+            if music:
+                var.playlist.append(music)
+                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:
             music = var.playlist.append({'type': 'radio',
index 0df853ebc595c000d4fbc5c3d2485e0cfa716b83..3cb17f50a1a6223f445f6f83f4af15605b401407 100644 (file)
@@ -367,6 +367,41 @@ class MumbleBot:
         self.playhead = 0
         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):
         if index == -1:
             index = var.playlist.current_index
@@ -386,26 +421,6 @@ class MumbleBot:
 
         # Download only if music is not existed
         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
             music['ready'] = "downloading"
 
@@ -441,7 +456,7 @@ class MumbleBot:
             logging.info("bot: music file existed, skip downloading " + mp3)
             music['ready'] = "yes"
 
-        music = util.get_music_tag_info(music, music['path'])
+        music = util.get_music_tag_info(music)
 
         var.playlist.update(music, index)
         return music
@@ -491,7 +506,7 @@ class MumbleBot:
     def async_download_next(self):
         # Function start if the next music isn't ready
         # 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' \
                 and (var.playlist.next_item()['ready'] in ["no", "validation"]):
             th = threading.Thread(
@@ -508,8 +523,8 @@ class MumbleBot:
             index = var.playlist.current_index
         music = var.playlist.playlist[index]
 
-        if music['type'] == 'radio':
-            return True
+        if music['type'] == 'radio':
+            return True
 
         if not 'path' in music:
             return False
diff --git a/util.py b/util.py
index b0bf32ea09879c891a930f42605284604e0b3046..64a63da3ab7acbc3cc99ec1dac57667b2f6d0d4f 100644 (file)
--- a/util.py
+++ b/util.py
@@ -45,12 +45,21 @@ def get_recursive_filelist_sorted(path):
     filelist.sort()
     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']
+
+    return uri
 
-def get_music_tag_info(music, uri = ""):
 
+def get_music_tag_info(music):
     if "path" in music:
-        if not uri:
-            uri = var.config.get('bot', 'music_folder') + music["path"]
+        uri = get_music_path(music)
 
         if os.path.isfile(uri):
             match = re.search("(.+)\.(.+)", uri)