소스 검색

refactored the youtubeAPI and subscription files to make it easier to abstract and hook alternative methods of retrieving data into it

Helmut Pozimski 10 년 전
부모
커밋
d9bb01f2b8
3개의 변경된 파일75개의 추가작업 그리고 56개의 파일을 삭제
  1. 11 53
      lib_stov/subscription.py
  2. 62 1
      lib_stov/youtubeAPI.py
  3. 2 2
      stov

+ 11 - 53
lib_stov/subscription.py

@@ -18,14 +18,6 @@
 
 from __future__ import unicode_literals
 
-import sys
-import ssl
-
-if sys.version_info >= (3,):
-    import urllib.request as urllib2
-else:
-    import urllib2
-
 from lib_stov import youtubeAPI
 from lib_stov import youtube
 from lib_stov import stov_exceptions
@@ -40,21 +32,21 @@ class sub(object):
         self.__name = name
         self.__search = search
         self.__directory = directory
-        self.__APIURL = ""
         self.__conf = conf
         self.DownloadedVideos = []
         self.FailedVideosCount = 0
         self.FailedVideos = []
         self.ToDelete = []
         self.__video_list = []
-        self.parsed_response = None
         self.__id_list = []
+        self.parsed_response = None
 
         if int(disabled) == 0:
             self.__disabled = False
         elif int(disabled) == 1:
             self.__disabled = True
-        self.__ConstructAPIURL()
+
+        self._connector = youtubeAPI.Connector(self.__type, self.__name, self.__search, self.__conf)
 
     def GetTitle(self):
         return self.__title
@@ -128,60 +120,26 @@ class sub(object):
 
     def AddSub(self):
         """Adds a new subscription to the database"""
-        self.ParseAPIData()
-        self.__title = self.parsed_response.title
+        parsed_response = self._connector.ParseAPIData()
+        self.__title = parsed_response.title
         self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
         data = (self.__title, self.__type, self.__search, self.__directory,
                 self.__name, 0)
         return data
 
-    def ParseAPIData(self):
-        """Retrieves the youtube API data and parses them so they can be used
-        by the object
-
-        """
-        try:
-            ConnectAPI = urllib2.urlopen(self.__APIURL, timeout=30)
-            APIResponse = ConnectAPI.read()
-            ConnectAPI.close()
-        except urllib2.HTTPError:
-                raise stov_exceptions.NoDataFromYoutubeAPIException()
-        except ssl.SSLError:
-                raise stov_exceptions.YoutubeAPITimeoutException(self.__title)
-        else:
-            parser = youtubeAPI.Parser(APIResponse)
-            self.parsed_response = parser.parse()
-
-    def __ConstructAPIURL(self):
-        """Constructs the API URL which is used to retrieve API data"""
-        if self.__type == "channel":
-            self.__APIURL = "https://gdata.youtube.com/feeds/api/users/" \
-                            + urllib2.quote(self.__name) + "/uploads/" \
-                            "?v=2&max-results=%s" \
-                            % self.__conf.values["maxvideos"]
-            if self.__search != "":
-                self.__APIURL = self.__APIURL + "&q=" + "%22" \
-                    + urllib2.quote(self.__search) + "%22"
-        elif self.__type == "search":
-            self.__APIURL = "http://gdata.youtube.com/feeds/api/videos?q=" \
-                            + urllib2.quote(self.__search) + "&v=2" \
-                            + "&max-results=%s" \
-                              % self.__conf.values["maxvideos"]
-        elif self.__type == "playlist":
-            self.__APIURL = "https://gdata.youtube.com/feeds/api/playlists/" \
-                            + "%20" + urllib2.quote(self.__name) + "%20?v=2" \
-                            + "&max-results=%s" \
-                            % self.__conf.values["maxvideos"]
-
     def CheckAndDelete(self, videos):
         """Checks if a video still exists in the current API response and
         deletes it if it doesn't
 
         """
