]> git.0d.be Git - botaradio.git/commitdiff
Cleanup
authorLartza <Lartza@users.noreply.github.com>
Tue, 10 Mar 2020 21:07:29 +0000 (23:07 +0200)
committerLartza <Lartza@users.noreply.github.com>
Tue, 10 Mar 2020 21:07:29 +0000 (23:07 +0200)
14 files changed:
command.py
constants.py
database.py
interface.py
librb/rbRadios.py
media/cache.py
media/file.py
media/item.py
media/playlist.py
media/radio.py
media/url.py
media/url_from_playlist.py
mumbleBot.py
util.py

index 979988403707abe2f36e08bf1c51e3fccbccabc1..725f0c8c29791525e165c01ad96bb10bf521422e 100644 (file)
@@ -1,6 +1,5 @@
 # coding=utf-8
 import logging
-import os.path
 import pymumble.pymumble_py3 as pymumble
 import re
 
@@ -10,12 +9,9 @@ import util
 import variables as var
 from librb import radiobrowser
 from database import SettingsDatabase, MusicDatabase
-from media.item import item_builders, item_loaders, item_id_generators, dict_to_item, dicts_to_items
+from media.item import item_id_generators, dict_to_item, dicts_to_items
 from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags
-from media.file import FileItem
-from media.url_from_playlist import PlaylistURLItem, get_playlist_info
-from media.url import URLItem
-from media.radio import RadioItem
+from media.url_from_playlist import get_playlist_info
 
 log = logging.getLogger("bot")
 
@@ -281,7 +277,6 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
 def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cache=False):
     global log
 
-    music_folder = var.music_folder
     if parameter:
         files = var.cache.files
         msgs = [constants.strings('multiple_file_added') + "<ul>"]
@@ -701,12 +696,10 @@ def cmd_remove(bot, user, text, command, parameter):
     global log
 
     # Allow to remove specific music into the queue with a number
-    if parameter and parameter.isdigit() and int(parameter) > 0 \
-            and int(parameter) <= len(var.playlist):
+    if parameter and parameter.isdigit() and 0 < int(parameter) <= len(var.playlist):
 
         index = int(parameter) - 1
 
-        removed = None
         if index == var.playlist.current_index:
             removed = var.playlist.remove(index)
 
@@ -766,7 +759,6 @@ def cmd_queue(bot, user, text, command, parameter):
     else:
         msgs = [constants.strings('queue_contents')]
         for i, music in enumerate(var.playlist):
-            newline = ''
             tags = ''
             if len(music.item().tags) > 0:
                 tags = "<sup>{}</sup>".format(", ".join(music.item().tags))
@@ -854,8 +846,6 @@ def cmd_add_tag(bot, user, text, command, parameter):
     global log
 
     params = parameter.split()
-    index = ""
-    tags = []
     if len(params) == 2:
         index = params[0]
         tags = list(map(lambda t: t.strip(), params[1].split(",")))
@@ -892,8 +882,6 @@ def cmd_remove_tag(bot, user, text, command, parameter):
 
     params = parameter.split()
 
-    index = ""
-    tags = []
     if len(params) == 2:
         index = params[0]
         tags = list(map(lambda t: t.strip(), params[1].split(",")))
@@ -1007,7 +995,6 @@ def cmd_search_library(bot, user, text, command, parameter):
 
 def cmd_shortlist(bot, user, text, command, parameter):
     global song_shortlist, log
-    indexes = []
     try:
         indexes = [int(i) for i in parameter.split(" ")]
     except ValueError:
@@ -1047,7 +1034,6 @@ def cmd_shortlist(bot, user, text, command, parameter):
 
 def cmd_delete_from_library(bot, user, text, command, parameter):
     global song_shortlist, log
-    indexes = []
     try:
         indexes = [int(i) for i in parameter.split(" ")]
     except ValueError:
index ed56effc6eadc326f5758df7df33dca28d73dc84..355112d1c00327ea3b74f882f0f152fd831b0f58 100644 (file)
@@ -2,33 +2,31 @@ import variables as var
 
 
 def strings(option, *argv, **kwargs):
