diff --git a/setup.py b/setup.py index 6978741..27ca471 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup(name="standings", url="https://dev.pleaseignore.com/", keywords="eveapi", packages=['standings'], - package_data={'standings': ['templates/*.html']}, + package_data={'standings': ['templates/*']}, entry_points={ 'console_scripts': [ 'evestandings = standings.cli:main', diff --git a/standings/cli.py b/standings/cli.py index ed4148e..96cbf49 100644 --- a/standings/cli.py +++ b/standings/cli.py @@ -10,7 +10,7 @@ from standings import Standings def load_config(file='~/.evestandings.conf'): config = ConfigParser() - file = os.path.expanduser(file) + file = os.path.expandvars(os.path.expanduser(file)) if os.path.exists(file): config.read(file) outconfig = object() @@ -18,44 +18,44 @@ def load_config(file='~/.evestandings.conf'): setattr(outconfig, name, val) return outconfig else: - return {} + return object() def main(): parser = ArgumentParser(prog='EVEStandings', description='Outputs a EVE corporation/alliance standings to a HTML page') parser.add_argument('-k', '--keyid', help='Key ID of the API key') parser.add_argument('-v', '--vcode', help='vCode of the API key') - parser.add_argument('-c', '--character', help='Character whos corporation you wish to output') parser.add_argument('-t', '--type', help='Type of standings list, either corp or alliance') parser.add_argument('-C', '--config', help='Path to your configuration file') parser.add_argument('-f', '--output', help='Output the resulting HTML to a file') parser.add_argument('--template', help='Location of a customized template to use instead of the default') ns = parser.parse_args() + print ns - # if 'keyid' in ns or 'vcode' in ns: conf = ns else: - if 'config' in ns: - conf = load_config(ns['config']) + if ns.config: + conf = load_config(ns.config) else: conf = load_config() - if not conf.keyid or not conf.vcode: sys.stderr.write('Key ID or vCode is missing, please provide both on the command line or in the config file\n') parser.print_help() sys.exit(1) - print ns - obj = Standings(conf.keyid, conf.vcode, conf.character) - html = obj._get_html(conf.template) + obj = Standings(conf.keyid, conf.vcode) + if ns.template: + output = obj.render_template(ns.template) + else: + output = obj.html() if conf.output: f = open(os.path.expanduser(conf.output), 'w') - f.write(html) + f.write(output) f.close() else: - sys.stdout.write(html) + sys.stdout.write(output) sys.exit(0) diff --git a/standings/core.py b/standings/core.py index 44d26a0..fd9903a 100644 --- a/standings/core.py +++ b/standings/core.py @@ -1,92 +1,66 @@ -# -*- coding: utf-8 -*- - -import sys try: from collections import OrderedDict except ImportError: from ordereddict import OrderedDict -from eveapi import EVEAPIConnection, Error +from eveapi import EVEAPIConnection from jinja2 import Environment, PackageLoader -from .cache import DbCacheHandler - STANDINGS_ALLIANCE = 0 STANDINGS_CORPORATION = 1 -class Standings: - """ - Grabs the latest Standings from the EVE API and outputs them into - a nice template format - """ - def __init__(self, keyid, vcode, characterid, dbpath='/tmp/standingscache.sqlite3', type=STANDINGS_ALLIANCE): - self.eveapi = EVEAPIConnection(cacheHandler=DbCacheHandler(dbpath)).auth(keyID=keyid, vCode=vcode) - self.character = characterid +class Standings(object): + """Grabs the latest Standings from the EVE API and outputs them into a nice template format""" + + def __init__(self, keyid, vcode): + self.eveapi = EVEAPIConnection().auth(keyID=keyid, vCode=vcode) self.standings_type = type - @property - def _get_alliance_id_list(self): - if not hasattr(self, '_allianceids'): - self._allianceids = set([x.allianceID for x in EVEAPIConnection().eve.AllianceList().alliances]) - return self._allianceids + @staticmethod + def _parse_list(standingslist, output): + for row in standingslist: + level = float(row['standing']) + if level > 5: + type = 'excellent' + elif level > 0: + type = 'good' + elif level >= -5: + type = 'bad' + elif level >= -10: + type = 'terrible' + else: + type = 'neutral' - def _check_if_corp(self, corpid): - try: - res = EVEAPIConnection().corp.CorporationSheet(corporationID=corpid) - except Error: - return False - return True + if row['contactTypeID'] == 16159: + rowtype = 'alli' + elif row['contactTypeID'] == 2: + rowtype = 'corp' + else: + rowtype = 'char' + + output[type].append((rowtype, row['contactID'], row['contactName'], row['standing'])) def _get_standings(self): - res = self.eveapi.corp.ContactList(characterID=self.character) - - standings = OrderedDict() - for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']: standings[x] = [] - - def parse_list(list, output): - for row in list: - level = float(row['standing']) - if level > 5 and level <= 10: - type = 'excellent' - elif level > 0 and level <= 5: - type = 'good' - elif level < 0 and level >= -5: - type = 'bad' - elif level < -5 and level >= -10: - type = 'terrible' - else: - # Neutral? - type = 'neutral' - - if int(row['contactID']) in self._get_alliance_id_list: - rowtype = 'alli' - elif self._check_if_corp(int(row['contactID'])): - rowtype = 'corp' - else: - rowtype = 'char' - - output[type].append((rowtype, row['contactID'], row['contactName'], row['standing'])) - - # Order standings for each group - for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']: - standings[x] = sorted(standings[x], key=lambda v: -int(v[3])) - - - if self.standings_type == STANDINGS_ALLIANCE: - parse_list(res.allianceContactList, standings) - else: - parse_list(res.corporateContactList, standings) - + if hasattr(self, '_standings_cache'): + return self._standings_cache + res = self.eveapi.corp.ContactList() + standings = OrderedDict((x, []) for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']) + self._parse_list(res.allianceContactList, standings) + self._parse_list(res.corporateContactList, standings) + print standings + for x in ['excellent', 'good', 'neutral', 'bad', 'terrible']: + standings[x] = sorted(standings[x], key=lambda v: -int(v[3])) + self._standings_cache = standings return standings - def _get_name(self): - res = self.eveapi.corp.CorporationSheet() - if hasattr(res, 'allianceName'): return res.allianceName - return res.corporationName - - def _get_html(self, template='standings_list.html'): - if not template: template = 'standings_list.html' + def render_template(self, template): env = Environment(loader=PackageLoader('standings', 'templates')) template = env.get_template(template) - return template.render(standings=self._get_standings(), name=self._get_name()) + return template.render(standings=self._get_standings()) + + def html(self): + return self.render_template('standings_list.html') + + def text(self): + return self.render_template('standings_list.txt') diff --git a/standings/templates/standings_list.txt b/standings/templates/standings_list.txt new file mode 100644 index 0000000..7404125 --- /dev/null +++ b/standings/templates/standings_list.txt @@ -0,0 +1,8 @@ +{% for type in standings %} +{{ type }} + +{% for type, id, entname, standing in standings[type] %} +{{ standing }} - {{ entname }} +{% endfor %} + +{% endfor %} \ No newline at end of file