Browse Source

Add /beatmap and /beatmapset api handlers

Giuseppe Guerra 1 year ago
parent
commit
a3fb228142
3 changed files with 56 additions and 1 deletions
  1. 5
    1
      tempmirror.py
  2. 27
    0
      web/handlers/beatmap.py
  3. 24
    0
      web/handlers/beatmapset.py

+ 5
- 1
tempmirror.py View File

@@ -3,9 +3,11 @@ from multiprocessing.pool import ThreadPool
3 3
 
4 4
 import tornado.ioloop
5 5
 
6
+from web.handlers import listing
7
+from web.handlers import beatmapset
8
+from web.handlers import beatmap
6 9
 from db import pool
7 10
 from objects import glob
8
-from web.handlers import listing
9 11
 from utils import console
10 12
 from constants import bcolors
11 13
 from objects import config
@@ -50,5 +52,7 @@ print("> Web server started on 0.0.0.0:{}".format(glob.config.config["server"]["
50 52
 glob.pool = ThreadPool(glob.config.config["server"]["threads"])
51 53
 tornado.web.Application([
52 54
 	(r"/listing", listing.Handler),
55
+	(r"/beatmapset", beatmapset.Handler),
56
+	(r"/beatmap", beatmap.Handler),
53 57
 ]).listen(glob.config.config["server"]["port"])
54 58
 tornado.ioloop.IOLoop.instance().start()

+ 27
- 0
web/handlers/beatmap.py View File

@@ -0,0 +1,27 @@
1
+from web.api import api
2
+from objects import glob
3
+from objects import exceptions
4
+
5
+class Handler(api.AsyncAPIHandler):
6
+	@api.api
7
+	@api.args("id")
8
+	def async_get(self, *args, **kwargs):
9
+		try:
10
+			beatmap_id = int(self.get_argument("id"))
11
+		except (TypeError, ValueError):
12
+			raise exceptions.InvalidArgumentsError()
13
+
14
+		beatmap_data = glob.db.fetch("SELECT beatmapset_id FROM child_beatmaps WHERE beatmap_id = %s LIMIT 1", [beatmap_id])
15
+		if beatmap_data is None:
16
+			raise exceptions.NotFoundError()
17
+		beatmapset_data = glob.db.fetch("SELECT * FROM beatmapsets WHERE beatmapset_id = %s LIMIT 1", [beatmap_data["beatmapset_id"]])
18
+		if beatmapset_data is None:
19
+			raise exceptions.NotFoundError()
20
+
21
+		self.data["data"] = {
22
+			"beatmapset_id": beatmap_data["beatmapset_id"],
23
+			"artist": beatmapset_data["artist"],
24
+			"title": beatmapset_data["title"],
25
+			"creator": beatmapset_data["creator"],
26
+			"ranked_status": beatmapset_data["ranked_status"],
27
+		}

+ 24
- 0
web/handlers/beatmapset.py View File

@@ -0,0 +1,24 @@
1
+from web.api import api
2
+from objects import glob
3
+from objects import exceptions
4
+
5
+class Handler(api.AsyncAPIHandler):
6
+	@api.api
7
+	@api.args("id")
8
+	def async_get(self, *args, **kwargs):
9
+		try:
10
+			beatmapset_id = int(self.get_argument("id"))
11
+		except (TypeError, ValueError):
12
+			raise exceptions.InvalidArgumentsError()
13
+
14
+		beatmapset_data = glob.db.fetch("SELECT * FROM beatmapsets WHERE beatmapset_id = %s LIMIT 1", [beatmapset_id])
15
+		if beatmapset_data is None:
16
+			raise exceptions.NotFoundError()
17
+
18
+		self.data["data"] = {
19
+			"beatmapset_id": beatmapset_id,
20
+			"artist": beatmapset_data["artist"],
21
+			"title": beatmapset_data["title"],
22
+			"creator": beatmapset_data["creator"],
23
+			"ranked_status": beatmapset_data["ranked_status"],
24
+		}

Loading…
Cancel
Save