Example #1
0
 def _start(self):
     self.cache = FileCache(path=self.cache_path)
     self.esi_app = EsiApp(cache=self.cache, cache_prefix=self.prefix)
     self.app = self.esi_app.get_latest_swagger
     self.security = EsiSecurity(
         app=self.app,
         redirect_uri='http://localhost/oauth-callback',  # This doesnt matter
         headers={
             'User-Agent':
             'Discord bot by Prozn: https://github.com/prozn/dankcord'
         },
         client_id=self.client_id,
         secret_key=self.secret_key,
     )
     self.esi = EsiClient(
         retry_requests=
         False,  # set to retry on http 5xx error (default False)
         headers={
             'User-Agent':
             'Discord bot by Prozn: https://github.com/prozn/dankcord'
         },
         security=self.security)
     self.security.update_token({
         'access_token': '',  # leave this empty
         'expires_in':
         -1,  # seconds until expiry, so we force refresh anyway
         'refresh_token': self.refresh_token
     })
     self.security.refresh()
Example #2
0
def download_from_esi(killmails):
    cache = FileCache(path=os.path.join(ROOT_DIR, 'cache/esipy_swagger'))
    esi_app = EsiApp(cache=cache, cache_time=60 * 60 * 24)
    app = esi_app.get_latest_swagger

    client = EsiClient(
        retry_requests=True,  # set to retry on http 5xx error (default False)
        headers={'User-Agent': 'Something CCP can use to contact you and that define your app'},
        raw_body_only=True,
        # default False, set to True to never parse response and only return raw JSON string content.
    )

    operations = []
    for killmail in killmails:
        operations.append(
            app.op['get_killmails_killmail_id_killmail_hash'](
                killmail_hash=killmail['zkb']['hash'],
                killmail_id=killmail['killmail_id']
            )
        )
    results = client.multi_request(operations)

    full_killmails = []
    for result in results:
        full_killmails.append(json.loads(result[1].raw))

    return full_killmails
Example #3
0
def config_esi_cache(cache_url):
    """Configure ESI cache backend

    Args:
        cache_url (string): diskcache or redis url

    Returns:
        cache: esipy.cache

    >>> config_esi_cache('diskcache:/tmp/esipy-diskcache') # doctest: +ELLIPSIS
    <esipy.cache.FileCache object at 0x...>
    >>> config_esi_cache('redis://*****:*****@127.0.0.1:6379/') # doctest: +ELLIPSIS
    <esipy.cache.RedisCache object at 0x...>
    """
    cache = DictCache()
    if cache_url:
        cache_url = urlparse(cache_url)
        if cache_url.scheme == 'diskcache':
            from esipy.cache import FileCache
            filename = cache_url.path
            cache = FileCache(path=filename)
        elif cache_url.scheme == 'redis':
            from esipy.cache import RedisCache
            import redis
            redis_server = cache_url.hostname
            redis_port = cache_url.port
            redis_client = redis.Redis(host=redis_server, port=redis_port)
            cache = RedisCache(redis_client)
    return cache
Example #4
0
import json

from esipy import App, EsiClient
from esipy.cache import FileCache
from esipy.exceptions import APIException

import config

esiapp = App.create(config.ESI_SWAGGER_JSON)

esiclient = EsiClient(
    # cache=RedisCache(Redis(config.REDIS_CACHE_HOST, port=config.REDIS_CACHE_PORT)),
    cache=FileCache('.webcache'),
    headers={'User-Agent': config.ESI_USER_AGENT})


def _get_esi(op, raw_body_only=False):
    """
    Try to get a result from an esipy request response tuple
    :param op: Request response tuple generated by esiapp.op
    :return: parsed result data
    :raises: `esipy.excpetions.APIException` if an ESI Exception is encountered
    """
    res = esiclient.request(op, raw_body_only=raw_body_only)

    if res.status == 200:
        if raw_body_only:
            return json.loads(res.raw.decode('utf-8'))
        else:
            return res.data
    else:
Example #5
0
 def setUp(self):
     shutil.rmtree('tmp', ignore_errors=True)
     self.c = FileCache('tmp')
Example #6
0
class TestFileCache(BaseTest):
    """ Class for testing the filecache """
    def setUp(self):
        shutil.rmtree('tmp', ignore_errors=True)
        self.c = FileCache('tmp')

    def tearDown(self):
        del self.c
        shutil.rmtree('tmp', ignore_errors=True)

    def test_file_cache_get_set(self):
        self.c.set(*self.ex_str)
        self.c.set(*self.ex_int)
        self.c.set(*self.ex_cpx)
        self.assertEqual(self.c.get(self.ex_str[0]), self.ex_str[1])
        self.assertEqual(self.c.get(self.ex_int[0]), self.ex_int[1])
        self.check_complex(self.c.get(self.ex_cpx[0]))

        self.c.set('expired', 'baz', -1)
        self.assertEqual(self.c.get('expired', 'default_because_expired'),
                         'default_because_expired')
        self.assertEqual(self.c.get('expired'), None)

    def test_file_cache_invalidate(self):
        self.c.set('key', 'bar')
        self.assertEqual(self.c.get('key'), 'bar')
        self.c.invalidate('key')
        self.assertEqual(self.c.get('key'), None)
