Browse Source

don't use singletons as global variables, they might not be initialized yet

Helmut Pozimski 6 years ago
parent
commit
2e4b966cbe
2 changed files with 85 additions and 64 deletions
  1. 65 46
      lib_stov/program.py
  2. 20 18
      lib_stov/youtubedl_wrapper.py

+ 65 - 46
lib_stov/program.py

@@ -33,8 +33,6 @@ from lib_stov.database import Db
 from lib_stov.configuration import Conf
 
 LOGGER = logging.getLogger("stov")
-DATABASE = Db.get_instance()
-CONFIGURATION = Conf.get_instance()
 
 
 def add_subscription(channel="", search="", playlist="", site="youtube"):
@@ -50,6 +48,7 @@ def add_subscription(channel="", search="", playlist="", site="youtube"):
     :param playlist: optional playlist ID
     :type playlist: str
     """
+    database = Db.get_instance()
     LOGGER.debug(_("Creating new subscription with the following "
                    "parameters:\nChannel: %s\nSearch: %s\nPlaylist: %s"),
                  channel, search, playlist)
@@ -80,12 +79,12 @@ def add_subscription(channel="", search="", playlist="", site="youtube"):
         sys.exit(1)
     try:
         subscription_data = new_subscription.add_sub()
-        site_id = DATABASE.get_site_id(subscription_data[6])
+        site_id = database.get_site_id(subscription_data[6])
         new_sub_data = (subscription_data[0], subscription_data[1],
                         subscription_data[2], subscription_data[3],
                         subscription_data[4], subscription_data[5],
                         site_id)
-        subscription_id = DATABASE.insert_subscription(new_sub_data)
+        subscription_id = database.insert_subscription(new_sub_data)
         new_subscription.set_id(subscription_id)
     except stov_exceptions.DBWriteAccessFailedException as error:
         LOGGER.error(error)
@@ -102,10 +101,10 @@ def add_subscription(channel="", search="", playlist="", site="youtube"):
     except stov_exceptions.NoDataFromYoutubeAPIException as error:
         LOGGER.error(error)
     for video in new_subscription.parsed_response.videos:
-        if not DATABASE.video_in_database(video.video_id):
+        if not database.video_in_database(video.video_id):
             if new_subscription.check_string_match(video):
                 try:
-                    DATABASE.insert_video(video, new_subscription.get_id())
+                    database.insert_video(video, new_subscription.get_id())
                 except stov_exceptions.DBWriteAccessFailedException as error:
                     LOGGER.error(error)
                     sys.exit(1)
@@ -121,7 +120,9 @@ def list_subscriptions():
     Prints a list of subscriptions from the database.
 
     """
-    subscriptions_list = DATABASE.get_subscriptions(CONFIGURATION)
+    database = Db.get_instance()
+    conf = Conf.get_instance()
+    subscriptions_list = database.get_subscriptions(conf)
     sub_state = None
     if subscriptions_list:
         LOGGER.info(_("ID Title Site"))
@@ -143,8 +144,9 @@ def delete_subscription(sub_id):
     :param sub_id: ID of the subscription to be deleted
     :type sub_id: int
     """
+    database = Db.get_instance()
     try:
-        DATABASE.delete_subscription(sub_id)
+        database.delete_subscription(sub_id)
     except stov_exceptions.SubscriptionNotFoundException as error:
         LOGGER.error(error)
         sys.exit(1)
@@ -162,10 +164,12 @@ def update_subscriptions(subscriptions=None):
     :param subscriptions: list of subscriptions to update
     :type subscriptions: list
     """
+    database = Db.get_instance()
+    conf = Conf.get_instance()
     subscriptions_list = get_subscriptions(subscriptions)
     for element in subscriptions_list:
         LOGGER.debug(_("Updating subscription %s"), element.get_title())
-        videos = DATABASE.get_videos(element.get_id(), CONFIGURATION)
+        videos = database.get_videos(element.get_id(), conf)
         element.gather_videos(videos)
         try:
             element.update_data()
