Browse Source

Timeouts and set Latitude location.

Douglas William Thrift 14 years ago
parent
commit
d37574ac4d
1 changed files with 17 additions and 7 deletions
  1. 17 7
      locationbot.py

+ 17 - 7
locationbot.py

@@ -98,6 +98,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		self.__latitude_timer = None
 		self.__locations_lock = threading.Lock()
 		self.__quiting = False
+		self.__timeout = 5
 		self.__variables = frozenset(['nick', 'secret', 'masks', 'channels', 'timezone', 'location', 'coordinates', 'latitude'])
 		self.__geocode_variables = self.__variables.intersection(['location', 'coordinates'])
 		self.__lists = self.__variables.intersection(['masks', 'channels'])
@@ -141,7 +142,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 				except KeyError:
 					parameters['address'] = location
 
-		geocode = json.load(urllib2.urlopen('http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode(parameters)))
+		geocode = json.load(urllib2.urlopen('http://maps.google.com/maps/api/geocode/json?' + urllib.urlencode(parameters), timeout = self.__timeout))
 		status = geocode['status']
 
 		if status != 'OK':
@@ -259,7 +260,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 	def __latitude(self, granularity = None, token = None, secret = None):
 		if granularity is not None:
-			response, content = oauth.Client(self.__latitude_consumer, oauth.Token(token, secret)).request('https://www.googleapis.com/latitude/v1/currentLocation?' + urllib.urlencode({'granularity': granularity}), 'GET')
+			response, content = oauth.Client(self.__latitude_consumer, oauth.Token(token, secret), timeout = self.__timeout).request('https://www.googleapis.com/latitude/v1/currentLocation?' + urllib.urlencode({'granularity': granularity}), 'GET')
 
 			if int(response['status']) != 200:
 				raise Exception(content.strip())
@@ -489,7 +490,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 							return invalid(mask, 'mask')
 			elif variable == 'latitude':
 				if value in self.__latitude_granularities:
-					response, content = oauth.Client(self.__latitude_consumer).request('https://www.google.com/accounts/OAuthGetRequestToken', 'POST', urllib.urlencode({
+					response, content = oauth.Client(self.__latitude_consumer, timeout = self.__timeout).request('https://www.google.com/accounts/OAuthGetRequestToken', 'POST', urllib.urlencode({
 						'scope': 'https://www.googleapis.com/auth/latitude',
 						'oauth_callback': 'oob',
 					}))
@@ -509,7 +510,7 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 					token.set_verifier(value)
 
-					response, content = oauth.Client(self.__latitude_consumer, token).request('https://www.google.com/accounts/OAuthGetAccessToken', 'GET')
+					response, content = oauth.Client(self.__latitude_consumer, token, timeout = self.__timeout).request('https://www.google.com/accounts/OAuthGetAccessToken', 'GET')
 					status = int(response['status'])
 
 					if status == 400:
@@ -530,9 +531,9 @@ class LocationBot(ircbot.SingleServerIRCBot):
 						'oauth_token': token,
 					}))
 			elif variable in self.__geocode_variables:
-				cursor.execute('select channels, location from locationbot.nick where nick = %s', (login,))
+				cursor.execute('select channels, location, latitude.granularity, token, latitude.secret, authorized from locationbot.nick left join locationbot.latitude using (id) where nick = %s', (login,))
 
-				channels, old_location = cursor.fetchone()
+				channels, old_location, granularity, token, secret, authorized = cursor.fetchone()
 
 				if variable == 'location':
 					coordinates = None
@@ -553,7 +554,6 @@ class LocationBot(ircbot.SingleServerIRCBot):
 						if not -180.0 <= coordinate <= 180.0:
 							return invalid(value)
 
-					granularity = 'best'
 					location = None
 
 				geocode = self.__geocode(False, coordinates, location)
@@ -564,6 +564,16 @@ class LocationBot(ircbot.SingleServerIRCBot):
 					value = new_location
 				else:
 					value = coordinates
+
+				if authorized:
+					response, content = oauth.Client(self.__latitude_consumer, oauth.Token(token, secret), timeout = self.__timeout).request('https://www.googleapis.com/latitude/v1/currentLocation?' + urllib.urlencode({'granularity': granularity}), 'POST', json.dumps({'data': {
+						'kind': 'latitude#location',
+						'latitude': coordinates[0],
+						'longitude': coordinates[1],
+					}}), {'Content-Type': 'application/json'})
+
+					if int(response['status']) != 200:
+						raise Exception(content.strip())
 			elif variable == 'nick':
 				_nick = value.split(None, 1)