Commits

Ada Young committed 39184ca

More error checking, some documentation here and there

Comments (0)

Files changed (1)

 
 browser = urllib2.build_opener()
 browser.addheaders = [
-        ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
-        ('Accept-Language', 'en-US,en;q=0.5'),
-        ('Connection', 'keep-alive'),
-        ('User-agent', '~*Ada\'s pretty script for getting orglogs*~'),
-    ]
+		('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
+		('Accept-Language', 'en-US,en;q=0.5'),
+		('Connection', 'keep-alive'),
+		('User-agent', '~*Ada\'s pretty script for getting orglogs*~ (Please see https://bitbucket.org/adayoung/adacode/src/master/irelogs.py)'),
+	]
+
 api_server = "http://api.achaea.com"
 orglogs_endpoint = "/orglogs.json"
 
 cursor = "A global object which we'll instantiate in main()"
 
 def createdb():
-    cursor.execute("PRAGMA foreign_keys=ON")
-    cursor.execute("""
-    CREATE TABLE IF NOT EXISTS orgs (
-        id INTEGER PRIMARY KEY AUTOINCREMENT,
-        orgname TEXT UNIQUE
-    )""")
-    cursor.execute("""
-    CREATE TABLE IF NOT EXISTS org_logs (
-        id INTEGER PRIMARY KEY AUTOINCREMENT,
-        timestamp TEXT,
-        message TEXT,
-        log_type TEXT,
-        orgid INTEGER REFERENCES orgs(id) ON DELETE CASCADE
-    )""")
-    cursor.execute("""
-    CREATE INDEX IF NOT EXISTS logs_index ON org_logs (orgid, timestamp, message)
-    """)
-    db.commit()
+	"""
+	createdb() -> None
+	This function creates the necessary tables for storing orglogs
+	"""
+	cursor.execute("PRAGMA foreign_keys=ON")
+	cursor.execute("""
+	CREATE TABLE IF NOT EXISTS orgs (
+		id INTEGER PRIMARY KEY AUTOINCREMENT,
+		orgname TEXT UNIQUE
+	)""")
+	cursor.execute("""
+	CREATE TABLE IF NOT EXISTS org_logs (
+		id INTEGER PRIMARY KEY AUTOINCREMENT,
+		timestamp TEXT,
+		message TEXT,
+		log_type TEXT,
+		orgid INTEGER REFERENCES orgs(id) ON DELETE CASCADE
+	)""")
+	cursor.execute("""
+	CREATE INDEX IF NOT EXISTS logs_index ON org_logs (orgid, timestamp, message)
+	""")
+	db.commit()
 
 def ProcessOrg(orgname):
-    cursor.execute("SELECT id FROM orgs WHERE orgname=?", (orgname, ))
-    results = cursor.fetchall()
-    if not len(results) > 0:
-        cursor.execute("INSERT INTO orgs (orgname) VALUES (?)", (orgname, ))
-        cursor.execute("SELECT last_insert_rowid()")
-        results = cursor.fetchone()
-        return results[0]
-    else:
-        return results[0][0]
-
-def ProcessLogs(orgname, lognotes, orgid, log_type="General"):
-    import_map = []
-    for i in lognotes:
-        splitlog = i.split('-')
-        timestamp = "-".join(splitlog[:3])
-        message = " ".join(splitlog[3:])[1:]
-        cursor.execute("SELECT * FROM org_logs l join orgs o on l.orgid=o.id WHERE o.orgname=? AND l.timestamp=datetime(?) AND l.message=? AND l.log_type=?", (orgname, timestamp, message, log_type))
-        results = cursor.fetchall()
-        if not len(results) > 0:
-            import_map.append((orgid, timestamp, message, log_type))
-    cursor.executemany("INSERT INTO org_logs (orgid, timestamp, message, log_type) VALUES (?, datetime(?), ?, ?)", import_map)
-    db.commit()
+	"""
+	ProcessOrg(orgname) -> orgid
+	This function returns an orgid for the given orgname while adding the
+	orgname to the database if it doesn't already exists
+	"""
+	cursor.execute("SELECT id FROM orgs WHERE orgname=?", (orgname, ))
+	results = cursor.fetchall()
+	if not len(results) > 0:
+		cursor.execute("INSERT INTO orgs (orgname) VALUES (?)", (orgname, ))
+		cursor.execute("SELECT last_insert_rowid()")
+		results = cursor.fetchone()
+		return results[0]
+	else:
+		return results[0][0]
+
+def ProcessLogs(lognotes, orgid, log_type="General"):
+	"""
+	ProcessLogs(lognotes, orgid, log_type) -> None
+	This function adds a list of lognotes for the given orgid to the database,
+	the log_type is "General" for now
+	"""
+	import_map = []
+	for i in lognotes:
+		splitlog = i.split('-')
+		timestamp = "-".join(splitlog[:3])
+		message = " ".join(splitlog[3:])[1:]
+		cursor.execute("SELECT * FROM org_logs l join orgs o on l.orgid=o.id WHERE o.id=? AND l.timestamp=datetime(?) AND l.message=? AND l.log_type=?", (orgid, timestamp, message, log_type))
+		results = cursor.fetchall()
+		if not len(results) > 0:
+			import_map.append((orgid, timestamp, message, log_type))
+	cursor.executemany("INSERT INTO org_logs (orgid, timestamp, message, log_type) VALUES (?, datetime(?), ?, ?)", import_map)
+	db.commit()
 
 def http_open(link):
+	"""
+	http_open(link) -> JSON object
+	This function fetches the given link and returns a json object
+	"""
 	print("Fetching: %s" % link)
 	try:
 		data = browser.open(link)
 		data = data.read()
 	except urllib2.HTTPError:
-		print("Encountered Http403 for %s" % link, sys.exc_info())
+		print("Encountered HTTPError for %s" % link, sys.exc_info())
 		print("Setting empty json object -> []")
 		data = "[]"
 	except:
 		print("Error while opening %s" % link, sys.exc_info())
 		exit()
+
+	try:
+		data = json.loads(data)
+	except:
+		print("Error decoding JSON object from %s" % link, sys.exc_info())
+		print("Setting empty JSON object -> []")
+		data = "[]"
+		data = json.loads(data)
 	return data
 
 def orglogs_summary(character, password, days=0):
 		})
 	orglogs = "%s%s" % (api_server, orglogs_endpoint)
 	dataset = http_open("%s?%s" % (orglogs, param_string))
-	dataset = json.loads(dataset)
 
 	orglogs = {}
 	for i in dataset:
 		orgid = ProcessOrg(orgname)
 
 		dataset = http_open(uri_set[i])
-		dataset = json.loads(dataset)
 
 		lognotes = []
 		for i in dataset:
 			lognote = i.get('event', '')
 			lognotes.append("%s - %s" % (timestamp, lognote))
 
-		ProcessLogs(orgname, lognotes, orgid)
+		ProcessLogs(lognotes, orgid)
 
 def main():
 	# options parser be here