@@ -174,10 +178,10 @@ def update_subscriptions(subscriptions=None):
         except stov_exceptions.NoDataFromYoutubeAPIException as error:
             LOGGER.error(error)
         for video in element.parsed_response.videos:
-            if not DATABASE.video_in_database(video.video_id):
+            if not database.video_in_database(video.video_id):
                 if element.check_string_match(video):
                     try:
-                        DATABASE.insert_video(video, element.get_id())
+                        database.insert_video(video, element.get_id())
                     except stov_exceptions.DBWriteAccessFailedException as \
                             error:
                         LOGGER.error(error)
@@ -197,27 +201,29 @@ def download_videos(subscriptions=None):
     videos and a list of the videos downloaded
     :rtype: tuple
     """
+    database = Db.get_instance()
+    conf = Conf.get_instance()
     video_titles = []
     subscriptions_list = get_subscriptions(subscriptions)
     videos_downloaded = 0
     videos_failed = 0
     for sub in subscriptions_list:
-        videos = DATABASE.get_videos(sub.get_id(), CONFIGURATION)
+        videos = database.get_videos(sub.get_id(), conf)
         sub.gather_videos(videos)
         try:
             sub.download_videos()
         except stov_exceptions.SubscriptionDisabledException as error:
             LOGGER.debug(error)
         for entry in sub.downloaded_videos:
-            DATABASE.update_video_download_status(entry.get_id(), 1)
+            database.update_video_download_status(entry.get_id(), 1)
             video_titles.append(entry.title)
         videos_downloaded = len(video_titles)
         videos_failed = videos_failed + sub.failed_videos_count
         for video in sub.failed_videos:
             try:
-                DATABASE.update_video_fail_count(video.failcnt, video.get_id())
-                if video.failcnt >= int(CONFIGURATION.values["maxfails"]):
-                    DATABASE.disable_failed_video(video.get_id())
+                database.update_video_fail_count(video.failcnt, video.get_id())
+                if video.failcnt >= int(conf.values["maxfails"]):
+                    database.disable_failed_video(video.get_id())
             except stov_exceptions.DBWriteAccessFailedException as error:
                 LOGGER.error(error)
                 sys.exit(1)
@@ -235,6 +241,7 @@ def compose_email(downloaded_videos, video_titles):
     :return: e-mail contents
     :rtype: MIMEMultipart
     """
+    conf = Conf.get_instance()
     mail_text = ""
     msg = MIMEMultipart()
     if downloaded_videos == 1:
@@ -245,8 +252,8 @@ def compose_email(downloaded_videos, video_titles):
         msg["Subject"] = _("Downloaded %i new videos") % downloaded_videos
         mail_text = _("The following episodes have been downloaded by "
                       "stov: \n\n")
-    msg["From"] = "stov <%s>" % CONFIGURATION.values["mailfrom"]
-    msg["To"] = "<%s>" % CONFIGURATION.values["mailto"]
+    msg["From"] = "stov <%s>" % conf.values["mailfrom"]
+    msg["To"] = "<%s>" % conf.values["mailto"]
     for line in video_titles:
         mail_text += line + "\n"
     msg_text = MIMEText(mail_text.encode("utf8"), _charset="utf8")
@@ -261,8 +268,9 @@ def send_email(msg):
     :param msg: message to be sent
     :type msg: MIMEMultipart
     """
-    server_connection = smtplib.SMTP(CONFIGURATION.values["mailhost"],
-                                     CONFIGURATION.values["smtpport"])
+    conf = Conf.get_instance()
+    server_connection = smtplib.SMTP(conf.values["mailhost"],
+                                     conf.values["smtpport"])
     try:
         server_connection.connect()
     except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
@@ -274,10 +282,10 @@ def send_email(msg):
             server_connection.starttls()
         except smtplib.SMTPException:
             LOGGER.debug(_("TLS not available, proceeding unencrypted."))
-        if CONFIGURATION.values["auth_needed"] == "yes":
+        if conf.values["auth_needed"] == "yes":
             try:
-                server_connection.login(CONFIGURATION.values["user_name"],
-                                        CONFIGURATION.values["password"])
+                server_connection.login(conf.values["user_name"],
+                                        conf.values["password"])
             except smtplib.SMTPAuthenticationError:
                 LOGGER.error(_("Authentication failed, please check user "
                                "name and password!"))
@@ -285,8 +293,8 @@ def send_email(msg):
                 LOGGER.error(_("Could not authenticate, server probably "
                                "does not support authentication!"))
         try:
-            server_connection.sendmail(CONFIGURATION.values["mailfrom"],
-                                       CONFIGURATION.values["mailto"],
+            server_connection.sendmail(conf.values["mailfrom"],
+                                       conf.values["mailto"],
                                        msg.as_string())
         except smtplib.SMTPRecipientsRefused:
             LOGGER.error(_("The server refused the recipient address, "
@@ -304,8 +312,10 @@ def list_videos(sub_id):
     :param sub_id: ID of the subscription
     :type sub_id: int
     """
