Browse Source

Catch database errors.

Douglas William Thrift 14 years ago
parent
commit
f8bfe0da56
1 changed files with 71 additions and 62 deletions
  1. 71 62
      locationbot.py

+ 71 - 62
locationbot.py

@@ -150,46 +150,50 @@ class LocationBot(ircbot.SingleServerIRCBot):
 			pass
 
 		self.__latitude_next = now.replace(minute = now.minute - now.minute % 5, second = 0, microsecond = 0) + timedelta(minutes = 5)
-		db, cursor = self.__db()
 
-		cursor.execute('select nick, channels, location, latitude from locationbot.nick where latitude is not null order by latitude')
+		try:
+			db, cursor = self.__db()
+
+			cursor.execute('select nick, channels, location, latitude from locationbot.nick where latitude is not null order by latitude')
 
-		locations = {}
+			locations = {}
 
-		for nick, channels, old_location, latitude in cursor.fetchall():
-			new_location = locations.get(latitude)
+			for nick, channels, old_location, latitude in cursor.fetchall():
+				new_location = locations.get(latitude)
 
-			if latitude not in locations:
-				url = 'http://www.google.com/latitude/apps/badge/api?' + urllib.urlencode({'user': latitude, 'type': 'json'})
+				if latitude not in locations:
+					url = 'http://www.google.com/latitude/apps/badge/api?' + urllib.urlencode({'user': latitude, 'type': 'json'})
 
-				try:
-					response = urllib2.urlopen(url)
-				except urllib2.URLError, error:
-					print error
-					continue
+					try:
+						response = urllib2.urlopen(url)
+					except urllib2.URLError, error:
+						print error
+						continue
 
-				try:
-					geo = json.load(response)
-				except (TypeError, ValueError), error:
-					print error
-					continue
+					try:
+						geo = json.load(response)
+					except (TypeError, ValueError), error:
+						print error
+						continue
 
-				try:
-					properties = geo['features'][0]['properties']
-					new_location = properties['reverseGeocode']
-					locations[latitude] = new_location
-					updated = datetime.fromtimestamp(properties['timeStamp'], pytz.utc)
-				except (IndexError, KeyError), error:
-					print error
-					continue
+					try:
+						properties = geo['features'][0]['properties']
+						new_location = properties['reverseGeocode']
+						locations[latitude] = new_location
+						updated = datetime.fromtimestamp(properties['timeStamp'], pytz.utc)
+					except (IndexError, KeyError), error:
+						print error
+						continue
 
-				cursor.execute('update locationbot.nick set location = %s, updated = %s where latitude = %s', (new_location, updated, latitude))
-				db.commit()
+					cursor.execute('update locationbot.nick set location = %s, updated = %s where latitude = %s', (new_location, updated, latitude))
+					db.commit()
 
-			if channels and new_location and new_location != old_location:
-				with self.__locations_lock:
-					for channel in frozenset(channels).intersection(self.__channels):
-						self.__locations.append((nick, channel, locations[latitude]))
+				if channels and new_location and new_location != old_location:
+					with self.__locations_lock:
+						for channel in frozenset(channels).intersection(self.__channels):
+							self.__locations.append((nick, channel, locations[latitude]))
+		except psycopg2.Error, error:
+			print error
 
 		with self.__latitude_timer_lock:
 			self.__latitude_timer = threading.Timer((self.__latitude_next - datetime.utcnow()).seconds, self.__latitude)
@@ -397,38 +401,43 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		if not admin and not self.__channel(nick):
 			return
 
-		login = self.__login(None, nickmask, nick)
-
 		try:
-			command, arguments = event.arguments()[0].split(None, 1)
-		except ValueError:
-			command = event.arguments()[0].strip()
-			arguments = ''
-
-		command = irclib.irc_lower(command.lstrip('!'))
-
-		if command == 'help':
-			self.__help(connection, nick, admin, login, arguments)
-		elif command == 'login':
-			self.__login(connection, nickmask, nick, arguments)
-		elif command == 'status':
-			self.__status(connection, nick, login, arguments)
-		elif not login and command == 'register':
-			self.__register(connection, nick, arguments)
-		elif login and command == 'logout':
-			self.__logout(connection, nick)
-		elif login and command == 'unset':
-			self.__unset(connection, nick, login, arguments)
-		elif admin and command == 'reload':
-			self.__reload(connection, nick)
-		elif admin and command == 'restart':
-			self.__restart(connection)
-		elif admin and command == 'say':
-			self.__say(connection, nick, arguments)
-		elif admin and command == 'who':
-			self.__who(connection, nick)
-		else:
-			self.__unknown(connection, nick, command)
+			login = self.__login(None, nickmask, nick)
+
+			try:
+				command, arguments = event.arguments()[0].split(None, 1)
+			except ValueError:
+				command = event.arguments()[0].strip()
+				arguments = ''
+
+			command = irclib.irc_lower(command.lstrip('!'))
+
+			if command == 'help':
+				self.__help(connection, nick, admin, login, arguments)
+			elif command == 'login':
+				self.__login(connection, nickmask, nick, arguments)
+			elif command == 'status':
+				self.__status(connection, nick, login, arguments)
+			elif not login and command == 'register':
+				self.__register(connection, nick, arguments)
+			elif login and command == 'logout':
+				self.__logout(connection, nick)
+			elif login and command == 'unset':
+				self.__unset(connection, nick, login, arguments)
+			elif admin and command == 'reload':
+				self.__reload(connection, nick)
+			elif admin and command == 'restart':
+				self.__restart(connection)
+			elif admin and command == 'say':
+				self.__say(connection, nick, arguments)
+			elif admin and command == 'who':
+				self.__who(connection, nick)
+			else:
+				self.__unknown(connection, nick, command)
+		except psycopg2.Error, error:
+			print error
+
+			connection.privmsg(nick, 'an error occurred')
 
 	def on_quit(self, connection, event):
 		self.__logins.pop(irclib.nm_to_n(event.source()), None)