浏览代码

Add some more verbose output for debugging purposes

Helmut Pozimski 12 年之前
父节点
当前提交
765ba9ef95
共有 5 个文件被更改,包括 66 次插入3 次删除
  1. 0 1
      TODO
  2. 31 0
      configuration.py
  3. 1 1
      outputhelper.py
  4. 27 1
      subscription.py
  5. 7 0
      youtube.py

+ 0 - 1
TODO

@@ -6,4 +6,3 @@ Long term goals:
 * Add additional video sites
 * Implement maxvids option
 * Improve handling of situations where a video is permanently unavailable
-* Add verbose output

+ 31 - 0
configuration.py

@@ -59,6 +59,8 @@ class conf(object):
 
 		"""
 		try:
+			printf(_("Opening configuration file"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "w")
 		except IOError, os.error:
 			printf(_("Configuration could not be written, please"
@@ -66,6 +68,8 @@ class conf(object):
 				outputlevel="default", level=self.outputlevel, descriptor="stderr")
 		else:
 			for key in self.values.iterkeys():
+				printf(_("Writing value for %s" % key.upper()),
+					outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 				self.configfile.write(key.upper() + "=" + self.values[key] + "\n")
 			self.configfile.close()
 
@@ -79,6 +83,8 @@ class conf(object):
 				outputlevel = "default", level=self.outputlevel, descriptor="stderr")
 		else:
 			self.__dbcursor = self.__database.cursor()
+			printf(_("Creating table subscriptions"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			self.__dbcursor.execute("""CREATE TABLE subscriptions (
 						id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 						title TEXT,
@@ -87,6 +93,8 @@ class conf(object):
 						searchstring TEXT,
 						directory TEXT
 						);""")
+			printf(_("Creating table videos"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			self.__dbcursor.execute("""CREATE TABLE videos (
 						id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 						title TEXT,
@@ -104,12 +112,16 @@ class conf(object):
 
 		"""
 		try:
+			printf(_("Creating hidden directory in home for configuration and database"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			os.mkdir(str(os.environ['HOME']) + "/.stov", 0750)
 		except os.error:
 			printf(_("Configuration directory could not be created, "
 				"please check that your home directory exists and is writable"),
 				outputlevel="default", level=self.outputlevel, descriptor="stderr")
 		else:
+			printf(_("Looking for youtube-dl file"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			self.values["youtube-dl"] = subprocess.Popen(["which", "youtube-dl"],
 				stdout=subprocess.PIPE).communicate()[0].strip()
 			self.WriteConfig()
@@ -121,12 +133,16 @@ class conf(object):
 
 		"""
 		try:
+			printf(_("Opening config file for reading"),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")	
 			self.configfile = open(str(os.environ['HOME']) + "/.stov/stov.config", "r")
 		except IOError:
 			printf(_("Configuration could not be "
 				"read, please check that the configuration file exists and is readable"),
 				outputlevel="default", level=self.outputlevel, descriptor="stderr")
 		for lines in self.configfile:
+			printf(_("Reading line %s" % lines.strip()),
+				outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 			self.tmpline = lines.strip()
 			self.tmplist = self.tmpline.split("=")
 			self.values[self.tmplist[0].lower()] = self.tmplist[1]
@@ -138,6 +154,8 @@ class conf(object):
 		stov version.
 
 		"""
+		printf(_("Checking current and running configuration version"),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		try:
 			self.__currentversion = int(self.values["config_version"])
 		except ValueError:
@@ -145,6 +163,9 @@ class conf(object):
 				level=self.outputlevel, descriptor="stderr")
 		self.values["config_version"] = "0"
 		self.ReadConfig()
+		printf(_("Found running version: " + self.values["config_version"] + "\n" +
+			"Current version: " + str(self.__currentversion)),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		if self.values["config_version"] == "0" \
 		or int(self.values["config_version"]) < self.__currentversion:
 			self.values["config_version"] = str(self.__currentversion)
@@ -162,6 +183,8 @@ class conf(object):
 
 	def CheckDB(self):
 		"""Checks the database if it is up-to-date"""
+		printf(_("Checking current and running database version."),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		try:
 			self.__currentversion = int(self.values["db_version"])
 		except ValueError:
@@ -169,6 +192,9 @@ class conf(object):
 				level=self.outputlevel, descriptor="stderr")
 		self.values["db_version"] = "0"
 		self.ReadConfig()
+		printf(_("Found running database version: " + self.values["db_version"] +
+			"\n" + "Current version: " + str(self.__currentversion)),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		if self.values["db_version"] == "0" \
 		or int(self.values["db_version"]) < self.__currentversion:
 			self.values["db_version"] = str(self.__currentversion)
@@ -187,6 +213,9 @@ class conf(object):
 		and returns it
 
 		"""
+		printf(_("Trying to determine the itag value for youtube-dl from your"
+			" quality and codec settings"),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		itag_value = 0
 		if self.values["videocodec"] == "flv":
 			if self.values["maxresolution"] == "240p":
@@ -215,4 +244,6 @@ class conf(object):
 				itag_value = 37
 			elif self.values["maxresolution"] == "3072p":
 				itag_value = 38
+		printf(_("Found value: %s" % itag_value),
+			outputlevel="verbose", level=self.outputlevel, descriptor="stderr")
 		return itag_value

+ 1 - 1
outputhelper.py

@@ -50,7 +50,7 @@ def printf(string, outputlevel, descriptor="stdout", level="default"):
 			else:
 				print str(string)
 		elif level == "verbose":
-			if string is unicode:
+			if isinstance(string, unicode):
 				print >> sys.stderr, string.encode("utf8")
 			elif string is str:
 				print >> sys.stderr, string

+ 27 - 1
subscription.py

@@ -85,11 +85,16 @@ class sub(object):
 				outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
 
 		for i in self.__ParsedResponse.videos:
+			printf(_('Checking if video "%s" exists in the database' %i.title),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__ytid = i.ytid
 			self.__videoquery = "SELECT id FROM videos WHERE ytid=?"
 			self.__cursor.execute(self.__videoquery, (self.__ytid,))
 			self.__tmpid = self.__cursor.fetchall()
 			if self.__tmpid == []:
+				printf(_("Video %s not found in database, inserting..." % i.title),
+					outputlevel="verbose", level=self.__conf.outputlevel,
+					descriptor="stderr")
 				self.__query = "INSERT INTO videos (title, description, \
 				ytid, subscription_id, downloaded) VALUES (?, ?, ?, ?, ?)"
 				self.__data = (i.title, i.description, self.__ytid, self.__ID, 0)
@@ -112,11 +117,16 @@ class sub(object):
 				"Please check permissions and try again."),
 				outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
 
+		printf(_("Getting all videos for subscription %s from database" 
+			% self.__title), outputlevel="verbose", level=self.__conf.outputlevel,
+			descriptor="stderr")
 		self.__videoquerybysubscription = "SELECT id, title, description, \
 		ytid, downloaded FROM videos WHERE subscription_id=?"
 		self.__cursor.execute(self.__videoquerybysubscription, (self.__ID,))
 		self.__videodata = self.__cursor.fetchall()
 		for i in self.__videodata:
+			printf(_("Got video %s" % i[1]),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__videos.append(youtube.video(id=i[0],
 			title=i[1], description=i[2], ytid=i[3],
 			downloaded=i[4], conf=self.__conf))
@@ -160,11 +170,17 @@ class sub(object):
 		else:
 			self.__cursor = self.__connection.cursor()
 			self.__title = self.__ParsedResponse.title
+			printf(_("Found subscription title %s" % self.__title),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__directory = self.__name + "_" + self.__search.replace(" ", "_")
+			printf(_("Directory: %s" % self.__directory),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__query = "INSERT INTO subscriptions (title, type, searchstring, \
 			directory, name) VALUES (?, ?, ?, ?, ?)"
 			self.__data = (self.__title, self.__type, self.__search,
 			self.__directory, self.__name)
+			printf(_("Writing subscription info into database..."),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__cursor.execute(self.__query, self.__data)
 			self.__connection.commit()
 
@@ -173,6 +189,8 @@ class sub(object):
 		self.__cursor.execute(self.__query, self.__data)
 		self.__ID = self.__cursor.fetchone()
 		self.__ID = self.__ID[0]
+		printf(_("Subscription got internal ID %s" %self.__ID),
+			outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 		self.__connection.close()
 
 	def __ParseAPIData(self):
@@ -181,7 +199,11 @@ class sub(object):
 
 		"""
 		try:
+			printf(_("Connectiong to youtube API."),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__ConnectAPI = urllib2.urlopen(self.__APIURL)
+			printf(_("Connection established, reading data."),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			self.__APIResponse = self.__ConnectAPI.read()
 			self.__ConnectAPI.close()
 		except IOError:
@@ -191,6 +213,8 @@ class sub(object):
 				outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
 			sys.exit(1)
 		else:
+			printf(_("Parsing youtube API response."),
+				outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 			parser = youtubeAPI.Parser(self.__APIResponse)
 			self.__ParsedResponse = parser.parse()
 
@@ -212,6 +236,8 @@ class sub(object):
 			printf(_("None or invalid subscription type given, "
 						"please check the type option and try again"),
 				outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
+		printf(_("Constructed the following API URL: %s" % self.__APIURL),
+			outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 	def CheckAndDelete(self):
 		"""Checks if a video still exists in the current API response and deletes
 		it if it doesn't
@@ -230,5 +256,5 @@ class sub(object):
 					descriptor="stdout")
 				else:
 					printf(_("Videos %s could not be deleted from the database"),
-					outputlevel="default", level=self.__conf.outputlevel,
+					outputlevel="verbose", level=self.__conf.outputlevel,
 					descriptor="stderr")

+ 7 - 0
youtube.py

@@ -38,6 +38,8 @@ class video(object):
 		self.__targetdir = self.__conf.values["downloaddir"] + "/" + directory
 		if os.access(self.__targetdir, os.F_OK) is False:
 			try:
+				printf(_("Creating directory %s" % self.__targetdir),
+					outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 				os.makedirs(self.__targetdir, 0750)
 			except os.error:
 				printf(_("Download directory does not exist \
@@ -47,6 +49,8 @@ and can't be created. Please check your configuration and try again"),
 		os.chdir(self.__targetdir)
 		if self.downloaded == 0:
 			try:
+				printf(_('Downloading video "%s"' % self.title),
+					outputlevel="verbose", level=self.__conf.outputlevel, descriptor="stderr")
 				if self.__conf.outputlevel == "default":
 					subprocess.check_call(["youtube-dl", "--max-quality=%s" % itag_value, "-t",
 					"http://www.youtube.com/watch?v=%s" % self.ytid],
@@ -68,6 +72,9 @@ and can't be created. Please check your configuration and try again"),
 						outputlevel="default", level=self.__conf.outputlevel, descriptor="stderr")
 				else:
 					self.__cursor = self.__database.cursor()
+					printf(_('Downloaded video "%s", updating database accordingly.'
+						% self.title), outputlevel="verbose", level=self.__conf.outputlevel,
+						descriptor="stderr")
 					self.__query = "UPDATE videos SET downloaded ='1' WHERE id = ?"
 					self.__cursor.execute(self.__query, (self.__ID,))
 					self.__database.commit()