Commits

Ada Young committed 661ff33

Now we have an option parser! Use irelog.py -h to see the different options ^^ Also handling quirky 403s and no longer using 'count' since it's flaky

Comments (0)

Files changed (1)

-#!/bin/env python
+#!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
 import sys
-
 import json
 import urllib2
 import sqlite3
+import optparse
 from urllib import urlencode
 from datetime import datetime
 
         ('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"
 
+db = "A global object which we'll instantiate in main()"
+cursor = "A global object which we'll instantiate in main()"
+
 def createdb():
     cursor.execute("PRAGMA foreign_keys=ON")
     cursor.execute("""
 	try:
 		data = browser.open(link)
 		data = data.read()
+	except urllib2.HTTPError:
+		print("Encountered Http403 for %s" % link, sys.exc_info())
+		print("Setting empty json object -> []")
+		data = "[]"
 	except:
-		print("Error while opening %s\n" % link, sys.exc_info())
+		print("Error while opening %s" % link, sys.exc_info())
 		exit()
 	return data
 
 	param_string = urlencode({
 			"character" : character,
 			"password" : password,
-			"days" : days
+			"day" : days
 		})
 	orglogs = "%s%s" % (api_server, orglogs_endpoint)
 	dataset = http_open("%s?%s" % (orglogs, param_string))
 	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)
+		# if i.get('count', 0) > 0: # just get all the uris, the api server is broken
+		orglogs[orgname] = "%s?%s" % (i.get('uri', ''), param_string)
 
 	return orglogs
 
 		lognotes = []
 		for i in dataset:
 			timestamp = i.get('date', 0)
-			timestamp = datetime.fromtimestamp(int(timestamp)).__str__()
+			timestamp = datetime.fromtimestamp(int(timestamp)).strftime("%Y-%m-%d %H:%M:%S")
 
 			lognote = i.get('event', '')
 			lognotes.append("%s - %s" % (timestamp, lognote))
 		ProcessLogs(orgname, lognotes, orgid)
 
 def main():
+	# options parser be here
+	parser = optparse.OptionParser()
+	parser.add_option('-c', '--charname', help='Your Achaea character name')
+	parser.add_option('-p', '--password', help='Your Achaea password')
+	parser.add_option('-d', '--days', help='Number of days to go back (max: 6)')
+	parser.add_option('-D', '--database', help="The database file")
+	(opts, args) = parser.parse_args()
+
+	if opts.database is None:
+		print("You need to specify a database file\n")
+		parser.print_help()
+		exit(-1)
+	elif opts.charname is None:
+		print("You need to specify a charname\n")
+		parser.print_help()
+		exit(-1)
+	elif opts.password is None:
+		print("You need to specify a password")
+		parser.print_help()
+		exit(-1)
+
+	charname = opts.charname
+	password = opts.password
+	days = opts.days or 0
+
 	# init db if it isn't there yet
+	global cursor
+	global db
+
+	DBFile = opts.database
+	db = sqlite3.connect(DBFile)
+	cursor = db.cursor()
 	createdb()
 
+	dataset = orglogs_summary(charname, password, days)
+	dataset = process_orglogs(dataset)
 
 if __name__ == "__main__":
 	main()