Exemple #1
0
def _safe_serve(params, client_ip, client_hostname, cookie_data):
    # Note: Only logging imports here
    from config import WORK_DIR
    from logging import basicConfig as log_basic_config

    # Enable logging
    try:
        from config import LOG_LEVEL

        log_level = _convert_log_level(LOG_LEVEL)
    except ImportError:
        from logging import WARNING as LOG_LEVEL_WARNING

        log_level = LOG_LEVEL_WARNING
    log_basic_config(filename=path_join(WORK_DIR, "server.log"), level=log_level)

    # Do the necessary imports after enabling the logging, order critical
    try:
        from common import ProtocolError, ProtocolArgumentError, NoPrintJSONError
        from dispatch import dispatch
        from jsonwrap import dumps
        from message import Messager
        from session import get_session, init_session, close_session, NoSessionError, SessionStoreError
    except ImportError:
        # Note: Heisenbug trap for #612, remove after resolved
        from logging import critical as log_critical
        from sys import path as sys_path

        log_critical("Heisenbug trap reports: " + str(sys_path))
        raise

    init_session(client_ip, cookie_data=cookie_data)
    response_is_JSON = True
    try:
        # Unpack the arguments into something less obscure than the
        #   Python FieldStorage object (part dictonary, part list, part FUBAR)
        http_args = DefaultNoneDict()
        for k in params:
            # Also take the opportunity to convert Strings into Unicode,
            #   according to HTTP they should be UTF-8
            try:
                http_args[k] = unicode(params.getvalue(k), encoding="utf-8")
            except TypeError:
                Messager.error(
                    "protocol argument error: expected string argument %s, got %s" % (k, type(params.getvalue(k)))
                )
                raise ProtocolArgumentError

        # Dispatch the request
        json_dic = dispatch(http_args, client_ip, client_hostname)
    except ProtocolError, e:
        # Internal error, only reported to client not to log
        json_dic = {}
        e.json(json_dic)

        # Add a human-readable version of the error
        err_str = str(e)
        if err_str != "":
            Messager.error(err_str, duration=-1)
Exemple #2
0
def _safe_serve(params, client_ip, client_hostname, cookie_data):
    # Note: Only logging imports here
    from config import WORK_DIR
    from logging import basicConfig as log_basic_config

    # Enable logging
    try:
        from config import LOG_LEVEL
        log_level = _convert_log_level(LOG_LEVEL)
    except ImportError:
        from logging import WARNING as LOG_LEVEL_WARNING
        log_level = LOG_LEVEL_WARNING
    log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
                     level=log_level)

    # Do the necessary imports after enabling the logging, order critical
    try:
        from common import ProtocolError, ProtocolArgumentError, NoPrintJSONError
        from dispatch import dispatch
        from jsonwrap import dumps
        from message import Messager
        from session import get_session, init_session, close_session, NoSessionError, SessionStoreError
    except ImportError:
        # Note: Heisenbug trap for #612, remove after resolved
        from logging import critical as log_critical
        from sys import path as sys_path
        log_critical('Heisenbug trap reports: ' + str(sys_path))
        raise

    init_session(client_ip, cookie_data=cookie_data)
    response_is_JSON = True
    try:
        # Unpack the arguments into something less obscure than the
        #   Python FieldStorage object (part dictonary, part list, part FUBAR)
        http_args = DefaultNoneDict()
        for k in params:
            # Also take the opportunity to convert Strings into Unicode,
            #   according to HTTP they should be UTF-8
            try:
                http_args[k] = unicode(params.getvalue(k), encoding='utf-8')
            except TypeError:
                Messager.error(
                    'protocol argument error: expected string argument %s, got %s'
                    % (k, type(params.getvalue(k))))
                raise ProtocolArgumentError

        # Dispatch the request
        json_dic = dispatch(http_args, client_ip, client_hostname)
    except ProtocolError, e:
        # Internal error, only reported to client not to log
        json_dic = {}
        e.json(json_dic)

        # Add a human-readable version of the error
        err_str = str(e)
        if err_str != '':
            Messager.error(err_str, duration=-1)