Example #7
0
from .esi_security_proxy import EsiSecurityProxy
from esipy import EsiClient, EsiApp
from esipy.cache import FileCache

import wx

pyfalog = Logger(__name__)

cache_path = os.path.join(config.savePath, config.ESI_CACHE)

from esipy.events import AFTER_TOKEN_REFRESH

if not os.path.exists(cache_path):
    os.mkdir(cache_path)

file_cache = FileCache(cache_path)


class EsiException(Exception):
    pass


class Servers(Enum):
    TQ = 0
    SISI = 1


class LoginMethod(Enum):
    SERVER = 0
    MANUAL = 1
Example #8
0
from esipy import App
from esipy import EsiClient
from esipy import EsiSecurity
from esipy import EsiApp
from esipy.cache import FileCache
import pickle
import requests
import json
import datetime
import pytz
import logging
from sql_sde import *

from config import *

cache = FileCache(path="/tmp")

from esi_helper import esiChar
from esi_helper import esi_get_structure_information
from esi_helper import esi_get_status
from esi_helper import esi_get_info_for_typeid

from time import sleep

#FIXME 
#Adds in the value of rigs fitted to ships
#Damaged crystals are unsellable but still counted in the valuation
#Some weird encoding problems
#Log contract_ids we couldn't complete for further inspection
#Log 403 structures so we aren't wasting time checking them again
#Reduce the amount of calls when checking for rigs
Example #9
0
import json
import os

from esipy import EsiApp, EsiClient

from Core import _config
from Core.bot import logger

from esipy.cache import FileCache

meta_app = EsiApp()
esiapp = meta_app.get_latest_swagger

esiclient = EsiClient(
    retry_requests=True,
    cache=FileCache(path=os.path.join(os.path.dirname(__file__), '.webcache')),
    headers={'User-Agent': _config.ESI_USER_AGENT},
    raw_body_only=True)

# TODO Log warning headers. Not sure how to access the header. Googled around a bit, but couldn't find anything solid


def get_id(name, category):
    """
    Get the id of something
    :param name: name of something
    :param category: ESI category the something is part of
    :return: either a json object containing the something id or None
    """
    if len(name) > 2:
        response = search(name, category, True)
Example #10
0
class TestFileCache(BaseTest):
    """ Class for testing the filecache """

    def setUp(self):
        shutil.rmtree('tmp', ignore_errors=True)
        self.c = FileCache('tmp')

    def tearDown(self):
        del self.c
        shutil.rmtree('tmp', ignore_errors=True)

    def test_file_cache_get_set(self):
        self.c.set(*self.ex_str)
        self.c.set(*self.ex_int)
        self.c.set(*self.ex_cpx)
        self.assertEqual(self.c.get(self.ex_str[0]), self.ex_str[1])
        self.assertEqual(self.c.get(self.ex_int[0]), self.ex_int[1])
        self.check_complex(self.c.get(self.ex_cpx[0]))

    def test_file_cache_update(self):
        self.c.set(
            self.ex_cpx[0],
            CachedResponse(
                status_code=200,
                headers={'foo', 'test'},
                content='Nothing'.encode('latin-1'),
                url='http://foobarbaz.com'
            )
        )
        self.c.set(*self.ex_cpx)
        self.check_complex(self.c.get(self.ex_cpx[0]))

    def test_file_cache_invalidate(self):
        self.c.set('key', 'bar')
        self.assertEqual(self.c.get('key'), 'bar')
        self.c.invalidate('key')
        self.assertEqual(self.c.get('key'), None)

    def test_file_cache_expire(self):
        self.c.set('key', 'bar', expire=1)
        self.assertEqual(self.c.get('key'), 'bar')
        time.sleep(1)
        self.assertEqual(self.c.get('key', None), None)

        self.c.set('key', 'bar', expire=0)
        self.assertEqual(self.c.get('key'), 'bar')
        time.sleep(1)
        self.assertEqual(self.c.get('key', None), 'bar')

        self.c.set('foo', 'baz', expire=None)
        self.assertEqual(self.c.get('foo'), 'baz')
        time.sleep(1)
        self.assertEqual(self.c.get('foo', None), 'baz')