-    string = ""
     try:
         string = var.config.get("strings", option)
-    except KeyError as e:
-        raise KeyError("Missed strings in configuration file: '{string}'. ".format(string=option) +
-                       "Please restore you configuration file back to default if necessary.")
+    except KeyError:
+        raise KeyError("Missed strings in configuration file: '{string}'. ".format(string=option)
+                       "Please restore you configuration file back to default if necessary.")
     if argv or kwargs:
         try:
             formatted = string.format(*argv, **kwargs)
             return formatted
         except KeyError as e:
             raise KeyError(
-                "Missed/Unexpected placeholder {{{placeholder}}} in string '{string}'. ".format(placeholder=str(e).strip("'"), string=option) +
-                "Please restore you configuration file back to default if necessary.")
-        except TypeError as e:
+                "Missed/Unexpected placeholder {{{placeholder}}} in string '{string}'. ".format(placeholder=str(e).strip("'"), string=option)
+                "Please restore you configuration file back to default if necessary.")
+        except TypeError:
             raise KeyError(
-                "Missed placeholder in string '{string}'. ".format(string=option) +
-                "Please restore you configuration file back to default if necessary.")
+                "Missed placeholder in string '{string}'. ".format(string=option)
+                "Please restore you configuration file back to default if necessary.")
     else:
         return string
 
 
 def commands(command):
-    string = ""
     try:
         string = var.config.get("commands", command)
         return string
-    except KeyError as e:
-        raise KeyError("Missed command in configuration file: '{string}'. ".format(string=command) +
-                       "Please restore you configuration file back to default if necessary.")
+    except KeyError:
+        raise KeyError("Missed command in configuration file: '{string}'. ".format(string=command)
+                       "Please restore you configuration file back to default if necessary.")
index 4ba493fbe39e681b334e0d9c9ad7352ce158ae19..c4815a1da50435f5ceb08216273e23e2b8589391 100644 (file)
@@ -15,7 +15,6 @@ class SettingsDatabase:
 
         # connect
         conn = sqlite3.connect(self.db_path)
-        cursor = conn.cursor()
 
         self.db_version_check_and_create()
 
index fcef7d72f22ac5037e90a716eec497a9b46513fe..60220727af9cafabd6988db6022da312fabe0037 100644 (file)
@@ -16,7 +16,7 @@ import time
 
 
 class ReverseProxied(object):
-    '''Wrap the application in this middleware and configure the
+    """Wrap the application in this middleware and configure the
     front-end server to add these headers, to let you quietly bind
     this to a URL other than / and to an HTTP scheme that is
     different than what is used locally.
@@ -31,7 +31,7 @@ class ReverseProxied(object):
         }
 
     :param app: the WSGI application
-    '''
+    """
 
     def __init__(self, app):
         self.app = app
index 2350baaa8315fc9ff3b48ccbb4aba4a6e493baf0..09baf1aeb2d88a23712b10bac2ece7bf2c1a166e 100644 (file)
@@ -1,8 +1,5 @@
 import requests
 
-from xml.etree import ElementTree
-from urllib.parse import urljoin
-
 from librb.rbConstants import endpoints, BASE_URL
 
 
index b4ca2745cdbb0126e9805730cff072a3a3f50972..1373258285c79e3744a3f288e0a1d15c1e39671a 100644 (file)
@@ -1,11 +1,10 @@
 import logging
 import os
 
-from database import MusicDatabase
 import json
 import threading
 
-from media.item import item_builders, item_loaders, item_id_generators, dict_to_item, dicts_to_items
+from media.item import item_builders, item_id_generators, dict_to_item
 from database import MusicDatabase
 import variables as var
 import util
@@ -18,6 +17,7 @@ class MusicCache(dict):
         self.log = logging.getLogger("bot")
         self.dir = None
         self.files = []
+        self.file_id_lookup = {}
         self.dir_lock = threading.Lock()
 
     def get_item_by_id(self, bot, id):  # Why all these functions need a bot? Because it need the bot to send message!
@@ -111,7 +111,6 @@ class MusicCache(dict):
         self.dir_lock.acquire()
         self.log.info("library: rebuild directory cache")
         self.files = []
-        self.file_id_lookup = {}
         files = util.get_recursive_file_list_sorted(var.music_folder)
         self.dir = util.Dir(var.music_folder)
         for file in files:
index 6b189816ceb711e5ce357cd84088a3005813c201..4718ea38036a8658f5c473273e852a291968eb59 100644 (file)
@@ -1,4 +1,3 @@
-import logging
 import os
 import re
 from io import BytesIO
@@ -6,9 +5,7 @@ import base64
 import hashlib
 import mutagen
 from PIL import Image
-import json
 
-import util
 import variables as var
 from media.item import BaseItem, item_builders, item_loaders, item_id_generators
 import constants
@@ -83,7 +80,7 @@ class FileItem(BaseItem):
         return True
 
     def _get_info_from_tag(self):
-        match = re.search("(.+)\.(.+)", self.uri())
+        match = re.search(r"(.+)\.(.+)", self.uri())
         assert match is not None
 
         file_no_ext = match[1]
index 4f28f4f04e1f86aff2499d7dfd586c242fb1014f..bd09a81dded131b76a8e085bd8ba3bed0edf326e 100644 (file)
@@ -1,15 +1,4 @@
 import logging
-import threading
-import os
-import re
-from io import BytesIO
-import base64
-import hashlib
-import mutagen
-from PIL import Image
-
-import util
-import variables as var
 
 item_builders = {}
 item_loaders = {}
@@ -117,5 +106,3 @@ class BaseItem:
 
     def to_dict(self):
         return {"type": self.type, "id": self.id, "ready": self.ready, "path": self.path, "tags": self.tags}
-
-
index 152906b92d9fb591bb017fe3a4870b2aa2646383..d078fa368fc139efa66cd792810e337d409d1443 100644 (file)
@@ -359,4 +359,3 @@ class AutoPlaylist(OneshotPlaylist):
         if len(self) == 0:
             self.refresh()
         return super().next()
-
index 54e445abd35533c77605c96b5442acedfea445ba..65b1c597656c7702145ffe2e2e69a76058ed02d6 100644 (file)
@@ -16,12 +16,11 @@ def get_radio_server_description(url):
     global log
 
     log.debug("radio: fetching radio server description")
-    p = re.compile('(https?\:\/\/[^\/]*)', re.IGNORECASE)
+    p = re.compile('(https?://[^/]*)', re.IGNORECASE)
     res = re.search(p, url)
     base_url = res.group(1)
     url_icecast = base_url + '/status-json.xsl'
     url_shoutcast = base_url + '/stats?json=1'
-    title_server = None
     try:
         r = requests.get(url_shoutcast, timeout=10)
         data = r.json()
@@ -31,7 +30,7 @@ def get_radio_server_description(url):
     except (requests.exceptions.ConnectionError,
             requests.exceptions.HTTPError,
             requests.exceptions.ReadTimeout,
-            requests.exceptions.Timeout) as e:
+            requests.exceptions.Timeout):
         error_traceback = traceback.format_exc()
         error = error_traceback.rstrip().split("\n")[-1]
         log.debug("radio: unsuccessful attempts on fetching radio description (shoutcast): " + error)
@@ -52,7 +51,7 @@ def get_radio_server_description(url):
     except (requests.exceptions.ConnectionError,
             requests.exceptions.HTTPError,
             requests.exceptions.ReadTimeout,
-            requests.exceptions.Timeout) as e:
+            requests.exceptions.Timeout):
         error_traceback = traceback.format_exc()
         error = error_traceback.rstrip().split("\n")[-1]
         log.debug("radio: unsuccessful attempts on fetching radio description (icecast): " + error)
@@ -82,7 +81,7 @@ def get_radio_title(url):
             requests.exceptions.HTTPError,
             requests.exceptions.ReadTimeout,
             requests.exceptions.Timeout,
-            KeyError) as e:
+            KeyError):
         log.debug("radio: unsuccessful attempts on fetching radio title (icy)")
     return url
 
@@ -163,6 +162,3 @@ class RadioItem(BaseItem):
 
     def display_type(self):
         return constants.strings("radio")
-
-
-
index 2d342f2ddf581b117289cef5d57750eef2bdb681..f5f416314bd56aa589aa3b0a233646c279faf4d5 100644 (file)
@@ -149,8 +149,6 @@ class URLItem(BaseItem):
         self.ready = "preparing"
 
         self.log.info("bot: downloading url (%s) %s " % (self.title, self.url))
-        ydl_opts = ""
-
         ydl_opts = {
             'format': 'bestaudio/best',
             'outtmpl': save_path,
@@ -170,7 +168,7 @@ class URLItem(BaseItem):
             for i in range(attempts):
                 self.log.info("bot: download attempts %d / %d" % (i+1, attempts))
                 try:
-                    info = ydl.extract_info(self.url)
+                    ydl.extract_info(self.url)
                     download_succeed = True
                     break
                 except:
index 8559743f1b60552db83d0cbd0a04da63ac56d508..99b60076139ac6fb9c513df8d2e9faaa23784cdb 100644 (file)
@@ -1,8 +1,6 @@
 import youtube_dl
 import constants
-import media
 import variables as var
-import hashlib
 from media.item import item_builders, item_loaders, item_id_generators
 from media.url import URLItem, url_item_id_generator
 
index c9f0102afa40220820c1ad87de415d386aef492c..ec63bb419f72929179f68b4f65ce6cfb5cc6fc8a 100644 (file)
@@ -14,8 +14,6 @@ import os
 import os.path
 import pymumble.pymumble_py3 as pymumble
 import variables as var
-import hashlib
-import youtube_dl
 import logging
 import logging.handlers
 import traceback
@@ -25,9 +23,6 @@ import util
 import command
 import constants
 from database import SettingsDatabase, MusicDatabase
-import media.url
-import media.file
-import media.radio
 import media.system
 from media.playlist import BasePlaylist
 from media.cache import MusicCache
@@ -585,7 +580,6 @@ def start_web_interface(addr, port):
     # setup logger
     werkzeug_logger = logging.getLogger('werkzeug')
     logfile = util.solve_filepath(var.config.get('webinterface', 'web_logfile'))
-    handler = None
     if logfile:
         handler = logging.handlers.RotatingFileHandler(logfile, mode='a', maxBytes=10240)  # Rotate after 10KB
     else:
diff --git a/util.py b/util.py
index 56d699e232178a621389208f799406e6e15cfe19..af763d6ec61499adb47e8b7887e4c7d904700ebd 100644 (file)
--- a/util.py
+++ b/util.py
@@ -6,23 +6,16 @@ import magic
 import os
 import sys
 import variables as var
-import constants
 import zipfile
 import requests
-import mutagen
 import re
 import subprocess as sp
 import logging
 import youtube_dl
 from importlib import reload
-from PIL import Image
-from io import BytesIO
 from sys import platform
 import traceback
 import urllib.request
-import base64
-import media
-import media.radio
 from packaging import version
 
 log = logging.getLogger("bot")
@@ -173,7 +166,7 @@ def url_unban(url):
 
 
 def pipe_no_wait(pipefd):
-    ''' Used to fetch the STDERR of ffmpeg. pipefd is the file descriptor returned from os.pipe()'''
+    """ Used to fetch the STDERR of ffmpeg. pipefd is the file descriptor returned from os.pipe()"""
     if platform == "linux" or platform == "linux2" or platform == "darwin":
         import fcntl
         import os
@@ -316,13 +309,13 @@ def youtube_search(query):
 
     try:
         r = requests.get("https://www.youtube.com/results", params={'search_query': query}, timeout=5)
-        results = re.findall("watch\?v=(.*?)\".*?title=\"(.*?)\".*?"
+        results = re.findall(r"watch\?v=(.*?)\".*?title=\"(.*?)\".*?"
                              "(?:user|channel).*?>(.*?)<", r.text)  # (id, title, uploader)
 
         if len(results) > 0:
             return results
 
-    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
+    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout):
         error_traceback = traceback.format_exc().split("During")[0]
         log.error("util: youtube query failed with error:\n %s" % error_traceback)
         return False