Exemple #3
0
def _safe_serve(params, client_ip, client_hostname, cookie_data):
    # Note: Only logging imports here
    from config import WORK_DIR
    from logging import basicConfig as log_basic_config

    # Enable logging
    try:
        from config import LOG_LEVEL
        log_level = _convert_log_level(LOG_LEVEL)
    except ImportError:
        from logging import WARNING as LOG_LEVEL_WARNING
        log_level = LOG_LEVEL_WARNING
    log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
            level=log_level)

    # Do the necessary imports after enabling the logging, order critical
    from common import ProtocolError, NoPrintJSONError
    from dispatch import dispatch
    from jsonwrap import dumps
    from message import Messager
    from session import get_session, init_session, close_session, NoSessionError

    init_session(client_ip, cookie_data=cookie_data)

    try:
        # Unpack the arguments into something less obscure than the
        #   Python FieldStorage object (part dictonary, part list, part FUBAR)
        http_args = DefaultNoneDict()
        for k in params:
            # Also take the opportunity to convert Strings into Unicode,
            #   according to HTTP they should be UTF-8
            http_args[k] = unicode(params.getvalue(k), encoding='utf-8')

        # Dispatch the request
        json_dic = dispatch(http_args, client_ip, client_hostname)
        response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
    except ProtocolError, e:
        # Internal error, only reported to client not to log
        json_dic = {}
        e.json(json_dic)

        # Add a human-readable version of the error
        err_str = str(e)
        if err_str != '':
            Messager.error(err_str)

        response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
Exemple #4
0
def _safe_serve(params, client_ip, client_hostname, cookie_data):
    # Note: Only logging imports here
    from config import WORK_DIR
    from logging import basicConfig as log_basic_config

    # Enable logging
    try:
        from config import LOG_LEVEL
        log_level = _convert_log_level(LOG_LEVEL)
    except ImportError:
        from logging import WARNING as LOG_LEVEL_WARNING
        log_level = LOG_LEVEL_WARNING
    log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
                     level=log_level)

    # Do the necessary imports after enabling the logging, order critical
    from common import ProtocolError, NoPrintJSONError
    from dispatch import dispatch
    from jsonwrap import dumps
    from message import Messager
    from session import get_session, init_session, close_session, NoSessionError

    init_session(client_ip, cookie_data=cookie_data)

    try:
        # Unpack the arguments into something less obscure than the
        #   Python FieldStorage object (part dictonary, part list, part FUBAR)
        http_args = DefaultNoneDict()
        for k in params:
            # Also take the opportunity to convert Strings into Unicode,
            #   according to HTTP they should be UTF-8
            http_args[k] = unicode(params.getvalue(k), encoding='utf-8')

        # Dispatch the request
        json_dic = dispatch(http_args, client_ip, client_hostname)
        response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
    except ProtocolError, e:
        # Internal error, only reported to client not to log
        json_dic = {}
        e.json(json_dic)

        # Add a human-readable version of the error
        err_str = str(e)
        if err_str != '':
            Messager.error(err_str)

        response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
Exemple #5
0
def import_summoners(data_path, verbose=0):
    s = session.init_session()

    # collect high ranks accounts
    queue = 'RANKED_SOLO_5x5'
    ranks = ['master', 'grandmaster', 'challenger']
    players = {}

    for rank in ranks:
        r = s.get(
            f'https://euw1.api.riotgames.com/lol/league/v4/{rank}leagues/by-queue/{queue}'
        )
        players[rank] = r.json()['entries']

    with open(f'{data_path}/raw/player_without_id.txt', 'w') as outfile:
        json.dump(players, outfile)

    if verbose == 1:
        for rank in ranks:
            print(rank, len(players[rank]))

    # link to summoner name
    counter = 0
    nb_errors = 0
    for rank in ranks:
        for player in players[rank]:
            if ((counter % 100) == 0) & (verbose == 1): print(counter)
            counter += 1
            name = player['summonerName']
            try:
                player['accountId'] = s.get(
                    f'https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/{name}'
                ).json()['accountId']
            except KeyError:
                nb_errors += 1
                players[rank].remove(player)
            # for API rate
            time.sleep(1.5)
    if verbose == 1: print(f'{nb_errors} errors')

    # export
    with open(f'{data_path}/raw/player_with_id.txt', 'w') as outfile:
        json.dump(players, outfile)
