-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsf_api.py
42 lines (34 loc) · 1.42 KB
/
msf_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests
import json
import os
def call_api(api_suffix, access_token):
headers = {
'Authorization': f'Bearer {access_token}',
'x-api-key': os.getenv("X-API-KEY")
}
response = requests.get('https://api.marvelstrikeforce.com/'+api_suffix, headers=headers)
return response.json()
def fetch_roster(access_token):
get_roster = call_api('player/v1/roster', access_token)
json.dump(get_roster, open('data/roster.json', 'w'))
def fetch_squad(access_token):
get_squads = call_api('player/v1/squads', access_token)
json.dump(get_squads, open('data/squads.json', 'w'))
def fetch_characters(access_token):
get_characters = fetch_all_pages('game/v1/characters?abilityKits=full', access_token)
print(f"Retrieved {len(get_characters)} characters")
json.dump(get_characters, open('data/character.json', 'w'))
def fetch_all_pages(api_suffix, access_token):
full_data = []
page = 1
per_page = 10 # Number of items per page
while True:
paginated_suffix = f"{api_suffix}&page={page}&perPage={per_page}"
data = call_api(paginated_suffix, access_token)
# Check if data is returned and has the expected structure
if 'data' in data and data['data']:
full_data.extend(data['data'])
page += 1 # Increment to request the next page
else:
break # Break loop if no more data to fetch
return full_data