Browse Source

implement execution of download and update for specific subscriptions (closes #12)

Helmut Pozimski 6 years ago
parent
commit
74b728576f
3 changed files with 56 additions and 12 deletions
  1. 3 2
      lib_stov/helpers.py
  2. 4 4
      lib_stov/main.py
  3. 49 6
      lib_stov/program.py

+ 3 - 2
lib_stov/helpers.py

@@ -100,11 +100,12 @@ def parse_arguments():
                         help=_("List the currently available subscriptions"))
     parser.add_argument("-r", "--remove", type=int, dest="deleteid",
                         help=_("remove a subscription"))
-    parser.add_argument("-u", "--update", action="store_true", dest="update",
+    parser.add_argument("-u", "--update", nargs="*", dest="update", type=int,
+                        default=None,
                         help=_(
                             "update the information about the available "
                             "videos"))
-    parser.add_argument("-d", "--download", action="store_true",
+    parser.add_argument("-d", "--download", nargs="*", type=int, default=None,
                         dest="download", help=_("download all available "
                                                 "videos which haven't already"
                                                 " been downloaded"))

+ 4 - 4
lib_stov/main.py

@@ -54,10 +54,10 @@ def main():
         program.list_subscriptions(conf, database)
     elif arguments.deleteid:
         program.delete_subscription(database, arguments.deleteid)
-    elif arguments.update:
-        program.update_subscriptions(database, conf)
-    elif arguments.download:
-        program.download_notify(database, conf)
+    elif arguments.update is not None:
+        program.update_subscriptions(database, conf, arguments.update)
+    elif arguments.download is not None:
+        program.download_notify(database, conf, arguments.download)
     elif arguments.subscriptionid:
         program.list_videos(database, conf, arguments.subscriptionid)
     elif arguments.catchup:

+ 49 - 6
lib_stov/program.py

@@ -166,7 +166,7 @@ def delete_subscription(database, sub_id):
         LOGGER.info(_("Subscription deleted successfully!"))
 
 
-def update_subscriptions(database, conf):
+def update_subscriptions(database, conf, subscriptions=None):
     """
     Updates data about videos in a subscription.
 
@@ -174,8 +174,10 @@ def update_subscriptions(database, conf):
     :type conf: lib_stov.configuration.Conf
     :param database: database object
     :type database: lib_stov.database.Db
+    :param subscriptions: list of subscriptions to update
+    :type subscriptions: list
     """
-    subscriptions_list = database.get_subscriptions(conf)
+    subscriptions_list = get_subscriptions(conf, database, subscriptions)
     for element in subscriptions_list:
         LOGGER.debug(_("Updating subscription %s"), element.get_title())
         videos = database.get_videos(element.get_id(), conf)
@@ -200,7 +202,7 @@ def update_subscriptions(database, conf):
                                        "database."), video.title)
 
 
-def download_videos(database, conf):
+def download_videos(database, conf, subscriptions=None):
     """
     Downloads videos that haven't been previously downloaded.
 
@@ -208,12 +210,14 @@ def download_videos(database, conf):
     :type conf: lib_stov.configuration.Conf
     :param database: database object
     :type database: lib_stov.database.Db
+    :param subscriptions: list of subscriptions to consider for downloading
+    :type subscriptions: list
     :return: tuple containing (in that order) downloaded videos, failed \
     videos and a list of the videos downloaded
     :rtype: tuple
     """
     video_titles = []
-    subscriptions_list = database.get_subscriptions(conf)
+    subscriptions_list = get_subscriptions(conf, database, subscriptions)
     videos_downloaded = 0
     videos_failed = 0
     for sub in subscriptions_list:
@@ -467,7 +471,7 @@ def print_license():
         along with stov.  If not, see <http://www.gnu.org/licenses/>.""")
 
 
-def download_notify(database, conf):
+def download_notify(database, conf, subscriptions=None):
     """
     starts an update of not yet downloaded videos and notifies the user
 
@@ -475,9 +479,11 @@ def download_notify(database, conf):
     :type database: lib_stov.database.Db
     :param conf: configuration object
     :type conf: lib_stov.configuration.Conf
+    :param subscriptions: list of subscriptions to consider for downloading
+    :type subscriptions: list
     """
     videos_downloaded, videos_failed, video_titles = \
-        download_videos(database, conf)
+        download_videos(database, conf, subscriptions)
     if videos_downloaded > 0 and conf.values["notify"] == "yes":
         msg = compose_email(conf, videos_downloaded, video_titles)
         send_email(conf, msg)
@@ -525,3 +531,40 @@ def list_sites(database):
     LOGGER.info(_("Sites currently supported by stov:"))
     for entry in sites:
         LOGGER.info(entry[1])
+
+
+def get_subscriptions(conf, database, subscriptions=None):
+    """
+    Retrieves all or only specific subscriptions from the database and
+    returns them as a list of subscription objects.
+
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    :param database: database object
+    :type database: lib_stov.database.Db
+    :param subscriptions: list of subscriptions to retrieve
+    :type subscriptions: list
+    :return: list of subscription objects
+    :rtype: list
+    """
+    if subscriptions:
+        subscriptions_list = []
+        for element in subscriptions:
+            data = database.get_subscription(element)
+            if data:
+                sub = subscription.Sub(subscription_id=data[0][0],
+                                       title=data[0][1],
+                                       subscription_type=data[0][2],
+                                       name=data[0][3],
+                                       search=data[0][4],
+                                       directory=data[0][5],
+                                       disabled=data[0][6],
+                                       site=data[0][7], conf=conf)
+                subscriptions_list.append(sub)
+            else:
+                LOGGER.error(
+                    _("Invalid subscription, please check the list and "
+                      "try again."))
+    else:
+        subscriptions_list = database.get_subscriptions(conf)
+    return subscriptions_list