+    database = Db.get_instance()
+    conf = Conf.get_instance()
     try:
-        data = DATABASE.get_subscription(sub_id)
+        data = database.get_subscription(sub_id)
     except stov_exceptions.DBWriteAccessFailedException as error:
         LOGGER.error(error)
         sys.exit(1)
@@ -317,7 +327,7 @@ def list_videos(sub_id):
                                    subscription_id=data[0][0],
                                    title=data[0][1], directory=data[0][5],
                                    disabled=data[0][6])
-            videos = DATABASE.get_videos(sub.get_id(), CONFIGURATION)
+            videos = database.get_videos(sub.get_id(), conf)
             sub.gather_videos(videos)
             videos_list = sub.print_videos()
             for video in videos_list:
@@ -334,15 +344,16 @@ def catchup(sub_id):
     :param sub_id: ID of the subscription
     :type sub_id: int
     """
+    database = Db.get_instance()
     try:
-        sub_data = DATABASE.get_subscription_title(sub_id)
+        sub_data = database.get_subscription_title(sub_id)
     except stov_exceptions.DBWriteAccessFailedException as error:
         LOGGER.error(error)
         sys.exit(1)
     else:
         if sub_data:
             try:
-                DATABASE.mark_video_downloaded(sub_id)
+                database.mark_video_downloaded(sub_id)
             except stov_exceptions.DBWriteAccessFailedException as error:
                 LOGGER.error(error)
         else:
@@ -356,20 +367,22 @@ def clean_database():
     in the scope of the query and vacuuming the database to free up space.
 
     """
-    subscription_list = DATABASE.get_subscriptions(CONFIGURATION)
+    database = Db.get_instance()
+    conf = Conf.get_instance()
+    subscription_list = database.get_subscriptions(conf)
     for element in subscription_list:
-        videos = DATABASE.get_videos(element.get_id(), CONFIGURATION)
+        videos = database.get_videos(element.get_id(), conf)
         element.check_and_delete(videos)
         for delete_video in element.to_delete:
             LOGGER.debug(_("Deleting video %s from "
                            "database"), delete_video.title)
             try:
-                DATABASE.delete_video(delete_video.get_id())
+                database.delete_video(delete_video.get_id())
             except stov_exceptions.DBWriteAccessFailedException as error:
                 LOGGER.error(error)
                 sys.exit(1)
     try:
-        DATABASE.vacuum()
+        database.vacuum()
     except stov_exceptions.DBWriteAccessFailedException as error:
         LOGGER.error(error)
         sys.exit(1)
@@ -384,7 +397,8 @@ def change_subscription_state(sub_id, enable=False):
     :param enable: whether to enable or disable the subscription
     :type enable: bool
     """
-    subscription_state = DATABASE.get_subscription(sub_id)
+    database = Db.get_instance()
+    subscription_state = database.get_subscription(sub_id)
     try:
         if enable:
             if int(subscription_state[0][6]) == 0:
@@ -392,7 +406,7 @@ def change_subscription_state(sub_id, enable=False):
                              sub_id)
             elif int(subscription_state[0][6]) == 1:
                 try:
-                    DATABASE.change_subscription_state(sub_id, 0)
+                    database.change_subscription_state(sub_id, 0)
                 except stov_exceptions.DBWriteAccessFailedException as error:
                     LOGGER.error(error)
                     sys.exit(1)
@@ -404,7 +418,7 @@ def change_subscription_state(sub_id, enable=False):
                              sub_id)
             elif int(subscription_state[0][6]) == 0:
                 try:
-                    DATABASE.change_subscription_state(sub_id, 1)
+                    database.change_subscription_state(sub_id, 1)
                 except stov_exceptions.DBWriteAccessFailedException as error:
                     LOGGER.error(error)
                     sys.exit(1)
@@ -441,21 +455,22 @@ def download_notify(subscriptions=None):
     :param subscriptions: list of subscriptions to consider for downloading
     :type subscriptions: list
     """
