diff --git a/wserver.py b/wserver.py index 49c856e..0dffd1b 100644 --- a/wserver.py +++ b/wserver.py @@ -15,38 +15,251 @@ LOGGER = logging.getLogger(__name__) routes = web.RouteTableDef() page = """ - - - - + + + + + + Torrent File Selector + + + + + + -

slam-mirrorbot: @Github

-
+ +
+ + +
+
+

Select the files you want to download

+ + {My_content} + + +
-{My_content} + - """ code_page = """ - - - - -Slam Torrent Files - - - + + + + + + Torrent Code Checker + + + + + + + -
-
-
- - - Dont mess around. You download will get messed up. -
- -
-
+ +
+
+ logo + +

slam-mirrorbot

+
+
+
+ + +
+
+
+
+
+ + +
+ +
+ * Dont mess around. Your download will get messed up. +
""" @@ -140,13 +551,13 @@ async def list_torrent_contents(request): rend_page = code_page.replace("{form_url}", f"/slam/files/{torr}") return web.Response(text=rend_page, content_type='text/html') - - client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") + client = qba.Client(host="localhost", port="8090", + username="admin", password="adminadmin") client.auth_log_in() try: - res = client.torrents_files(torrent_hash=torr) + res = client.torrents_files(torrent_hash=torr) except qba.NotFound404Error: - raise web.HTTPNotFound() + raise web.HTTPNotFound() count = 0 passw = "" for n in str(torr): @@ -161,17 +572,17 @@ async def list_torrent_contents(request): if gets["pin_code"] != pincode: return web.Response(text="Incorrect pin code") - par = nodes.make_tree(res) - - cont = ["",0] + + cont = ["", 0] nodes.create_list(par, cont) rend_page = page.replace("{My_content}", cont[0]) - rend_page = rend_page.replace("{form_url}", f"/slam/files/{torr}?pin_code={pincode}") + rend_page = rend_page.replace( + "{form_url}", f"/slam/files/{torr}?pin_code={pincode}") client.auth_log_out() return web.Response(text=rend_page, content_type='text/html') - + async def re_verfiy(paused, resumed, client, torr): @@ -183,10 +594,10 @@ async def re_verfiy(paused, resumed, client, torr): resumed = resumed.split("|") k = 0 while True: - + res = client.torrents_files(torrent_hash=torr) verify = True - + for i in res: if str(i.id) in paused: if i.priority == 0: @@ -202,18 +613,20 @@ async def re_verfiy(paused, resumed, client, torr): verify = False break - if not verify: LOGGER.error("Reverification Failed, correcting stuff...") client.auth_log_out() - client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") + lient = qba.Client(host="localhost", port="8090", + username="admin", password="adminadmin") client.auth_log_in() try: - client.torrents_file_priority(torrent_hash=torr, file_ids=paused, priority=0) + client.torrents_file_priority( + torrent_hash=torr, file_ids=paused, priority=0) except: LOGGER.error("Errored in reverification paused") try: - client.torrents_file_priority(torrent_hash=torr, file_ids=resumed, priority=1) + client.torrents_file_priority( + torrent_hash=torr, file_ids=resumed, priority=1) except: LOGGER.error("Errored in reverification resumed") client.auth_log_out() @@ -225,19 +638,19 @@ async def re_verfiy(paused, resumed, client, torr): return True - @routes.post('/slam/files/{hash_id}') async def set_priority(request): torr = request.match_info["hash_id"] - client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") + client = qba.Client(host="localhost", port="8090", + username="admin", password="adminadmin") client.auth_log_in() data = await request.post() resume = "" pause = "" data = dict(data) - + for i in data.keys(): if i.find("filenode") != -1: node_no = i.split("_")[-1] @@ -246,19 +659,21 @@ async def set_priority(request): resume += f"{node_no}|" else: pause += f"{node_no}|" - + pause = pause.strip("|") resume = resume.strip("|") - + try: - client.torrents_file_priority(torrent_hash=torr, file_ids=pause, priority=0) + client.torrents_file_priority( + torrent_hash=torr, file_ids=pause, priority=0) except qba.NotFound404Error: raise web.HTTPNotFound() except: LOGGER.error("Errored in paused") - + try: - client.torrents_file_priority(torrent_hash=torr, file_ids=resume, priority=1) + client.torrents_file_priority( + torrent_hash=torr, file_ids=resume, priority=1) except qba.NotFound404Error: raise web.HTTPNotFound() except: @@ -266,29 +681,32 @@ async def set_priority(request): await asyncio.sleep(2) if not await re_verfiy(pause, resume, client, torr): - LOGGER.error("The torrent choose errored reverification failed") + LOGGER.error("The Torrent choose errored reverification failed") client.auth_log_out() return await list_torrent_contents(request) + @routes.get('/') async def homepage(request): - return web.Response(text="

See slam-mirrorbot @GitHub By Breakdowns

",content_type="text/html") + return web.Response(text="

See slam-mirrorbot @GitHub By Breakdowns

", content_type="text/html") + async def e404_middleware(app, handler): - async def middleware_handler(request): + async def middleware_handler(request): + + try: + response = await handler(request) + if response.status == 404: + return web.Response(text="

404: Page not found


slam-mirrorbot

", content_type="text/html") + return response + except web.HTTPException as ex: + if ex.status == 404: + return web.Response(text="

404: Page not found


slam-mirrorbot

", content_type="text/html") + raise + return middleware_handler - try: - response = await handler(request) - if response.status == 404: - return web.Response(text="

404: Page not found


slam-mirrorbot

",content_type="text/html") - return response - except web.HTTPException as ex: - if ex.status == 404: - return web.Response(text="

404: Page not found


slam-mirrorbot

",content_type="text/html") - raise - return middleware_handler async def start_server(): @@ -296,10 +714,11 @@ async def start_server(): app.add_routes(routes) return app -async def start_server_async(port = 8080): + +async def start_server_async(port=8080): app = web.Application(middlewares=[e404_middleware]) app.add_routes(routes) runner = web.AppRunner(app) await runner.setup() - await web.TCPSite(runner,"0.0.0.0", port).start() + await web.TCPSite(runner, "0.0.0.0", port).start()