Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
api_examples [2020/11/10 07:52]
Jeftaei
api_examples [2020/11/10 19:03] (current)
Jeftaei
Line 1: Line 1:
 +=====Python=====
 +
 Below is the most basic thing you will need for starting API requests. Note: Currently this is only in python, if you are using something like javascript you will have to adapt this code to that language. Below is the most basic thing you will need for starting API requests. Note: Currently this is only in python, if you are using something like javascript you will have to adapt this code to that language.
  
Line 4: Line 6:
  
 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: "#" is a comment in python, this allows you to write anything without it being executed when running the file
  
  
-==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 21: Line 25:
 </code> </code>
  
-==API Requests with user input==+===API Requests with user input===
  
 <code python> <code python>
Line 43: Line 47:
 </code> </code>
  
-==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 82: Line 86:
  
 </code> </code>
 +
 +===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://github.com/fiveman1/rbhopdog|Fivemans Bot Github]]
 +<code python>
 +import json
 +import requests
 +
 +API_KEY = "api_key_goes_here"
 +URL = "https://api.strafes.net/v1/"
 +
 +headers = {
 +    "api-key":API_KEY,
 +}
 +
 +# function to request from the API without having to write the same lines every time
 +
 +def get(end_of_url, params): 
 +    res = requests.get(URL + end_of_url, headers=headers, params=params)
 +    if not res:
 +        print(res)
 +        print(res.text)
 +        raise Exception("request failed")
 +    else:
 +        return res
 +# this will pass in the API request we wanna invoke, in this case "map", and we pass the parameters in with it
 +bhop_maps = get("map", {
 +    "game":"1", 
 +    "page":"1" # note that alot of api requests that will give out 200 or more results will be paged, take this into account when using certain requests
 +}) 
 +total_pages = int(bhop_maps.headers["Pagination-Count"]) # cool page counting stuff to make sure we get every map
 +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("map", {
 +        "game":"1",
 +        "page":str(i)
 +    })
 +    bhop_map_data = bhop_map_data + m.json() # adding our newly requested maps to the other list
 +
 +# "with open()" will always close the file after its done using it, although it wont change anything to close it after just in case
 +with open("bhop_maps.json", "w") as file: # this will create a file if you dont already have one named this (so dont worry about making one)
 +    json.dump(bhop_map_data, file) # then it will dump the dictionary of maps we made into this file
 +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("map", {
 +    "game":"2",
 +    "page":"1"
 +})
 +total_pages = int(surf_maps.headers["Pagination-Count"])
 +surf_map_data = surf_maps.json()
 +for i in range(2, total_pages + 1):
 +    m = get("map", {
 +        "game":"2",
 +        "page":str(i)
 +    })
 +    surf_map_data = surf_map_data + m.json()
 +
 +with open("surf_maps.json", "w") as file:
 +    json.dump(surf_map_data, file)
 +file.close()
 +
 +</code>
 +
 +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://github.com/fiveman1/rbhopdog|Fivemans Bot Github]]
 +
 +<code python>
 +
 +bhop_maps = {}
 +with open("bhop_maps.json") as file: # this will open the file and read it then put the data into the bhop_maps variable
 +    data = file.read()
 +    bhop_maps = json.loads(data)
 +
 +surf_maps = {}
 +with open("surf_maps.json") as file: # this does the same but with surf
 +    data = file.read()
 +    surf_maps = json.loads(data)
 +    
 +def map_name_from_id(map_id, game): # heres the function that will give us the map name
 +    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("Invalid game") # raises an error if we dont give it a valid game id
 +    for m in map_data: # lopping through the map info in the maps file
 +        if m["ID"] == map_id: # checking if the current id of the map we are looking at is the same as the one we passed into the function
 +            return m["DisplayName"] # if it is, it returns the display name of the map
 +    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, game_id)
 +print(map_name)
 +
 +</code>
 +
Navigation