Last active
December 10, 2024 10:37
-
-
Save ixtk/cef7d07e45e1978bff0afac9de18d2b2 to your computer and use it in GitHub Desktop.
Python code example of OSU! API v2 usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from pprint import pprint | |
from os import getenv | |
API_URL = 'https://osu.ppy.sh/api/v2' | |
TOKEN_URL = 'https://osu.ppy.sh/oauth/token' | |
def get_token(): | |
data = { | |
'client_id': getenv('CLIENT_ID'), | |
'client_secret': getenv('CLIENT_SECRET'), | |
'grant_type': 'client_credentials', | |
'scope': 'public' | |
} | |
response = requests.post(TOKEN_URL, data=data) | |
return response.json().get('access_token') | |
def main(): | |
token = get_token() | |
headers = { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
'Authorization': f'Bearer {token}' | |
} | |
params = { | |
'mode': 'osu', | |
'limit': 5 | |
} | |
response = requests.get(f'{API_URL}/users/15810304/scores/best', params=params, headers=headers) | |
beatmapset_data = response.json()[0].get('beatmapset') | |
pprint(beatmapset_data, indent=2) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for making this example, I used it and it worked perfectly.