Browse Source

refactored some database code to create the objects directly in the database class, further testing and bug fixing regarding the API-less mode

Helmut Pozimski 9 years ago
parent
commit
bb1007d459
6 changed files with 48 additions and 61 deletions
  1. 1 0
      TODO
  2. 23 5
      lib_stov/database.py
  3. 7 4
      lib_stov/noapi.py
  4. 1 1
      lib_stov/stov_exceptions.py
  5. 6 12
      lib_stov/subscription.py
  6. 10 39
      stov

+ 1 - 0
TODO

@@ -6,3 +6,4 @@ TODOs for 1.0:
 * Remove python 2 compatibility code, support python 3 only
 * Implement youtube-dl as alternative mode to API version 2
 * Check with pep8 and pylinth to do coding style improvements before the next release
+* Update man page

+ 23 - 5
lib_stov/database.py

@@ -20,6 +20,8 @@ from __future__ import unicode_literals
 import sqlite3
 
 from lib_stov import stov_exceptions
+from lib_stov import youtube
+from lib_stov import subscription
 
 
 class db(object):
@@ -115,16 +117,24 @@ class db(object):
             deletesubscription = "DELETE FROM subscriptions WHERE id=?"
             self.__ExecuteStatement(deletesubscription, (sub_id,))
 
-    def GetVideos(self, video_id):
+    def GetVideos(self, subscription_id, conf):
         """Gets all videos of a given subscription id from the database and
         returns them
 
         """
+        videos_list = []
         video_query = "SELECT id, title, description, ytid, downloaded, " \
                       "failcnt FROM videos WHERE subscription_id=?"
-        cursor = self.__ExecuteStatement(video_query, (video_id,))
+        cursor = self.__ExecuteStatement(video_query, (subscription_id,))
         result = cursor.fetchall()
-        return result
+        for video in result:
+            videos_list.append(youtube.video(id=video[0],title=video[1],
+                                             description=video[2],
+                                             ytid=video[3],
+                                             downloaded=video[4],
+                                             failcount=video[5],
+                                             conf=conf))
+        return videos_list
 
     def VideoInDatabase(self, ytid):
         """Checks if the video with a given youtube id already exists in the
@@ -203,13 +213,21 @@ class db(object):
                            "WHERE subscription_id =?"
         self.__ExecuteStatement(update_statement, (video_id,))
 
-    def GetSubscriptions(self):
+    def GetSubscriptions(self, conf):
         """Retrieves all subscriptions from the database"""
+        subscriptions_list = []
         subscriptions_query = "SELECT id,title,type,name,searchstring," \
                               "directory,disabled FROM subscriptions"
         result_cursor = self.__ExecuteStatement(subscriptions_query)
         result = result_cursor.fetchall()
-        return result
+        for sub in result:
+            subscriptions_list.append(subscription.sub(id=sub[0],title=sub[1],
+                                                       type=sub[2],name=sub[3],
+                                                       search=sub[4],
+                                                       directory=sub[5],
+                                                       disabled=sub[6],
+                                                       conf=conf))
+        return subscriptions_list
 
     def Vacuum(self):
         """Vacuums the database, shrinking it in size"""

+ 7 - 4
lib_stov/noapi.py

@@ -34,7 +34,7 @@ class Connector(object):
     """This class will retrieve all the necessary data from youtube using
     youtube-dl, thus bypassing the API.
     """
-    def __init__(self, type, name, conf, search = ""):
+    def __init__(self, type, name, conf, search=""):
         """Populates the object with all necessary data."""
         self._type = type
         self._name = name
@@ -53,18 +53,21 @@ class Connector(object):
                         % urllib2.quote(self._search)
         elif self._type == "playlist":
             self._url = "https://www.youtube.com/playlist?list=%s" \
-                        % urllib2.quote(self._search)
+                        % urllib2.quote(self._name)
 
     def _fetch_title(self):
         """Retrieves the title of the HTML page to use as a title for the
         subscription."""
         data = urllib2.urlopen(self._url)
         parsed_html = lxml.html.parse(data)
+        data.close()
         i = 0
         for item in parsed_html.iter("title"):
             if i == 0:
                 self._title = item.text_content().strip().replace("\n", "")
             i += 1
+        if self._search != "" and self._type == "channel":
+            self._title += " search %s" % self._search
 
     def _fetch_videos(self):
         """Retrieves all the relevant videos in a subscription."""
@@ -76,7 +79,7 @@ class Connector(object):
                                                                    "--match-title",
                                                                    self._search,
                                                                    "--get-id",
-                                                                   self._url])
+                                                                   self._url]).strip()
             except subprocess.CalledProcessError as e:
                 video_ids = e.output.strip()
         else:
@@ -84,7 +87,7 @@ class Connector(object):
                 video_ids = subprocess.check_output([self._conf.values["youtube-dl"], "--max-downloads",
                                                                    self._conf.values["maxvideos"],
                                                                    "--get-id",
-                                                                   self._url])
+                                                                   self._url]).strip()
             except subprocess.CalledProcessError as e:
                 video_ids = e.output.strip()
         for video_id in video_ids.split("\n"):

+ 1 - 1
lib_stov/stov_exceptions.py

@@ -182,7 +182,7 @@ class YoutubeDlCallFailed(Exception):
         of an error returned by youtube-dl.
     """
     def __init__(self):
