Commits

Ada Young  committed 23ee354

Ada's pretty script for getting orglogs, need to write the main function() stuff to query the db ^_^

  • Participants
  • Parent commits 9d61c6f

Comments (0)

Files changed (1)

+#!/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+
+import json
+import urllib2
+import sqlite3
+from urllib import urlencode
+from datetime import datetime
+
+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*~'),
+    ]
+
+DBFile = "orglogs.sqlite3"
+
+db = sqlite3.connect(DBFile)
+cursor = db.cursor()
+
+api_server = "http://api.achaea.com"
+orglogs_endpoint = "/orglogs.json"
+
+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()
+
+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()
+
+def http_open(link):
+	print("Fetching: %s" % link)
+	try:
+		data = browser.open(link)
+		data = data.read()
+	except:
+		print("Error while opening %s\n" % link, sys.exc_info())
+		exit()
+	return data
+
+def orglogs_summary(character, password, days=0):
+	param_string = urlencode({
+			"character" : character,
+			"password" : password,
+			"days" : days
+		})
+	orglogs = "%s%s" % (api_server, orglogs_endpoint)
+	dataset = http_open("%s?%s" % (orglogs, param_string))
+	dataset = json.loads(dataset)
+
+	orglogs = {}
+	for i in dataset:
+		orgname = i.get('name', 'NONAME')
+		if i.get('count', 0) > 0:
+			orglogs[orgname] = "%s?%s" % (i.get('uri', ''), param_string)
+
+	return orglogs
+
+def process_orglogs(uri_set):
+	for i in uri_set.keys():
+		orgname = i
+		orgid = ProcessOrg(orgname)
+
+		dataset = http_open(uri_set[i])
+		dataset = json.loads(dataset)
+
+		lognotes = []
+		for i in dataset:
+			timestamp = i.get('date', 0)
+			timestamp = datetime.fromtimestamp(int(timestamp)).__str__()
+
+			lognote = i.get('event', '')
+			lognotes.append("%s - %s" % (timestamp, lognote))
+
+		ProcessLogs(orgname, lognotes, orgid)
+
+def main():
+	# init db if it isn't there yet
+	createdb()
+
+
+if __name__ == "__main__":
+	main()