-        self.ParseAPIData()
+        parsed_response = self._connector.ParseAPIData()
         self.GatherVideos(videos)
-        for entry in self.parsed_response.videos:
+        for entry in parsed_response.videos:
             self.__id_list.append(entry.ytid)
         for item in self.__video_list:
             if item.ytid not in self.__id_list:
                 self.ToDelete.append(item)
+
+    def update_data(self):
+        """Updates the data from the API."""
+        self.parsed_response = self._connector.ParseAPIData()

+ 62 - 1
lib_stov/youtubeAPI.py

@@ -18,8 +18,16 @@
 
 from __future__ import unicode_literals
 
-from xml.dom.minidom import parseString
+import sys
+import ssl
+
+if sys.version_info >= (3,):
+    import urllib.request as urllib2
+else:
+    import urllib2
 
+from xml.dom.minidom import parseString
+from lib_stov import stov_exceptions
 
 class Parser(object):
     def __init__(self, api_result):
@@ -72,3 +80,56 @@ class YtVideo(object):
         self.title = title
         self.description = description
         self.ytid = ytid
+
+
+class Connector(object):
+    """Handles all connections to the youtube API and invokes the parsing
+    functions.
+    """
+    def __init__(self, type, name, search, conf):
+        """Populates the object will all necessary variables."""
+        self._type = type
+        self._name = name
+        self._search = search
+        self._conf = conf
+        self._APIURL = self._ConstructAPIURL()
+
+    def ParseAPIData(self):
+        """Retrieves the youtube API data and parses them so they can be used
+        by the object
+
+        """
+        try:
+            ConnectAPI = urllib2.urlopen(self._APIURL, timeout=30)
+            APIResponse = ConnectAPI.read()
+            ConnectAPI.close()
+        except urllib2.HTTPError:
+                raise stov_exceptions.NoDataFromYoutubeAPIException()
+        except ssl.SSLError:
+                raise stov_exceptions.YoutubeAPITimeoutException(self._subscription.title)
+        else:
+            parser = Parser(APIResponse)
+            parsed_response = parser.parse()
+            return parsed_response
+
+    def _ConstructAPIURL(self):
+        """Constructs the API URL which is used to retrieve API data"""
+        if self._type == "channel":
+            APIURL = "https://gdata.youtube.com/feeds/api/users/" \
+                            + urllib2.quote(self._name) + "/uploads/" \
+                            "?v=2&max-results=%s" \
+                            % self._conf.values["maxvideos"]
+            if self._search != "":
+                APIURL = APIURL + "&q=" + "%22" \
+                    + urllib2.quote(self._search) + "%22"
+        elif self._type == "search":
+            APIURL = "http://gdata.youtube.com/feeds/api/videos?q=" \
+                            + urllib2.quote(self._search) + "&v=2" \
+                            + "&max-results=%s" \
+                              % self._conf.values["maxvideos"]
+        elif self._type == "playlist":
+            APIURL = "https://gdata.youtube.com/feeds/api/playlists/" \
+                            + "%20" + urllib2.quote(self._name) + "%20?v=2" \
+                            + "&max-results=%s" \
+                            % self._conf.values["maxvideos"]
+        return APIURL

+ 2 - 2
stov

@@ -427,7 +427,7 @@ if options.add is True:
     else:
         logger.debug(_("Subscription sucessfully inserted into database."))
     try:
-        NewSubscription.ParseAPIData()
+        NewSubscription.update_data()
     except stov_exceptions.YoutubeAPITimeoutException as e:
         logger.error(e)
     except stov_exceptions.NoDataFromYoutubeAPIException as e:
@@ -493,7 +493,7 @@ elif options.update is True:
                                    conf=conf))
     for element in listofsubscriptions:
         try:
-            element.ParseAPIData()
+            element.update_data()
         except stov_exceptions.YoutubeAPITimeoutException as e:
             logger.error(e)
         except stov_exceptions.NoDataFromYoutubeAPIException as e: