#!/usr/bin/env python3

STRUCTURE=1021149293700

from collections import defaultdict
from esipy import App, EsiSecurity, EsiClient
import codecs

FILE='refresh.code'

def init_esi():
    esi_app = App.create('https://esi.tech.ccp.is/latest/swagger.json?datasource=tranquility')
    esi_security = EsiSecurity(
        app=esi_app,
        redirect_uri='https://www.msully.net/stuff/get-token',
        client_id='fca36d677f9a4b8e8581d8cd2c738c2c',
        # 'The "Secret Key" should never be human-readable in your application.'
        secret_key=codecs.decode('AIUr5ntWiEIXiavPjKtUCiNFwlvTBlJqmElgAk4x',
                                 'rot_13'),
    )
    return (esi_app, esi_security)


def getRefreshToken():
    esi_app, esi_security = init_esi()
    url = esi_security.get_auth_uri(scopes=['esi-markets.structure_markets.v1'])
    print("Visit: ", url)
    print("Enter code: ", end = '')
    code = input()

    auth = esi_security.auth(code)
    refresh_token = auth['refresh_token']
    with open(FILE, 'w') as f:
        f.write(refresh_token)


def auth(esi_security):
    refresh = open(FILE).read().strip()
    esi_security.update_token({'refresh_token': refresh,
                               'access_token': None, 'expires_in': 0})
    esi_security.refresh()

def getOrders(structure=STRUCTURE):
    esi_app, esi_security = init_esi()
    auth(esi_security)
    esi_client = EsiClient(esi_security)
    # TODO: ok this might get paginated at some point and then we'll
    # need to do multiple pages??
    op=esi_app.op['get_markets_structures_structure_id'](
        structure_id=STRUCTURE
    )
    return esi_client.request(op).data

def summarizeOrders(orders):
    sells = [x for x in orders if not x.is_buy_order]
    groups = defaultdict(list)
    for order in sells:
        groups[order.type_id] += [order]

    summary = defaultdict(lambda:(0, 0))
    for id, orders in groups.items():
        price = min(x.price for x in orders)
        volume = sum(x.volume_remain for x in orders)
        summary[id] = (price, volume)

    return summary

#!/usr/bin/env python3

STRUCTURE=1021149293700

from collections import defaultdict, namedtuple
from esipy import App, EsiSecurity, EsiClient
import codecs

FILE='refresh.code'

ESI = namedtuple('ESI', ['app', 'security', 'client'])


def init_esi():
    esi_app = App.create('https://esi.tech.ccp.is/latest/swagger.json?datasource=tranquility')
    esi_security = EsiSecurity(
        app=esi_app,
        redirect_uri='https://www.msully.net/stuff/get-token',
        client_id='fca36d677f9a4b8e8581d8cd2c738c2c',
        # 'The "Secret Key" should never be human-readable in your application.'
        secret_key=codecs.decode('AIUr5ntWiEIXiavPjKtUCiNFwlvTBlJqmElgAk4x',
                                 'rot_13'),
    )
    esi_client = EsiClient(esi_security)
    return ESI(esi_app, esi_security, esi_client)

def getRefreshToken():
    esi = init_esi()
    url = esi.security.get_auth_uri(scopes=['esi-markets.structure_markets.v1',
                                            'esi-search.search_structures.v1'])
    print("Visit: ", url)
    print("Enter code: ", end = '')
    code = input()

    auth = esi.security.auth(code)
    refresh_token = auth['refresh_token']
    with open(FILE, 'w') as f:
        f.write(refresh_token)

def auth(esi):
    refresh = open(FILE).read().strip()
    esi.security.update_token({'refresh_token': refresh,
                               'access_token': None, 'expires_in': 0})
    esi.security.refresh()

def initAndAuth():
    esi = init_esi()
    auth(esi)
    return esi

def getOrders(structure=STRUCTURE):
    esi = initAndAuth()
    # TODO: ok this might get paginated at some point and then we'll
    # need to do multiple pages??
    op=esi.app.op['get_markets_structures_structure_id'](
        structure_id=STRUCTURE
    )
    return esi.client.request(op).data

def summarizeOrders(orders):
    sells = [x for x in orders if not x.is_buy_order]
    groups = defaultdict(list)
    for order in sells:
        groups[order.type_id] += [order]

    summary = defaultdict(lambda:(0, 0))
    for id, orders in groups.items():
        price = min(x.price for x in orders)
        volume = sum(x.volume_remain for x in orders)
        summary[id] = (price, volume)

    return summary

Reorganize stuff a bit