Exemple #6
0
def import_matchs(data_path, verbose):
    s = session.init_session()

    # structure
    colors = ['blue', 'red']
    colnames = ['champion', 'role', 'lane']
    numbers = range(5)

    # load match already stored if any
    try:
        matchs = pd.read_csv(f'{data_path}/matchs/matchs.csv',
                             sep=';',
                             index_col=0)
        # create temp save
        matchs.to_csv(f'{data_path}/matchs/matchs_tmp.csv', sep=';')
        if verbose == 1: print(len(matchs))
    except FileNotFoundError:
        matchs = pd.DataFrame(columns=['Id', 'time', 'version', 'winner'] + [
            f'{color}_{number}_{colname}' for color in colors
            for number in numbers for colname in colnames
        ])
    # id to champion
    dict_champ = id_to_champion(s)

    # read match ids
    match_id = pd.read_csv(f'{data_path}/raw/match_id.csv',
                           sep=';',
                           index_col=0,
                           header=None)
    match_id_to_query = set(match_id[1].to_list()) - set(
        matchs['Id'].to_list())

    # export every n iterations
    export_rate = 1000
    for counter, matchId in enumerate(match_id_to_query):
        res = parse_match(matchId, s, dict_champ, verbose)
        if res is not None:
            matchs.loc[matchId, :] = res
        if (counter % 100 == 0) & (verbose == 1): print(counter)
        time.sleep(1.5)
        if counter % export_rate == 0:
            matchs.to_csv(f'{data_path}/matchs/matchs.csv', sep=';')
Exemple #7
0
def import_matchs_id(data_path, verbose=0):
    s = session.init_session()

    # read players ID to use as keys for match history
    with open(f'{data_path}/raw/player_with_id.txt') as jsonfile:
        players = json.load(jsonfile)

    # we store match in set as their is a lot of duplicates
    all_match_id = set([])
    players_without_match = []

    counter = 0
    for rank, members in players.items():
        for member in members:
            if ((counter % 100) == 0) & (verbose):
                print(counter)
                print(len(players_without_match))
            counter += 1
            try:
                encryptedAccountId = member['accountId']
                member_matches = s.get(
                    f'https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/{encryptedAccountId}?queue=420'
                ).json()['matches']
                # efficient parsing of json instead of yet another for loop
                member_matches = json_normalize(
                    member_matches)['gameId'].to_list()
                all_match_id = all_match_id.union(set(member_matches))

            # no match in history
            except KeyError:
                players_without_match.append(member)

            time.sleep(1.5)
    if verbose == 1:
        print(f'{len(all_match_id)} matchs')
        print(f'{len(players_without_match)} players without match')

    # export
    pd.Series(list(all_match_id)).to_csv(f'{data_path}/raw/match_id.csv',
                                         sep=';')
Exemple #8
0
def _safe_serve(params, client_ip, client_hostname, cookie_data):
    # Note: Only logging imports here
    from config import WORK_DIR
    from logging import basicConfig as log_basic_config

    # Enable logging
    try:
        from config import LOG_LEVEL
        log_level = _convert_log_level(LOG_LEVEL)
    except ImportError:
        from logging import WARNING as LOG_LEVEL_WARNING
        log_level = LOG_LEVEL_WARNING
    log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
                     level=log_level)

    # Do the necessary imports after enabling the logging, order critical
    try:
        from common import ProtocolError, ProtocolArgumentError, NoPrintJSONError
        from dispatch import dispatch
        from jsonwrap import dumps
        from message import Messager
        from session import get_session, init_session, close_session, NoSessionError, SessionStoreError
    except ImportError:
        # Note: Heisenbug trap for #612, remove after resolved
        from logging import critical as log_critical
        from sys import path as sys_path
        log_critical('Heisenbug trap reports: ' + str(sys_path))
        raise

    init_session(client_ip, cookie_data=cookie_data)
    response_is_JSON = True
    try:
        # Unpack the arguments into something less obscure than the
        #   Python FieldStorage object (part dictonary, part list, part FUBAR)
        http_args = DefaultNoneDict()
        for k in params:
            # Also take the opportunity to convert Strings into Unicode,
            #   according to HTTP they should be UTF-8
            try:
                http_args[k] = params.getvalue(k)
            except TypeError as e:
                # Messager.error(e)
                Messager.error(
                    'protocol argument error: expected string argument %s, got %s' %
                    (k, type(
                        params.getvalue(k))))
                raise ProtocolArgumentError

        # Dispatch the request
        json_dic = dispatch(http_args, client_ip, client_hostname)
    except ProtocolError as e:
        # Internal error, only reported to client not to log
        json_dic = {}
        e.json(json_dic)

        # Add a human-readable version of the error
        err_str = str(e)
        if err_str != '':
            Messager.error(err_str, duration=-1)
    except NoPrintJSONError as e:
        # Terrible hack to serve other things than JSON
        response_data = (e.hdrs, e.data)
        response_is_JSON = False

    # Get the potential cookie headers and close the session (if any)
    try:
        cookie_hdrs = get_session().cookie.hdrs()
        close_session()
    except SessionStoreError:
        Messager.error(
            "Failed to store cookie (missing write permission to brat work directory)?", -1)
    except NoSessionError:
        cookie_hdrs = None

    if response_is_JSON:
        response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))

    return (cookie_hdrs, response_data)