-        self._message = _("Exectution of youtube-dl failed.")
+        self._message = _("Execution of youtube-dl failed.")
 
     def __str__(self):
         return self._message

+ 6 - 12
lib_stov/subscription.py

@@ -43,9 +43,9 @@ class sub(object):
         self.parsed_response = None
 
         if int(disabled) == 0:
-            self.__disabled = False
+            self.disabled = False
         elif int(disabled) == 1:
-            self.__disabled = True
+            self.disabled = True
 
         if self.__conf.values["use_api"]:
             self._connector = youtubeAPI.Connector(self.__type, self.__name,
@@ -66,7 +66,7 @@ class sub(object):
     def CheckStringMatch(self, video):
         """Checks if the subscription is enabled and the video matches the
         search string defined for the subscription"""
-        if self.__disabled is False:
+        if not self.disabled:
             if self.__search != "" and self.__conf.values["check_title"]\
                     == "yes":
                 if self.__search in video.title:
@@ -78,18 +78,12 @@ class sub(object):
         else:
                 return False
 
-    def GatherVideos(self, video_data):
+    def GatherVideos(self, video_list):
         """Gathers all videos in the subscription and saves
         them in an the internal list so they can be accessed by the object
 
         """
-        for i in video_data:
-            self.__video_list.append(youtube.video(id=i[0], title=i[1],
-                                                   description=i[2],
-                                                   ytid=i[3],
-                                                   downloaded=i[4],
-                                                   failcount=i[5],
-                                                   conf=self.__conf))
+        self.__video_list = video_list
 
     def DownloadVideos(self, itag_value):
         """Uses the DownloadVideo method of the video object to download all
@@ -97,7 +91,7 @@ class sub(object):
         downloaded videos if the download succeeds.
 
         """
-        if self.__disabled is False:
+        if not self.disabled:
             for video in self.__video_list:
                 if video.downloaded == 0:
                     if video.DownloadVideo(self.__directory, itag_value) is\

+ 10 - 39
stov

@@ -39,7 +39,6 @@ from lib_stov import subscription
 from lib_stov import configuration
 from lib_stov import stov_exceptions
 from lib_stov import database
-from lib_stov import noapi
 
 
 """Setup the logger to log messages to stdout and stderr"""
@@ -448,17 +447,16 @@ if options.add is True:
                 + _(" successfully added"))
 
 elif options.list is True:
-    Listofsubscriptions = db.GetSubscriptions()
+    Listofsubscriptions = db.GetSubscriptions(conf)
     sub_state = None
     if len(Listofsubscriptions) != 0:
         logger.info(_("ID Title"))
         for sub in Listofsubscriptions:
-            if int(sub[6]) == 0:
+            if not sub.disabled:
                 sub_state = _("enabled")
-            elif int(sub[6]) == 1:
+            elif sub.disabled:
                 sub_state = _("disabled")
-            if sub[0] is not None:
-                logger.info(str(sub[0]) + " " + sub[1]
+            logger.info(str(sub.GetId()) + " " + sub.GetTitle()
                             + " (%s)" % sub_state)
     else:
         logger.info(_("No subscriptions added yet, add one!"))
@@ -481,17 +479,7 @@ elif options.deleteid is not None:
         else:
             logger.info(_("Subscription deleted successfully!"))
 elif options.update is True:
-    listofsubscriptions = []
-    subscriptions = db.GetSubscriptions()
-    for element in subscriptions:
-        listofsubscriptions.append(subscription.sub(id=element[0],
-                                   title=element[1],
-                                   type=element[2],
-                                   name=element[3],
-                                   search=element[4],
-                                   directory=element[5],
-                                   disabled=element[6],
-                                   conf=conf))
+    listofsubscriptions = db.GetSubscriptions(conf)
     for element in listofsubscriptions:
         try:
             element.update_data()
@@ -512,8 +500,7 @@ elif options.update is True:
                                        "database.") % video.title)
 
 elif options.download is True:
-    listofsubscriptions = []
-    subscriptions = db.GetSubscriptions()
+    listofsubscriptions = db.GetSubscriptions(conf)
     logger.debug(_("Trying to determine the itag value for youtube-dl from"
                    " your quality and codec settings."))
     itag_value = conf.GetYoutubeParameter()
@@ -522,19 +509,10 @@ elif options.download is True:
         logger.debug(_("Codec and resolution could not be determined, using "
                        "maximum possible value."))
         itag_value = 38
-    for element in subscriptions:
-        listofsubscriptions.append(subscription.sub(id=element[0],
-                                                    title=element[1],
-                                                    type=element[2],
-                                                    name=element[3],
-                                                    search=element[4],
-                                                    directory=element[5],
-                                                    disabled=element[6],
-                                                    conf=conf))
     videosdownloaded = 0
     videosfailed = 0
     for element in listofsubscriptions:
-        videos = db.GetVideos(element.GetId())
+        videos = db.GetVideos(element.GetId(), conf)
         element.GatherVideos(videos)
         try:
             element.DownloadVideos(itag_value)
@@ -636,7 +614,7 @@ elif options.subscriptionid is not None:
                                             search=Data[0][4],
                                             directory=Data[0][5],
                                             disabled=Data[0][6], conf=conf)
-            videos = db.GetVideos(Subscription.GetId())
+            videos = db.GetVideos(Subscription.GetId(), conf)
             Subscription.GatherVideos(videos)
             video_list = Subscription.PrintVideos()
             for video in video_list:
@@ -662,16 +640,9 @@ elif options.catchup is not None:
 
 
 elif options.cleanup is True:
-    subscriptions_list = []
-    subscriptions = db.GetSubscriptions()
-    for element in subscriptions:
-        subscriptions_list.append(subscription.sub(id=element[0],
-                                  title=element[1], type=element[2],
-                                  name=element[3], search=element[4],
-                                  directory=element[5], disabled=element[6],
-                                  conf=conf))
+    subscriptions_list = db.GetSubscriptions(conf)
     for element in subscriptions_list:
-        videos = db.GetVideos(element.GetId())
+        videos = db.GetVideos(element.GetId(), conf)
         element.CheckAndDelete(videos)
         for delete_video in element.ToDelete:
             logger.debug(_("Deleting video %s from "