Browse Source

Fixy! And work without geojson.

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

+ 17 - 8
locationbot.py

@@ -7,7 +7,6 @@
 
 from ConfigParser import NoOptionError, SafeConfigParser
 from datetime import datetime, timedelta
-import geojson
 import getpass
 import ircbot
 import irclib
@@ -20,6 +19,11 @@ import threading
 import urllib, urllib2
 import warnings
 
+try:
+	import simplejson as json
+except ImportError:
+	import json
+
 class LocationBot(ircbot.SingleServerIRCBot):
 	def __init__(self, bot = None):
 		config = SafeConfigParser({'hostname': ''})
@@ -165,19 +169,22 @@ class LocationBot(ircbot.SingleServerIRCBot):
 					continue
 
 				try:
-					geo = geojson.load(response, object_hook = geojson.GeoJSON.to_instance)
+					geo = json.load(response)
 				except (TypeError, ValueError), error:
 					print error
 					continue
 
-				if len(geo.features):
-					properties = geo.features[0].properties
+				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:
@@ -339,13 +346,15 @@ class LocationBot(ircbot.SingleServerIRCBot):
 			connection.privmsg(nick, '%-23s %s' % login)
 
 	def _connect(self):
-		password = None
-
 		if len(self.server_list[0]) != 2:
 			ssl = self.server_list[0][2]
+		else:
+			ssl = False
 
 		if len(self.server_list[0]) == 4:
 			password = self.server_list[0][3]
+		else:
+			password = None
 
 		try:
 			with warnings.catch_warnings():