Explorar el Código

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 hace 9 años
padre
commit
bb1007d459
Se han modificado 6 ficheros con 48 adiciones y 61 borrados
  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
 * Remove python 2 compatibility code, support python 3 only
 * Implement youtube-dl as alternative mode to API version 2
 * Implement youtube-dl as alternative mode to API version 2
 * Check with pep8 and pylinth to do coding style improvements before the next release
 * 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
 import sqlite3
 
 
 from lib_stov import stov_exceptions
 from lib_stov import stov_exceptions
+from lib_stov import youtube
+from lib_stov import subscription
 
 
 
 
 class db(object):
 class db(object):
@@ -115,16 +117,24 @@ class db(object):
             deletesubscription = "DELETE FROM subscriptions WHERE id=?"
             deletesubscription = "DELETE FROM subscriptions WHERE id=?"
             self.__ExecuteStatement(deletesubscription, (sub_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
         """Gets all videos of a given subscription id from the database and
         returns them
         returns them
 
 
         """
         """
+        videos_list = []
         video_query = "SELECT id, title, description, ytid, downloaded, " \
         video_query = "SELECT id, title, description, ytid, downloaded, " \
                       "failcnt FROM videos WHERE subscription_id=?"
                       "failcnt FROM videos WHERE subscription_id=?"
-        cursor = self.__ExecuteStatement(video_query, (video_id,))
+        cursor = self.__ExecuteStatement(video_query, (subscription_id,))
         result = cursor.fetchall()
         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):
     def VideoInDatabase(self, ytid):
         """Checks if the video with a given youtube id already exists in the
         """Checks if the video with a given youtube id already exists in the
@@ -203,13 +213,21 @@ class db(object):
                            "WHERE subscription_id =?"
                            "WHERE subscription_id =?"
         self.__ExecuteStatement(update_statement, (video_id,))
         self.__ExecuteStatement(update_statement, (video_id,))
 
 
-    def GetSubscriptions(self):
+    def GetSubscriptions(self, conf):
         """Retrieves all subscriptions from the database"""
         """Retrieves all subscriptions from the database"""
+        subscriptions_list = []
         subscriptions_query = "SELECT id,title,type,name,searchstring," \
         subscriptions_query = "SELECT id,title,type,name,searchstring," \
                               "directory,disabled FROM subscriptions"
                               "directory,disabled FROM subscriptions"
         result_cursor = self.__ExecuteStatement(subscriptions_query)
         result_cursor = self.__ExecuteStatement(subscriptions_query)
         result = result_cursor.fetchall()
         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):
     def Vacuum(self):
         """Vacuums the database, shrinking it in size"""
         """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
     """This class will retrieve all the necessary data from youtube using
     youtube-dl, thus bypassing the API.
     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."""
         """Populates the object with all necessary data."""
         self._type = type
         self._type = type
         self._name = name
         self._name = name
@@ -53,18 +53,21 @@ class Connector(object):
                         % urllib2.quote(self._search)
                         % urllib2.quote(self._search)
         elif self._type == "playlist":
         elif self._type == "playlist":
             self._url = "https://www.youtube.com/playlist?list=%s" \
             self._url = "https://www.youtube.com/playlist?list=%s" \
-                        % urllib2.quote(self._search)
+                        % urllib2.quote(self._name)
 
 
     def _fetch_title(self):
     def _fetch_title(self):
         """Retrieves the title of the HTML page to use as a title for the
         """Retrieves the title of the HTML page to use as a title for the
         subscription."""
         subscription."""
         data = urllib2.urlopen(self._url)
         data = urllib2.urlopen(self._url)
         parsed_html = lxml.html.parse(data)
         parsed_html = lxml.html.parse(data)
+        data.close()
         i = 0
         i = 0
         for item in parsed_html.iter("title"):
         for item in parsed_html.iter("title"):
             if i == 0:
             if i == 0:
                 self._title = item.text_content().strip().replace("\n", "")
                 self._title = item.text_content().strip().replace("\n", "")
             i += 1
             i += 1
+        if self._search != "" and self._type == "channel":
+            self._title += " search %s" % self._search
 
 
     def _fetch_videos(self):
     def _fetch_videos(self):
         """Retrieves all the relevant videos in a subscription."""
         """Retrieves all the relevant videos in a subscription."""
@@ -76,7 +79,7 @@ class Connector(object):
                                                                    "--match-title",
                                                                    "--match-title",
                                                                    self._search,
                                                                    self._search,
                                                                    "--get-id",
                                                                    "--get-id",
-                                                                   self._url])
+                                                                   self._url]).strip()
             except subprocess.CalledProcessError as e:
             except subprocess.CalledProcessError as e:
                 video_ids = e.output.strip()
                 video_ids = e.output.strip()
         else:
         else:
@@ -84,7 +87,7 @@ class Connector(object):
                 video_ids = subprocess.check_output([self._conf.values["youtube-dl"], "--max-downloads",
                 video_ids = subprocess.check_output([self._conf.values["youtube-dl"], "--max-downloads",
                                                                    self._conf.values["maxvideos"],
                                                                    self._conf.values["maxvideos"],
                                                                    "--get-id",
                                                                    "--get-id",
-                                                                   self._url])
+                                                                   self._url]).strip()
             except subprocess.CalledProcessError as e:
             except subprocess.CalledProcessError as e:
                 video_ids = e.output.strip()
                 video_ids = e.output.strip()
         for video_id in video_ids.split("\n"):
         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.
         of an error returned by youtube-dl.
     """
     """
     def __init__(self):
     def __init__(self):
-        self._message = _("Exectution of youtube-dl failed.")
+        self._message = _("Execution of youtube-dl failed.")
 
 
     def __str__(self):
     def __str__(self):
         return self._message
         return self._message

+ 6 - 12
lib_stov/subscription.py

@@ -43,9 +43,9 @@ class sub(object):
         self.parsed_response = None
         self.parsed_response = None
 
 
         if int(disabled) == 0:
         if int(disabled) == 0:
-            self.__disabled = False
+            self.disabled = False
         elif int(disabled) == 1:
         elif int(disabled) == 1:
-            self.__disabled = True
+            self.disabled = True
 
 
         if self.__conf.values["use_api"]:
         if self.__conf.values["use_api"]:
             self._connector = youtubeAPI.Connector(self.__type, self.__name,
             self._connector = youtubeAPI.Connector(self.__type, self.__name,
@@ -66,7 +66,7 @@ class sub(object):
     def CheckStringMatch(self, video):
     def CheckStringMatch(self, video):
         """Checks if the subscription is enabled and the video matches the
         """Checks if the subscription is enabled and the video matches the
         search string defined for the subscription"""
         search string defined for the subscription"""
-        if self.__disabled is False:
+        if not self.disabled:
             if self.__search != "" and self.__conf.values["check_title"]\
             if self.__search != "" and self.__conf.values["check_title"]\
                     == "yes":
                     == "yes":
                 if self.__search in video.title:
                 if self.__search in video.title:
@@ -78,18 +78,12 @@ class sub(object):
         else:
         else:
                 return False
                 return False
 
 
-    def GatherVideos(self, video_data):
+    def GatherVideos(self, video_list):
         """Gathers all videos in the subscription and saves
         """Gathers all videos in the subscription and saves
         them in an the internal list so they can be accessed by the object
         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):
     def DownloadVideos(self, itag_value):
         """Uses the DownloadVideo method of the video object to download all
         """Uses the DownloadVideo method of the video object to download all
@@ -97,7 +91,7 @@ class sub(object):
         downloaded videos if the download succeeds.
         downloaded videos if the download succeeds.
 
 
         """
         """
-        if self.__disabled is False:
+        if not self.disabled:
             for video in self.__video_list:
             for video in self.__video_list:
                 if video.downloaded == 0:
                 if video.downloaded == 0:
                     if video.DownloadVideo(self.__directory, itag_value) is\
                     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 configuration
 from lib_stov import stov_exceptions
 from lib_stov import stov_exceptions
 from lib_stov import database
 from lib_stov import database
-from lib_stov import noapi
 
 
 
 
 """Setup the logger to log messages to stdout and stderr"""
 """Setup the logger to log messages to stdout and stderr"""
@@ -448,17 +447,16 @@ if options.add is True:
                 + _(" successfully added"))
                 + _(" successfully added"))
 
 
 elif options.list is True:
 elif options.list is True:
-    Listofsubscriptions = db.GetSubscriptions()
+    Listofsubscriptions = db.GetSubscriptions(conf)
     sub_state = None
     sub_state = None
     if len(Listofsubscriptions) != 0:
     if len(Listofsubscriptions) != 0:
         logger.info(_("ID Title"))
         logger.info(_("ID Title"))
         for sub in Listofsubscriptions:
         for sub in Listofsubscriptions:
-            if int(sub[6]) == 0:
+            if not sub.disabled:
                 sub_state = _("enabled")
                 sub_state = _("enabled")
-            elif int(sub[6]) == 1:
+            elif sub.disabled:
                 sub_state = _("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)
                             + " (%s)" % sub_state)
     else:
     else:
         logger.info(_("No subscriptions added yet, add one!"))
         logger.info(_("No subscriptions added yet, add one!"))
@@ -481,17 +479,7 @@ elif options.deleteid is not None:
         else:
         else:
             logger.info(_("Subscription deleted successfully!"))
             logger.info(_("Subscription deleted successfully!"))
 elif options.update is True:
 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:
     for element in listofsubscriptions:
         try:
         try:
             element.update_data()
             element.update_data()
@@ -512,8 +500,7 @@ elif options.update is True:
                                        "database.") % video.title)
                                        "database.") % video.title)
 
 
 elif options.download is True:
 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"
     logger.debug(_("Trying to determine the itag value for youtube-dl from"
                    " your quality and codec settings."))
                    " your quality and codec settings."))
     itag_value = conf.GetYoutubeParameter()
     itag_value = conf.GetYoutubeParameter()
@@ -522,19 +509,10 @@ elif options.download is True:
         logger.debug(_("Codec and resolution could not be determined, using "
         logger.debug(_("Codec and resolution could not be determined, using "
                        "maximum possible value."))
                        "maximum possible value."))
         itag_value = 38
         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
     videosdownloaded = 0
     videosfailed = 0
     videosfailed = 0
     for element in listofsubscriptions:
     for element in listofsubscriptions:
-        videos = db.GetVideos(element.GetId())
+        videos = db.GetVideos(element.GetId(), conf)
         element.GatherVideos(videos)
         element.GatherVideos(videos)
         try:
         try:
             element.DownloadVideos(itag_value)
             element.DownloadVideos(itag_value)
@@ -636,7 +614,7 @@ elif options.subscriptionid is not None:
                                             search=Data[0][4],
                                             search=Data[0][4],
                                             directory=Data[0][5],
                                             directory=Data[0][5],
                                             disabled=Data[0][6], conf=conf)
                                             disabled=Data[0][6], conf=conf)
-            videos = db.GetVideos(Subscription.GetId())
+            videos = db.GetVideos(Subscription.GetId(), conf)
             Subscription.GatherVideos(videos)
             Subscription.GatherVideos(videos)
             video_list = Subscription.PrintVideos()
             video_list = Subscription.PrintVideos()
             for video in video_list:
             for video in video_list:
@@ -662,16 +640,9 @@ elif options.catchup is not None:
 
 
 
 
 elif options.cleanup is True:
 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:
     for element in subscriptions_list:
-        videos = db.GetVideos(element.GetId())
+        videos = db.GetVideos(element.GetId(), conf)
         element.CheckAndDelete(videos)
         element.CheckAndDelete(videos)
         for delete_video in element.ToDelete:
         for delete_video in element.ToDelete:
             logger.debug(_("Deleting video %s from "
             logger.debug(_("Deleting video %s from "