repl.it
@mat1/

skyblock api

Python

No description

fork
loading
Files
  • main.py
  • apikeys.py
  • clean.py
  • inventories.json
  • mojang.py
  • nbt.py
  • rank_colors.json
  • rank_real_names.json
  • skyblock.py
  • utils.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from aiohttp import web
import skyblock
import json
import time
from utils import json_dumps
import mojang

routes = web.RouteTableDef()

def json_response(data):
	return web.Response(
		text=json_dumps(data),
		content_type='application/json'
	)

def requires_key(func):
	def wrapper(*args, **kwargs):
		request = args[0]
		args = args[1:]

		if request.key is None:
			raise web.HTTPForbidden(
				text='Please provide API key'
			)
		return func(request, *args, **kwargs)
	return wrapper


@routes.get('/')
async def index(request):
	return {
		'ok': True
	}

@routes.get('/player/{user}')
@requires_key
async def fetch_player(request):
	print('fetch_player')
	user = request.match_info['user']
	show_profiles = request.query.get('profiles', 'true').lower() == 'true'
	print('show_profiles', show_profiles)
	show_profile_members = request.query.get('profile_members', True)
	if show_profiles:
		player_data = await skyblock.fetch_player_and_profiles(user, request.key, show_profile_members=show_profile_members)
	else:
		player_data = await skyblock.fetch_player(user, request.key)
	return player_data

@routes.get('/player/{user}/{profile}')
@requires_key
async def fetch_user_profiles(request):
	user = request.match_info['user']
	profile = request.match_info['profile']
	return await skyblock.fetch_profile_member(user, profile, request.key)

@routes.get('/full_profile/{uuid}')
@requires_key
async def fetch_full_profile(request):
	uuid = request.match_info['uuid']
	return await skyblock.fetch_full_profile(uuid, request.key)


@routes.get('/username/{uuid}')
async def fetch_username(request):
	uuid = request.match_info['uuid']
	username = await mojang.get_username_from_uuid(uuid)
	return {
		'username': username
	}

@web.middleware
async def middleware(request, handler):
	key = request.query.get('key')
	if not key:
		key = request.headers.get('key')
	request.key = key
	resp = await handler(request)
	if isinstance(resp, (dict, list)) or resp is None:
		resp = json_response(resp)
	return resp

app = web.Application(
	middlewares=[middleware]
)
app.add_routes(routes)
web.run_app(app)
?