Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
METASCOUTER_API_USERNAME=
METASCOUTER_API_PASSWORD=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
16 changes: 16 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
python-language-server = "*"

[packages]
requests = "*"
numpy = "*"
matplotlib = "*"
seaborn = "*"

[requires]
python_version = "3.8"
259 changes: 259 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,41 @@
import json

# We need to retrieve the API authorization via JWT token before we can do anything
pwd = os.getenv('METASCOUTER_PWD')
username = os.getenv('METASCOUTER_API_USERNAME')
password = os.getenv('METASCOUTER_API_PASSWORD')
request = requests.post('https://api.metascouter.gg/auth/obtain-token/',
json={'username': 'Cyan', 'password': pwd})
json={'username': username, 'password': password})
if request.status_code == 200:
token = request.json()['token']
else:
print('ERROR RETRIEVING JWT TOKEN')
exit()

def memoize_api(func):
cache_file_location = os.getenv('API_DATA_CACHE',
os.path.join(os.path.curdir,
'.cache.json'))
if not os.path.exists(cache_file_location):
with open(cache_file_location, 'w') as cache_file:
json.dump({}, cache_file)

def f(url, *args, invalidate=False, **kwargs):
# Try to get this URL path from the cache
with open(cache_file_location, 'r') as cache_file:
cache = json.load(cache_file)
# If it exists and we don't want to invalidate the
# cache for this call, return the cached val
if cache.get(url) is not None and not invalidate:
return cache[url]
resp = func(url, *args, **kwargs)
cache[url] = resp

with open(cache_file_location, 'w') as cache_file:
json.dump(cache, cache_file)
return resp
return f

@memoize_api
def api_request(url):
jwt_auth = {'Authorization': 'JWT ' + token}
request = requests.get('https://api.metascouter.gg/ssbu/' + url, headers=jwt_auth)
Expand Down