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)
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))
def refresh_all_summonerspell_metadata(request): # (GET) dump of SELECTED summonerspell metadata from STATIC API r = riotapi.get(riotapi.platform("EUW") + "/lol/static-data/v3/summoner-spells" + "?spellListData=range" + "&spellListData=cooldown", is_static=True) if not r['success']: return HttpResponseServerError(json.dumps(r)) if r['status'] == 200: itemdata = r['data'] else: return HttpResponseServerError(json.dumps(r)) # Update Summonerspells' data to/in cache update_query = replace_summonerspell_metadata(json.dumps( r['data']['data'])) if not update_query['success']: return HttpResponseServerError(update_query['data']) else: pass # Successfully updated return JsonResponse({ "success": True, "data": "Successfully updated all Summonerspell metadata" })
def refresh_all_item_metadata(request): # (GET) dump of SELECTED Items' metadata from STATIC API r = riotapi.get(riotapi.platform("EUW") + "/lol/static-data/v3/items" + "?itemListData=from" + "&itemListData=into" + "&itemListData=gold" + "&itemListData=image" + "&itemListData=stats", is_static=True) if not r['success']: return HttpResponseServerError(json.dumps(r)) if r['status'] == 200: itemdata = r['data'] else: return HttpResponseServerError(json.dumps(r)) # Update Itemdata to/in cache items_by_key = itemdata['data'] for k in items_by_key: items_by_key[k]["ddragon_key"] = items_by_key[k]['image']['full'] del items_by_key[k]['image'] update_query = replace_item_metadata(json.dumps(items_by_key)) if not update_query['success']: return HttpResponseServerError(update_query['data']) else: pass # Successfully updated return JsonResponse({ "success": True, "data": "Successfully updated all Item metadata" })
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)
def update_version(): r = riotapi.get( "https://euw1.api.riotgames.com/lol/static-data/v3/versions", is_static=True) if not r['success']: return False newest_euw_version = r['data'][0] with open(DATADRAGON_DAILY_VERSION_FILEPATH, 'w') as fh: json.dump( { 'epoch_sec_timestamp': time.time(), 'version': newest_euw_version }, fh) return True
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))
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)
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))
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))
def refresh_all_champion_metadata(request): # (GET) dump of all Champion metadata from STATIC API r = riotapi.get(riotapi.platform("EUW") + "/lol/static-data/v3/champions" + "?champListData=allytips" + "&champListData=enemytips" + "&champListData=passive" + "&champListData=spells", is_static=True) if not r['success']: return HttpResponseServerError(json.dumps(r)) if r['status'] == 200: championdata = r['data'] else: return HttpResponseServerError(json.dumps(r)) # Re-structure dataset champions = [] champion_skills = [] champion_tips = [] for champion_key in championdata['data']: champion = championdata['data'][champion_key] # Add champion <=> ID mapping champions.append({ "id": champion['id'], "name": champion['name'], "ddragon_key": champion['key'] }) # Add passive skill champion_skills.append({ "champion": champion['id'], "name": champion['passive']['name'], "description": champion['passive']['description'], "cooldowns": "-1", # this is argueable but meh... "slot": 0 }) # Add QWER champion_skills.extend([{ "champion": champion['id'], "name": skill['name'], "description": skill['description'], "cooldowns": "/".join(map(lambda n: str(n), skill['cooldown'])), "slot": (i + 1) } for i, skill in enumerate(champion['spells'])]) # Add tips "for" champion_tips.extend([{ "champion": champion['id'], "vs": "FALSE", "description": tip } for tip in champion['allytips']]) # Add tips "against" champion_tips.extend([{ "champion": champion['id'], "vs": "TRUE", "description": tip } for tip in champion['enemytips']]) # Update Champion basedata to/in cache update_query = replace_champion_metadata(champions, champion_skills, champion_tips) if not update_query['success']: return HttpResponseServerError(update_query['data']) else: pass # Successfully updated return JsonResponse({ "success": True, "data": "Successfully updated all Champion metadata" })