|
@@ -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()
|