Browse Source

Feature request from Seth.

Douglas William Thrift 14 years ago
parent
commit
fca19d7d93
1 changed files with 19 additions and 9 deletions
  1. 19 9
      locationbot.py

+ 19 - 9
locationbot.py

@@ -173,7 +173,13 @@ class LocationBot(ircbot.SingleServerIRCBot):
 		self.__write()
 		connection.privmsg(nick, 'successfully joined channel ("%s")' % channel)
 
-	def __latitude(self):
+	def __latitude(self, latitude = None):
+		if latitude is not None:
+			feature = json.load(urllib2.urlopen('http://www.google.com/latitude/apps/badge/api?' + urllib.urlencode({'user': latitude, 'type': 'json'})))['features'][0]
+			properties = feature['properties']
+
+			return properties['reverseGeocode'], tuple(reversed(feature['geometry']['coordinates'])), datetime.fromtimestamp(properties['timeStamp'], pytz.utc)
+
 		now = datetime.utcnow()
 
 		try:
@@ -198,12 +204,8 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 				if latitude not in locations:
 					try:
-						feature = json.load(urllib2.urlopen('http://www.google.com/latitude/apps/badge/api?' + urllib.urlencode({'user': latitude, 'type': 'json'})))['features'][0]
-						coordinates = tuple(reversed(feature['geometry']['coordinates']))
-						properties = feature['properties']
-						new_location = properties['reverseGeocode']
+						new_location, coordinates, updated = self.__latitude(latitude)
 						locations[latitude] = new_location
-						updated = datetime.fromtimestamp(properties['timeStamp'], pytz.utc)
 					except Exception, error:
 						print error
 						continue
@@ -474,12 +476,20 @@ class LocationBot(ircbot.SingleServerIRCBot):
 
 				raise
 
+			connection.privmsg(nick, 'variable ("%s") successfully set to value ("%s")' % (variable, ' '.join(value) if isinstance(value, list) else '%f,%f' % value if isinstance(value, tuple) else value))
+
 			if variable == 'nick':
 				self.__logins[nick] = (value, nickmask)
-			elif variable in self.__geocode:
-				self.__location(nick, channels, old_location, new_location, coordinates)
+			elif variable in self.__geocode or variable == 'latitude':
+				if variable == 'latitude':
+					cursor.execute('select channels, location from locationbot.nick where nick = %s', (login,))
 
-			connection.privmsg(nick, 'variable ("%s") successfully set to value ("%s")' % (variable, ' '.join(value) if isinstance(value, list) else '%f,%f' % value if isinstance(value, tuple) else value))
+					channels, old_location = cursor.fetchone()
+					new_location, coordinates, updated = self.__latitude(value)
+
+					cursor.execute('update locationbot.nick set location = %s, coordinates = point %s, updated = %s where nick = %s', (new_location, coordinates, updated, login))
+
+				self.__location(login, channels, old_location, new_location, coordinates)
 
 	def __status(self, connection, nick, login, arguments):
 		_nick = arguments.split(None, 1)[0] if arguments else None