Source

AdaCode / irelogs.py

#!/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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.