From ef591224cdf093115a070d11c4b8f0d9260421d6 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:09:07 -0400 Subject: [PATCH 01/17] feat(speedtest): added the cog --- speedtest/__init__.py | 5 ++++ speedtest/info.json | 12 ++++++++++ speedtest/speedtest.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 speedtest/__init__.py create mode 100644 speedtest/info.json create mode 100644 speedtest/speedtest.py diff --git a/speedtest/__init__.py b/speedtest/__init__.py new file mode 100644 index 0000000..f91daf6 --- /dev/null +++ b/speedtest/__init__.py @@ -0,0 +1,5 @@ +from .speedtest import Speedtest + + +async def setup(bot): + await bot.add_cog(Speedtest(bot)) diff --git a/speedtest/info.json b/speedtest/info.json new file mode 100644 index 0000000..f331eac --- /dev/null +++ b/speedtest/info.json @@ -0,0 +1,12 @@ +{ + "author" : ["SeaswimmerTheFsh (seasw.)"], + "install_msg" : "Thank you for installing SeaUtils!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).", + "name" : "SeaUtils", + "short" : "A collection of useful utilities.", + "description" : "A collection of useful utilities.", + "end_user_data_statement" : "This cog does not store end user data.", + "hidden": true, + "disabled": false, + "min_bot_version": "3.5.0", + "min_python_version": [3, 8, 0] +} diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py new file mode 100644 index 0000000..bdfacda --- /dev/null +++ b/speedtest/speedtest.py @@ -0,0 +1,53 @@ +# _____ _ +# / ____| (_) +# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __ +# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__| +# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ | +# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| + +import json +import subprocess + +import discord +from redbot.core import commands +from redbot.core.bot import Red +from redbot.core.utils import chat_formatting as cf + + +class Speedtest(commands.Cog): + """A collection of random utilities.""" + + __author__ = ["SeaswimmerTheFsh"] + __version__ = "1.0.0" + + def __init__(self, bot: Red): + self.bot = bot + + def format_help_for_context(self, ctx: commands.Context) -> str: + pre_processed = super().format_help_for_context(ctx) or "" + n = "\n" if "\n\n" not in pre_processed else "" + text = [ + f"{pre_processed}{n}", + f"Cog Version: **{self.__version__}**", + f"Author: {cf.humanize_list(self.__author__)}" + ] + return "\n".join(text) + + @commands.command() + @commands.is_owner() + async def speedtest(self, ctx: commands.Context): + """Run a speedtest.""" + msg = await ctx.maybe_send_embed("Running speedtest...") + result = subprocess.run(["speedtest", "-f json", "--accept-license"], stdout=subprocess.PIPE) + r = json.loads(result.stdout.decode("utf-8")) + if await ctx.embed_requested(): + embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) + embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") + embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") + embed.set_image(url=r["result"]["image"] + ".png") + await msg.edit(embed=embed) + else: + await msg.edit(content=f"**Speedtest Results**\n" + f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n" + f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n" + f"**Results**: {r['result']['url']}") -- 2.45.2 From c3ab7593c77dd9e0887bf75b0f6ceb4ed8034813 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:19:48 -0400 Subject: [PATCH 02/17] fix(speedtest): bunch of fixes --- speedtest/speedtest.py | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index bdfacda..96ad02d 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -5,6 +5,7 @@ # ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ | # |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| +import asyncio import json import subprocess @@ -33,21 +34,35 @@ class Speedtest(commands.Cog): ] return "\n".join(text) + async def run_speedtest(self) -> str | asyncio.Any: + process = await asyncio.create_subprocess_exec( + "speedtest", "-f json", "--accept-license", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + stdout, stderr = await process.communicate() + if process.returncode != 0: + return stderr.decode("utf-8") + return json.loads(stdout.decode("utf-8")) + @commands.command() @commands.is_owner() - async def speedtest(self, ctx: commands.Context): + async def speedtest(self, ctx: commands.Context) -> None: """Run a speedtest.""" msg = await ctx.maybe_send_embed("Running speedtest...") - result = subprocess.run(["speedtest", "-f json", "--accept-license"], stdout=subprocess.PIPE) - r = json.loads(result.stdout.decode("utf-8")) - if await ctx.embed_requested(): - embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) - embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") - embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") - embed.set_image(url=r["result"]["image"] + ".png") - await msg.edit(embed=embed) - else: - await msg.edit(content=f"**Speedtest Results**\n" - f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n" - f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n" - f"**Results**: {r['result']['url']}") + async with ctx.typing(): + r = await self.run_speedtest() + if isinstance(r, str): + await msg.edit(content=f"An error occurred: {r}") + return + if await ctx.embed_requested(): + embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) + embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") + embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") + embed.set_image(url=r["result"]["image"] + ".png") + await msg.edit(embed=embed) + else: + await msg.edit(content=f"**Speedtest Results**\n" + f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n" + f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n" + f"**Results**: {r['result']['url']}") -- 2.45.2 From 14dfafea2a95b8c2fbdb6b3563b5f98c796d1452 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:20:33 -0400 Subject: [PATCH 03/17] fix(speedtest): fixed an AttributeError --- speedtest/speedtest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 96ad02d..4d16bc2 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -8,6 +8,7 @@ import asyncio import json import subprocess +from typing import Any import discord from redbot.core import commands @@ -34,7 +35,7 @@ class Speedtest(commands.Cog): ] return "\n".join(text) - async def run_speedtest(self) -> str | asyncio.Any: + async def run_speedtest(self) -> str | Any: process = await asyncio.create_subprocess_exec( "speedtest", "-f json", "--accept-license", stdout=subprocess.PIPE, -- 2.45.2 From 4795df7dcc451e2e8dbdfbe146f4adac7b24ab2d Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:21:33 -0400 Subject: [PATCH 04/17] fix(speedtest): added a error message for if speedtest is not installed --- speedtest/speedtest.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 4d16bc2..ea1daa4 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -36,11 +36,14 @@ class Speedtest(commands.Cog): return "\n".join(text) async def run_speedtest(self) -> str | Any: - process = await asyncio.create_subprocess_exec( - "speedtest", "-f json", "--accept-license", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE - ) + try: + process = await asyncio.create_subprocess_exec( + "speedtest", "-f json", "--accept-license", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + except FileNotFoundError: + return "Speedtest CLI is not installed." stdout, stderr = await process.communicate() if process.returncode != 0: return stderr.decode("utf-8") -- 2.45.2 From 8f55c6083a075691f7a91280a65962be4d71445e Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:24:04 -0400 Subject: [PATCH 05/17] fix(speedtest): improve an error message --- speedtest/speedtest.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index ea1daa4..1d10019 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -56,16 +56,19 @@ class Speedtest(commands.Cog): msg = await ctx.maybe_send_embed("Running speedtest...") async with ctx.typing(): r = await self.run_speedtest() - if isinstance(r, str): - await msg.edit(content=f"An error occurred: {r}") - return if await ctx.embed_requested(): + if isinstance(r, str): + await msg.edit(embed=discord.Embed(description=f"An error occurred! {r}", color=await discord.Colour.red())) + return embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") embed.set_image(url=r["result"]["image"] + ".png") await msg.edit(embed=embed) else: + if isinstance(r, str): + await msg.edit(content=f"An error occurred! {r}") + return await msg.edit(content=f"**Speedtest Results**\n" f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n" f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n" -- 2.45.2 From 7806aa29c596d1b6e72da1c2548233320cba6310 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:24:40 -0400 Subject: [PATCH 06/17] fix(speedtest): removed an await --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 1d10019..3e427f3 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -58,7 +58,7 @@ class Speedtest(commands.Cog): r = await self.run_speedtest() if await ctx.embed_requested(): if isinstance(r, str): - await msg.edit(embed=discord.Embed(description=f"An error occurred! {r}", color=await discord.Colour.red())) + await msg.edit(embed=discord.Embed(description=f"An error occurred! {r}", color=discord.Colour.red())) return embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") -- 2.45.2 From b210fa8bb023db8737eaef57a95d8c0555ed45da Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:37:35 -0400 Subject: [PATCH 07/17] fix(speedtest): hopefully fixed an error --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 3e427f3..901b9bd 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -38,7 +38,7 @@ class Speedtest(commands.Cog): async def run_speedtest(self) -> str | Any: try: process = await asyncio.create_subprocess_exec( - "speedtest", "-f json", "--accept-license", + program=["speedtest", "-f", "json", "--accept-license"], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) -- 2.45.2 From 30003cd7c8506e3a207cf584d3836618a03557f4 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:38:02 -0400 Subject: [PATCH 08/17] fix(speedtest) whoops! --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 901b9bd..08615bd 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -38,7 +38,7 @@ class Speedtest(commands.Cog): async def run_speedtest(self) -> str | Any: try: process = await asyncio.create_subprocess_exec( - program=["speedtest", "-f", "json", "--accept-license"], + "speedtest", "-f", "json", "--accept-license", stdout=subprocess.PIPE, stderr=subprocess.PIPE ) -- 2.45.2 From c2333f40efe6bc5675fa217a4a278523b5a2214b Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:38:52 -0400 Subject: [PATCH 09/17] fix(speedtest): fixed an incorrectly named kwarg --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 08615bd..aa8acec 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -60,7 +60,7 @@ class Speedtest(commands.Cog): if isinstance(r, str): await msg.edit(embed=discord.Embed(description=f"An error occurred! {r}", color=discord.Colour.red())) return - embed = discord.Embed(title="Speedtest Results", link=r["result"]["url"], color=await ctx.embed_color()) + embed = discord.Embed(title="Speedtest Results", url=r["result"]["url"], color=await ctx.embed_color()) embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") embed.set_image(url=r["result"]["image"] + ".png") -- 2.45.2 From eb41c1a72b4e973df343db5b812540d530c56573 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:40:02 -0400 Subject: [PATCH 10/17] fix(speedtest): fixed two keyerrors --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index aa8acec..165071a 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -62,7 +62,7 @@ class Speedtest(commands.Cog): return embed = discord.Embed(title="Speedtest Results", url=r["result"]["url"], color=await ctx.embed_color()) embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") - embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['igm'])} ms \nUpload: {round(r['upload']['latency']['igm'])} ms") + embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['iqm'])} ms \nUpload: {round(r['upload']['latency']['iqm'])} ms") embed.set_image(url=r["result"]["image"] + ".png") await msg.edit(embed=embed) else: -- 2.45.2 From e88cba6cf4562ecd93efd4f729e9e25d118dbf08 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:40:46 -0400 Subject: [PATCH 11/17] fix(speedtest): fixed another keyerror --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 165071a..a4429de 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -63,7 +63,7 @@ class Speedtest(commands.Cog): embed = discord.Embed(title="Speedtest Results", url=r["result"]["url"], color=await ctx.embed_color()) embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['iqm'])} ms \nUpload: {round(r['upload']['latency']['iqm'])} ms") - embed.set_image(url=r["result"]["image"] + ".png") + embed.set_image(url=r["result"]["url"] + ".png") await msg.edit(embed=embed) else: if isinstance(r, str): -- 2.45.2 From 45d0b3282653acc5185e4618f3809e72cbf7f219 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:55:05 -0400 Subject: [PATCH 12/17] feat(speedtest): convert to pydantic --- speedtest/info.json | 8 +++-- speedtest/models.py | 72 ++++++++++++++++++++++++++++++++++++++++++ speedtest/speedtest.py | 33 ++++++++++--------- 3 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 speedtest/models.py diff --git a/speedtest/info.json b/speedtest/info.json index f331eac..c36a53a 100644 --- a/speedtest/info.json +++ b/speedtest/info.json @@ -1,12 +1,14 @@ { "author" : ["SeaswimmerTheFsh (seasw.)"], - "install_msg" : "Thank you for installing SeaUtils!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).", - "name" : "SeaUtils", + "install_msg" : "Thank you for installing Speedtest!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs).", + "name" : "Speedtest", "short" : "A collection of useful utilities.", "description" : "A collection of useful utilities.", "end_user_data_statement" : "This cog does not store end user data.", "hidden": true, "disabled": false, "min_bot_version": "3.5.0", - "min_python_version": [3, 8, 0] + "min_python_version": [3, 10, 0], + "tags" : ["utility", "information"], + "requirements": ["pydantic"] } diff --git a/speedtest/models.py b/speedtest/models.py new file mode 100644 index 0000000..0a60699 --- /dev/null +++ b/speedtest/models.py @@ -0,0 +1,72 @@ +from datetime import datetime + +from pydantic import BaseModel + + +class Speedtest(BaseModel): + type: str + timestamp: datetime + ping: "Ping" + download: "Bandwidth" + upload: "Bandwidth" + isp: str + interface: "Interface" + server: "Server" + result: "Result" + + @classmethod + def from_json(cls, data: dict) -> "Speedtest": + return cls( + type=data["type"], + timestamp=datetime.fromisoformat(data["timestamp"]), + ping=Ping(**data["ping"]), + download=Bandwidth(**data["download"]), + upload=Bandwidth(**data["upload"]), + isp=data["isp"], + interface=Interface(**data["interface"]), + server=Server(**data["server"]), + result=Result(**data["result"]) + ) + +class Bandwidth(BaseModel): + bandwidth: float + bytes: int + elapsed: int + latency: "Latency" + +class Interface(BaseModel): + internalIp: str + name: str + macAddr: str + isVpn: bool + externalIp: str + +class Ping(BaseModel): + jitter: float + latency: float + low: float + high: float + +class Latency(BaseModel): + iqm: float + low: float + high: float + jitter: float + +class Server(BaseModel): + id: str + name: str + location: str + country: str + host: str + port: int + ip: str + +class Result: + id: str + url: str + persisted: bool + + @property + def image(self): + return self.url + ".png" diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index a4429de..9e522a6 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -8,13 +8,14 @@ import asyncio import json import subprocess -from typing import Any import discord from redbot.core import commands from redbot.core.bot import Red from redbot.core.utils import chat_formatting as cf +from .models import Speedtest as sp + class Speedtest(commands.Cog): """A collection of random utilities.""" @@ -35,7 +36,7 @@ class Speedtest(commands.Cog): ] return "\n".join(text) - async def run_speedtest(self) -> str | Any: + async def run_speedtest(self) -> str | sp: try: process = await asyncio.create_subprocess_exec( "speedtest", "-f", "json", "--accept-license", @@ -47,7 +48,7 @@ class Speedtest(commands.Cog): stdout, stderr = await process.communicate() if process.returncode != 0: return stderr.decode("utf-8") - return json.loads(stdout.decode("utf-8")) + return sp.from_json(json.loads(stdout.decode("utf-8"))) @commands.command() @commands.is_owner() @@ -55,21 +56,23 @@ class Speedtest(commands.Cog): """Run a speedtest.""" msg = await ctx.maybe_send_embed("Running speedtest...") async with ctx.typing(): - r = await self.run_speedtest() + speedtest = await self.run_speedtest() if await ctx.embed_requested(): - if isinstance(r, str): - await msg.edit(embed=discord.Embed(description=f"An error occurred! {r}", color=discord.Colour.red())) + if not isinstance(speedtest, sp): + await msg.edit(embed=discord.Embed(description=f"An error occurred! {speedtest}", color=discord.Colour.red())) return - embed = discord.Embed(title="Speedtest Results", url=r["result"]["url"], color=await ctx.embed_color()) - embed.add_field(name="Bandwidth", value=f"{r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up") - embed.add_field(name="Ping", value=f"Base: {round(r['ping']['latency'])} ms \nDownload: {round(r['download']['latency']['iqm'])} ms \nUpload: {round(r['upload']['latency']['iqm'])} ms") - embed.set_image(url=r["result"]["url"] + ".png") + embed = discord.Embed(title="Speedtest Results", url=speedtest.result.url, color=await ctx.embed_color()) + embed.add_field(name="Bandwidth", value=f"{speedtest.download.bandwidth / 1_000_000:.2f} Mbps down, {speedtest.download.bandwidth / 1_000_000:.2f} Mbps up") + embed.add_field(name="Ping", value=f"Base: {round(speedtest.ping.latency)} ms \nDownload: {round(speedtest.download.latency.iqm)} ms \nUpload: {round(speedtest.upload.latency.iqm)} ms") + embed.set_image(url=speedtest.result.image) await msg.edit(embed=embed) else: - if isinstance(r, str): - await msg.edit(content=f"An error occurred! {r}") + if not isinstance(speedtest, sp): + await msg.edit(content=f"An error occurred! {speedtest}") return await msg.edit(content=f"**Speedtest Results**\n" - f"**Bandwidth**: {r['download']['bandwidth'] / 1_000_000:.2f} Mbps down, {r['upload']['bandwidth'] / 1_000_000:.2f} Mbps up\n" - f"**Ping**: Base: {round(r['ping']['latency'])} ms, Download: {round(r['download']['latency']['igm'])} ms, Upload: {round(r['upload']['latency']['igm'])} ms\n" - f"**Results**: {r['result']['url']}") + f"**Bandwidth**: {speedtest.download.bandwidth / 1_000_000:.2f} Mbps down, {speedtest.download.bandwidth / 1_000_000:.2f} Mbps up\n" + f"**Ping**: Base: {round(speedtest.ping.latency)} ms\n" + f"Download: {round(speedtest.download.latency.iqm)} ms\n" + f"Upload: {round(speedtest.upload.latency.iqm)} ms\n" + f"**Image**: {speedtest.result.image}") -- 2.45.2 From 418eeb983cdf96d62e3a12777ff080bb4ddd5df9 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:56:40 -0400 Subject: [PATCH 13/17] fix(speedtest): fixed a pydantic validation error --- speedtest/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/models.py b/speedtest/models.py index 0a60699..cce2ebb 100644 --- a/speedtest/models.py +++ b/speedtest/models.py @@ -54,7 +54,7 @@ class Latency(BaseModel): jitter: float class Server(BaseModel): - id: str + id: int name: str location: str country: str -- 2.45.2 From d557928feec047a167e6850f29769f806fc51cc1 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 17:58:22 -0400 Subject: [PATCH 14/17] fix(speedtest): another pydantic fix --- speedtest/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speedtest/models.py b/speedtest/models.py index cce2ebb..89b520e 100644 --- a/speedtest/models.py +++ b/speedtest/models.py @@ -62,11 +62,11 @@ class Server(BaseModel): port: int ip: str -class Result: +class Result(BaseModel): id: str url: str persisted: bool @property - def image(self): + def image(self) -> str: return self.url + ".png" -- 2.45.2 From cba25d1f9439aff9996bb03e390722841eeb212d Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 18:08:18 -0400 Subject: [PATCH 15/17] misc(speedtest): doing some testing --- speedtest/models.py | 16 ++++++++++------ speedtest/speedtest.py | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/speedtest/models.py b/speedtest/models.py index 89b520e..cd1ca07 100644 --- a/speedtest/models.py +++ b/speedtest/models.py @@ -34,6 +34,16 @@ class Bandwidth(BaseModel): elapsed: int latency: "Latency" + @property + def mbps(self) -> float: + return self.bandwidth / 1_000_000 + +class Latency(BaseModel): + iqm: float + low: float + high: float + jitter: float + class Interface(BaseModel): internalIp: str name: str @@ -47,12 +57,6 @@ class Ping(BaseModel): low: float high: float -class Latency(BaseModel): - iqm: float - low: float - high: float - jitter: float - class Server(BaseModel): id: int name: str diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 9e522a6..0fe98f8 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -62,16 +62,16 @@ class Speedtest(commands.Cog): await msg.edit(embed=discord.Embed(description=f"An error occurred! {speedtest}", color=discord.Colour.red())) return embed = discord.Embed(title="Speedtest Results", url=speedtest.result.url, color=await ctx.embed_color()) - embed.add_field(name="Bandwidth", value=f"{speedtest.download.bandwidth / 1_000_000:.2f} Mbps down, {speedtest.download.bandwidth / 1_000_000:.2f} Mbps up") + embed.add_field(name="Bandwidth", value=f"{speedtest.download.mbps:,.2f} Mbps down, {speedtest.upload.mbps:,.2f} Mbps up") embed.add_field(name="Ping", value=f"Base: {round(speedtest.ping.latency)} ms \nDownload: {round(speedtest.download.latency.iqm)} ms \nUpload: {round(speedtest.upload.latency.iqm)} ms") embed.set_image(url=speedtest.result.image) await msg.edit(embed=embed) else: if not isinstance(speedtest, sp): - await msg.edit(content=f"An error occurred! {speedtest}") + await msg.edit(content=f"An error occurred! \n`{speedtest}`") return await msg.edit(content=f"**Speedtest Results**\n" - f"**Bandwidth**: {speedtest.download.bandwidth / 1_000_000:.2f} Mbps down, {speedtest.download.bandwidth / 1_000_000:.2f} Mbps up\n" + f"**Bandwidth**: {speedtest.download.mbps:,.2f} Mbps down, {speedtest.download.mbps:,.2f} Mbps up\n" f"**Ping**: Base: {round(speedtest.ping.latency)} ms\n" f"Download: {round(speedtest.download.latency.iqm)} ms\n" f"Upload: {round(speedtest.upload.latency.iqm)} ms\n" -- 2.45.2 From 94b692a7057c9c5b66ed7f8e6e5b26a61b32d934 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 18:12:45 -0400 Subject: [PATCH 16/17] fix(speedtest): adding a debugging statement --- speedtest/speedtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 0fe98f8..0176c04 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -71,7 +71,7 @@ class Speedtest(commands.Cog): await msg.edit(content=f"An error occurred! \n`{speedtest}`") return await msg.edit(content=f"**Speedtest Results**\n" - f"**Bandwidth**: {speedtest.download.mbps:,.2f} Mbps down, {speedtest.download.mbps:,.2f} Mbps up\n" + f"**Bandwidth**: {speedtest.download.mbps:,.2f} Mbps down ({speedtest.download.bandwidth}), {speedtest.download.mbps:,.2f} Mbps up\n" f"**Ping**: Base: {round(speedtest.ping.latency)} ms\n" f"Download: {round(speedtest.download.latency.iqm)} ms\n" f"Upload: {round(speedtest.upload.latency.iqm)} ms\n" -- 2.45.2 From 9103d64fbcc379d30d4a2b9a5ebfe368ca97a149 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Sat, 25 May 2024 18:17:00 -0400 Subject: [PATCH 17/17] fix(speedtest): just show the speedtest.net image --- speedtest/speedtest.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/speedtest/speedtest.py b/speedtest/speedtest.py index 0176c04..e727b0f 100644 --- a/speedtest/speedtest.py +++ b/speedtest/speedtest.py @@ -62,17 +62,10 @@ class Speedtest(commands.Cog): await msg.edit(embed=discord.Embed(description=f"An error occurred! {speedtest}", color=discord.Colour.red())) return embed = discord.Embed(title="Speedtest Results", url=speedtest.result.url, color=await ctx.embed_color()) - embed.add_field(name="Bandwidth", value=f"{speedtest.download.mbps:,.2f} Mbps down, {speedtest.upload.mbps:,.2f} Mbps up") - embed.add_field(name="Ping", value=f"Base: {round(speedtest.ping.latency)} ms \nDownload: {round(speedtest.download.latency.iqm)} ms \nUpload: {round(speedtest.upload.latency.iqm)} ms") embed.set_image(url=speedtest.result.image) await msg.edit(embed=embed) else: if not isinstance(speedtest, sp): await msg.edit(content=f"An error occurred! \n`{speedtest}`") return - await msg.edit(content=f"**Speedtest Results**\n" - f"**Bandwidth**: {speedtest.download.mbps:,.2f} Mbps down ({speedtest.download.bandwidth}), {speedtest.download.mbps:,.2f} Mbps up\n" - f"**Ping**: Base: {round(speedtest.ping.latency)} ms\n" - f"Download: {round(speedtest.download.latency.iqm)} ms\n" - f"Upload: {round(speedtest.upload.latency.iqm)} ms\n" - f"**Image**: {speedtest.result.image}") + await msg.edit(content=f"**[Result]({speedtest.result.url})**") -- 2.45.2