Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
api_examples [2020/11/10 15:46] Jeftaei |
api_examples [2020/11/10 19:03] (current) Jeftaei |
||
|---|---|---|---|
| Line 7: | Line 7: | ||
| Another Note: This is all for python 3.8.3+, I have no idea if this will work for earlier versions, or even later versions in some cases | Another Note: This is all for python 3.8.3+, I have no idea if this will work for earlier versions, or even later versions in some cases | ||
| + | Another Note: "#" | ||
| - | ==Basic interacting with API== | + | |
| + | ===Basic interacting with API=== | ||
| <code python> | <code python> | ||
| import requests # requests is a library that allows us to interact with a site | import requests # requests is a library that allows us to interact with a site | ||
| Line 23: | Line 25: | ||
| </ | </ | ||
| - | ==API Requests with user input== | + | ===API Requests with user input=== |
| <code python> | <code python> | ||
| Line 45: | Line 47: | ||
| </ | </ | ||
| - | ==Iterating API Requests== | + | ===Iterating API Requests=== |
| So you need to sort through the response the API gives you, heres one way, it might not be the best but it will work. | So you need to sort through the response the API gives you, heres one way, it might not be the best but it will work. | ||
| Line 84: | Line 86: | ||
| </ | </ | ||
| + | |||
| + | ===How do I get a map name from its id?=== | ||
| + | Im gonna straight up steal this from fivemans bot, since hes already made it and it works very very well | ||
| + | |||
| + | Theres 2 parts to this, first one will make the map files, second will give you a way to get the maps name, and creators if you wish | ||
| + | |||
| + | Credit: [[https:// | ||
| + | <code python> | ||
| + | import json | ||
| + | import requests | ||
| + | |||
| + | API_KEY = " | ||
| + | URL = " | ||
| + | |||
| + | headers = { | ||
| + | " | ||
| + | } | ||
| + | |||
| + | # function to request from the API without having to write the same lines every time | ||
| + | |||
| + | def get(end_of_url, | ||
| + | res = requests.get(URL + end_of_url, headers=headers, | ||
| + | if not res: | ||
| + | print(res) | ||
| + | print(res.text) | ||
| + | raise Exception(" | ||
| + | else: | ||
| + | return res | ||
| + | # this will pass in the API request we wanna invoke, in this case " | ||
| + | bhop_maps = get(" | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | total_pages = int(bhop_maps.headers[" | ||
| + | bhop_map_data = bhop_maps.json() | ||
| + | for i in range(2, total_pages + 1): # looping through all the pages we need to see, in this case 2, (might need to be changed when maps get added again) | ||
| + | m = get(" | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | bhop_map_data = bhop_map_data + m.json() # adding our newly requested maps to the other list | ||
| + | |||
| + | # "with open()" | ||
| + | with open(" | ||
| + | json.dump(bhop_map_data, | ||
| + | file.close() # fiveman dumb and closes the file anyway | ||
| + | |||
| + | |||
| + | # this is the exact same stuff just for surf instead of bhop maps | ||
| + | surf_maps = get(" | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | total_pages = int(surf_maps.headers[" | ||
| + | surf_map_data = surf_maps.json() | ||
| + | for i in range(2, total_pages + 1): | ||
| + | m = get(" | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | surf_map_data = surf_map_data + m.json() | ||
| + | |||
| + | with open(" | ||
| + | json.dump(surf_map_data, | ||
| + | file.close() | ||
| + | |||
| + | </ | ||
| + | |||
| + | Heres how we will get the names of the maps in the 2 new files we just made, also stolen from fivemans bot since im lazy | ||
| + | |||
| + | Credit: [[https:// | ||
| + | |||
| + | <code python> | ||
| + | |||
| + | bhop_maps = {} | ||
| + | with open(" | ||
| + | data = file.read() | ||
| + | bhop_maps = json.loads(data) | ||
| + | |||
| + | surf_maps = {} | ||
| + | with open(" | ||
| + | data = file.read() | ||
| + | surf_maps = json.loads(data) | ||
| + | | ||
| + | def map_name_from_id(map_id, | ||
| + | game = game # this specifies game we passed in when invoking the function | ||
| + | if game == 1: # simple checking which game we specified | ||
| + | map_data = bhop_maps | ||
| + | elif game == 2: | ||
| + | map_data = surf_maps | ||
| + | else: | ||
| + | raise Exception(" | ||
| + | for m in map_data: # lopping through the map info in the maps file | ||
| + | if m[" | ||
| + | return m[" | ||
| + | return "Map name not found" # if it finishes the loop and doesnt find any maps with that id, it will simply return "map not found" | ||
| + | |||
| + | # Example of how to invoke this function | ||
| + | |||
| + | map_name = map_name_from_id(map_id, | ||
| + | print(map_name) | ||
| + | |||
| + | </ | ||
| + | |||