123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #
- # This file is part of stov, written by Helmut Pozimski 2012-2014.
- #
- # stov is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, version 2 of the License.
- #
- # stov is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with stov. If not, see <http://www.gnu.org/licenses/>.
- # -*- coding: utf8 -*-
- 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
- class sub(object):
- def __init__(self, type, name, conf, search="", id=0, title="", directory="",
- disabled=0):
- self.__ID = id
- self.__title = title
- self.__type = type
- 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 = []
- if int(disabled) == 0:
- self.__disabled = False
- elif int(disabled) == 1:
- self.__disabled = True
- self.__ConstructAPIURL()
- def GetTitle(self):
- return self.__title
- def GetId(self):
- return self.__ID
- def SetID(self, id):
- self.__ID = id
- 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 self.__search != "" and self.__conf.values["check_title"] == "yes":
- if self.__search in video.title:
- return True
- else:
- return False
- else:
- return True
- else:
- return False
- def GatherVideos(self, video_data):
- """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))
- def DownloadVideos(self, itag_value):
- """Uses the DownloadVideo method of the video object to download all
- videos contained in the subscription and adds them to the list of
- downloaded videos if the download succeeds.
- """
- if self.__disabled is False:
- for video in self.__video_list:
- if video.downloaded == 0:
- if video.DownloadVideo(self.__directory, itag_value) is True:
- self.DownloadedVideos.append(video)
- else:
- self.FailedVideosCount = self.FailedVideosCount + 1
- self.FailedVideos.append(video)
- else:
- raise stov_exceptions.SubscriptionDisabledException(self.__title)
- def PrintVideos(self):
- """Prepares a human readable list of all videos contained
- in the subscription.
- """
- for i in self.__video_list:
- video_list = []
- if i.downloaded == 0:
- video_list.append(i.title + _(" (pending)"))
- elif i.downloaded == 1:
- video_list.append(i.title + _(" (downloaded)"))
- elif i.downloaded == -1:
- video_list.append(i.title + _(" (failed)"))
- return video_list
- def AddSub(self):
- """Adds a new subscription to the database"""
- self.ParseAPIData()
- self.__title = self.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:
- self.__ConnectAPI = urllib2.urlopen(self.__APIURL, timeout=30)
- self.__APIResponse = self.__ConnectAPI.read()
- self.__ConnectAPI.close()
- except urllib2.HTTPError:
- raise stov_exceptions.NoDataFromYoutubeAPIException()
- except ssl.SSLError:
- raise stov_exceptions.YoutubeAPITimeoutException(self.__title)
- else:
- parser = youtubeAPI.Parser(self.__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()
- self.GatherVideos(videos)
- for entry in self.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)
|