+    conf = Conf.get_instance()
     videos_downloaded, videos_failed, video_titles = \
         download_videos(subscriptions)
-    if videos_downloaded > 0 and CONFIGURATION.values["notify"] == "yes":
+    if videos_downloaded > 0 and conf.values["notify"] == "yes":
         msg = compose_email(videos_downloaded, video_titles)
         send_email(msg)
     elif videos_downloaded == 0 and videos_failed == 0:
-        if CONFIGURATION.values["notify"] == "no":
+        if conf.values["notify"] == "no":
             LOGGER.info(_("There are no videos to be downloaded."))
-    elif CONFIGURATION.values["notify"] == "no":
+    elif conf.values["notify"] == "no":
         if videos_failed == 0:
             LOGGER.info(_("The following videos have been downloaded:\n"))
             for i in video_titles:
                 LOGGER.info(i)
     else:
-        if CONFIGURATION.values["notify"] != "yes":
+        if conf.values["notify"] != "yes":
             LOGGER.error(_("Could not determine how you want to be informed "
                            "about new videos, please check the notify "
                            "parameter in your configuration."))
@@ -466,18 +481,19 @@ def initialize_sites():
     Adds sites to the database if they are not in there yet.
 
     """
+    database = Db.get_instance()
     supported_sites = ["youtube", "zdf_mediathek", "twitch"]
-    sites = DATABASE.get_sites()
+    sites = database.get_sites()
     for site in supported_sites:
         site_found = False
         for result in sites:
             if site in result:
                 site_found = True
         if not site_found:
-            DATABASE.add_site(site)
+            database.add_site(site)
     for site in sites:
         if site[1] not in supported_sites:
-            DATABASE.remove_site(site[1])
+            database.remove_site(site[1])
 
 
 def list_sites():
@@ -485,7 +501,8 @@ def list_sites():
     Lists the currently supported sites.
 
     """
-    sites = DATABASE.get_sites()
+    database = Db.get_instance()
+    sites = database.get_sites()
     LOGGER.info(_("Sites currently supported by stov:"))
     for entry in sites:
         LOGGER.info(entry[1])
@@ -501,10 +518,12 @@ def get_subscriptions(subscriptions=None):
     :return: list of subscription objects
     :rtype: list
     """
+    database = Db.get_instance()
+    conf = Conf.get_instance()
     if subscriptions:
         subscriptions_list = []
         for element in subscriptions:
-            data = DATABASE.get_subscription(element)
+            data = database.get_subscription(element)
             if data:
                 sub = subscription.Sub(subscription_type=data[0][2],
                                        name=data[0][3], site=data[0][7],
@@ -518,5 +537,5 @@ def get_subscriptions(subscriptions=None):
                     _("Invalid subscription, please check the list and "
                       "try again."))
     else:
-        subscriptions_list = DATABASE.get_subscriptions(CONFIGURATION)
+        subscriptions_list = database.get_subscriptions(conf)
     return subscriptions_list

+ 20 - 18
lib_stov/youtubedl_wrapper.py

@@ -26,7 +26,6 @@ from lib_stov import stov_exceptions
 from lib_stov import configuration
 
 LOGGER = logging.getLogger("stov")
-CONFIGURATION = configuration.Conf.get_instance()
 
 
 def get_ids(url, reverse=False):
@@ -40,24 +39,25 @@ def get_ids(url, reverse=False):
     :return: video IDs
     :rtype: list
     """
+    conf = configuration.Conf.get_instance()
     videos_list = []
-    if CONFIGURATION.outputlevel == "verbose":
+    if conf.outputlevel == "verbose":
         stderr = sys.stderr
     else:
         stderr = open("/dev/null", "w")
     LOGGER.debug(_("Executing command: %s %s %s %s %s %s"),
-                 CONFIGURATION.values["youtube-dl"], "--max-downloads",
-                 CONFIGURATION.values["maxvideos"], "-i", "--get-id", url)
+                 conf.values["youtube-dl"], "--max-downloads",
+                 conf.values["maxvideos"], "-i", "--get-id", url)
     try:
         if reverse:
             video_ids = subprocess.check_output(
-                [CONFIGURATION.values["youtube-dl"], "--max-downloads",
-                 CONFIGURATION.values["maxvideos"], "-i", "--playlist-reverse",
+                [conf.values["youtube-dl"], "--max-downloads",
+                 conf.values["maxvideos"], "-i", "--playlist-reverse",
                  "--get-id", url], stderr=stderr)
         else:
             video_ids = subprocess.check_output(
-                [CONFIGURATION.values["youtube-dl"], "--max-downloads",
-                 CONFIGURATION.values["maxvideos"],
+                [conf.values["youtube-dl"], "--max-downloads",
+                 conf.values["maxvideos"],
                  "-i", "--get-id", url], stderr=stderr)
     except subprocess.CalledProcessError as error_message:
         video_ids = error_message.output
@@ -75,14 +75,15 @@ def get_title(url):
     :param url: URL to pass to youtube-dl
     :type url: str
     """
-    if CONFIGURATION.outputlevel == "verbose":
+    conf = configuration.Conf.get_instance()
+    if conf.outputlevel == "verbose":
         stderr = sys.stderr
     else:
         stderr = open("/dev/null", "w")
     LOGGER.debug(_("Executing command: %s %s %s"),
-                 CONFIGURATION.values["youtube-dl"], "--get-title", url)
+                 conf.values["youtube-dl"], "--get-title", url)
     video_title = subprocess.check_output([
-        CONFIGURATION.values["youtube-dl"], "--get-title", url], stderr=stderr)
+        conf.values["youtube-dl"], "--get-title", url], stderr=stderr)
     video_title = video_title.decode(sys.stdout.encoding)
     return video_title
 
@@ -96,22 +97,23 @@ def download_video(url, itag_value):
     :param url: URL to pass to youtube-dl
     :type url: str
     """
+    conf = configuration.Conf.get_instance()
     try:
         LOGGER.debug(_("Executing command: %s -f %s %s"),
-                     CONFIGURATION.values["youtube-dl"], itag_value, url)
-        if CONFIGURATION.outputlevel == "default":
-            subprocess.check_call([CONFIGURATION.values["youtube-dl"], "-f %s"
+                     conf.values["youtube-dl"], itag_value, url)
+        if conf.outputlevel == "default":
+            subprocess.check_call([conf.values["youtube-dl"], "-f %s"
                                    % itag_value,
                                    "-o", "%(title)s-%(id)s.%(ext)s", url],
                                   stderr=sys.stderr,
                                   stdout=open("/dev/null", "w"))
-        elif CONFIGURATION.outputlevel == "verbose":
-            subprocess.check_call([CONFIGURATION.values["youtube-dl"], "-f %s"
+        elif conf.outputlevel == "verbose":
+            subprocess.check_call([conf.values["youtube-dl"], "-f %s"
                                    % itag_value,
                                    "-o", "%(title)s-%(id)s.%(ext)s", url],
                                   stderr=sys.stderr, stdout=sys.stdout)
-        elif CONFIGURATION.outputlevel == "quiet":
-            subprocess.check_call([CONFIGURATION.values["youtube-dl"],
+        elif conf.outputlevel == "quiet":
+            subprocess.check_call([conf.values["youtube-dl"],
                                    "-f %s/%s" % itag_value,
                                    "-o", "%(title)s-%(id)s.%(ext)s", url],
                                   stderr=open("/dev/null", "w"),