Esempio n. 1
0
def get_by_name(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    name = request.GET.get("name", None)
    if (name is None or region is None):
        return HttpResponseBadRequest("Missing arg name/region in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    if (len(name) < 3 or len(name) > 16):
        return HttpResponseBadRequest("Invalid summoner name.")

    # (check cache) If context is "current game lookup", as account data isn't going to change mid-match
    in_match_id = request.GET.get("inmatch", None)
    if in_match_id is not None:
        datajson = cache.get(
            'summoner_by_name' +
            base64.b64encode(name.encode('utf-8')).decode('utf-8') +
            'in_match' + in_match_id, None)
        if datajson:
            return HttpResponse(datajson)

    # (GET) Summoner basedata
    r = riotapi.get(riotapi.platform(region) +
                    "/lol/summoner/v3/summoners/by-name/" + name.lower(),
                    method="summoner/v3",
                    region=region)
    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound("Summoner not found.")
    if r['status'] == 200:
        summoner = r['data']
    else:
        return HttpResponseServerError(json.dumps(r))

    # https://developer.riotgames.com/api-methods/#summoner-v3/GET_getBySummonerName
    datajson = json.dumps({
        "id": summoner['id'],
        "name": summoner['name'],
        "icon": summoner['profileIconId'],
        "account": summoner['accountId'],
        "level": summoner['summonerLevel']
    })

    # (save to cache) If context is "current game lookup", as account data isn't going to change mid-match
    in_match_id = request.GET.get("inmatch", None)
    if in_match_id is not None:
        cache.set('summoner_by_name' +
                  base64.b64encode(name.encode('utf-8')).decode('utf-8') +
                  'in_match' + in_match_id, datajson, 30 * 60)  # 30 minutes

    return HttpResponse(datajson)
Esempio n. 2
0
def get_current_gameinfo(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    summoner_id = request.GET.get("summoner_id", None)
    if (region is None or summoner_id is None):
        return HttpResponseBadRequest(
            "Missing arg region/summoner_id in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    # (GET) Current gameinfo
    r = riotapi.get(riotapi.platform(region) +
                    "/lol/spectator/v3/active-games/by-summoner/" +
                    summoner_id,
                    method="spectator/v3",
                    region=region)

    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound(
            "Summoner not currently in a spectateable game")
    if r['status'] == 200:
        gameinfo = r['data']
        return HttpResponse(json.dumps(r['data']))
    else:
        return HttpResponseServerError(json.dumps(r))
Esempio n. 3
0
def get_past_ranked_flex_matchids(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    account_id = request.GET.get("account_id", None)
    if (region is None or account_id is None):
        return HttpResponseBadRequest(
            "Missing arg region/account_id in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    if not account_id.isdigit():
        return HttpResponseBadRequest("Invalid account id")

    # (check cache) If context is "current game lookup", as account data isn't going to change mid-match
    in_match_id = request.GET.get("inmatch", None)
    if in_match_id is not None:
        datajson = cache.get(
            'flexq_history_by_accountid' + account_id + 'in_match' +
            in_match_id, None)
        if datajson:
            return HttpResponse(datajson)

    # (GET) List of past games up to maxcount
    r = riotapi.get(riotapi.platform(region) +
                    "/lol/match/v3/matchlists/by-account/" + account_id +
                    "?queue=440" + "&season=" + str(CURRENT_SEASON),
                    method="match/v3/matchlist",
                    region=region)
    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound("No matching account was found.")
    if r['status'] == 200:
        history_matches = r['data']
    else:
        return HttpResponseServerError(json.dumps(r))

    # https://developer.riotgames.com/api-methods/#match-v3/GET_getMatchlist
    datajson = json.dumps(history_matches)

    # (save to cache) If context is "current game lookup", as game history isn't going to change mid-match
    in_match_id = request.GET.get("inmatch", None)
    if in_match_id is not None:
        cache.set('flexq_history_by_accountid' + account_id + 'in_match' +
                  in_match_id, datajson, 30 * 60)  # 30 minutes

    return HttpResponse(datajson)
Esempio n. 4
0
def get_single_timeline(request):

    # Fetch arguments
    match_id = request.GET.get("match_id", None)
    region = request.GET.get("region", None)
    if (match_id is None or region is None):
        return HttpResponseBadRequest(
            "Missing arg match_id/region in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    if not match_id.isdigit():
        return HttpResponseBadRequest("Invalid match id.")

    # Check if match-timeline exists in cache
    find_query = get_match_timeline(match_id, region.upper())
    if not find_query['success']:
        return HttpResponseServerError(find_query['data'])
    elif find_query['data'] is None:
        # Didn't exist so fetch
        r = riotapi.get(riotapi.platform(region) +
                        "/lol/match/v3/timelines/by-match/" + match_id,
                        method="match/v3",
                        region=region)

        if not r['success']:
            return HttpResponseServerError(json.dumps(r))
        if r['status'] == 404:
            return HttpResponseNotFound("No matching match was found.")
        if r['status'] == 200:
            # On success add it to cache and return
            update_query = add_match_timeline(match_id, region.upper(),
                                              json.dumps(r['data']))
            return HttpResponse(json.dumps(r['data'], indent=4))
        else:
            return HttpResponseServerError(json.dumps(r))
    else:
        # Match-timeline existed in cache so return it, it's not gonna need update anyway since it's from a singular old match
        return HttpResponse(json.dumps(find_query['data'], indent=4))
Esempio n. 5
0
def get_by_id(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    account_id = request.GET.get("account_id", None)
    if (account_id is None or region is None):
        return HttpResponseBadRequest(
            "Missing arg account_id/region in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    if not account_id.isdigit():
        return HttpResponseBadRequest("Invalid account id")

    # (GET) Summoner basedata
    r = riotapi.get(riotapi.platform(region) +
                    "/lol/summoner/v3/summoners/by-account/" + account_id,
                    method="summoner/v3",
                    region=region)
    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound("Summoner not found.")
    if r['status'] == 200:
        summoner = r['data']
    else:
        return HttpResponseServerError(json.dumps(r))

    # https://developer.riotgames.com/api-methods/#summoner-v3/GET_getByAccountId
    datajson = json.dumps({
        "id": summoner['id'],
        "name": summoner['name'],
        "icon": summoner['profileIconId'],
        "account": summoner['accountId'],
        "level": summoner['summonerLevel']
    })

    return HttpResponse(datajson)
Esempio n. 6
0
def get_odd_api_current_gameinfo(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    summoner_id = request.GET.get("summoner_id", None)
    if (region is None or summoner_id is None):
        return HttpResponseBadRequest(
            "Missing arg region/summoner_id in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    region_to_p = {"EUW": "EUW1", "EUNE": "EUN1"}
    platform = region_to_p[region.upper()] if (region.upper()
                                               in region_to_p) else None
    if platform is None:
        return HttpResponseServerError("Undefined platform for given region")

    if not summoner_id.isdigit():
        return HttpResponseBadRequest("Invalid summoner id")

    # (GET) Current gameinfo
    r = riotapi.get(
        "https://" + region.lower() +
        ".api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/" +
        platform + "/" + summoner_id,
        method="odd-api",
        region=region)
    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound(
            "Summoner not currently in a spectateable game")
    if r['status'] == 200:
        gameinfo = r['data']
        return HttpResponse(json.dumps(r['data']))
    else:
        return HttpResponseServerError(json.dumps(r))
Esempio n. 7
0
def get_all_uptodate(request):

    # Fetch arguments
    region = request.GET.get("region", None)
    summoner_id = request.GET.get("summoner_id", None)
    if (region is None or summoner_id is None):
        return HttpResponseBadRequest(
            "Missing arg region/summoner_id in querystring")

    # Validate arguments
    region_query = list_region()
    if not region_query['success']:
        return HttpResponseServerError(region_query['data'])

    if region.upper() not in region_query['data']:
        return HttpResponseBadRequest("Invalid or unsupported region.")

    if not summoner_id.isdigit():
        return HttpResponseBadRequest("Invalid summoner id")

    # (GET) Summoner championmasteries
    r = riotapi.get(
        riotapi.platform(region) +
        "/lol/champion-mastery/v3/champion-masteries/by-summoner/" +
        summoner_id,
        method="championmastery/v3",
        region=region)
    if not r['success']:
        return HttpResponseServerError(json.dumps(r))
    if r['status'] == 404:
        return HttpResponseNotFound("Summoner not found.")
    if r['status'] == 200:
        masteries = r['data']
    else:
        return HttpResponseServerError(json.dumps(r))

    # https://developer.riotgames.com/api-methods/#champion-mastery-v3/GET_getChampionMastery
    return HttpResponse(json.dumps(masteries))