Compare commits

...

17 commits

Author SHA1 Message Date
9103d64fbc
fix(speedtest): just show the speedtest.net image 2024-05-25 18:17:00 -04:00
94b692a705
fix(speedtest): adding a debugging statement 2024-05-25 18:12:45 -04:00
cba25d1f94
misc(speedtest): doing some testing 2024-05-25 18:08:18 -04:00
d557928fee
fix(speedtest): another pydantic fix 2024-05-25 17:58:22 -04:00
418eeb983c
fix(speedtest): fixed a pydantic validation error 2024-05-25 17:56:40 -04:00
45d0b32826
feat(speedtest): convert to pydantic 2024-05-25 17:55:05 -04:00
e88cba6cf4
fix(speedtest): fixed another keyerror 2024-05-25 17:40:46 -04:00
eb41c1a72b
fix(speedtest): fixed two keyerrors 2024-05-25 17:40:02 -04:00
c2333f40ef
fix(speedtest): fixed an incorrectly named kwarg 2024-05-25 17:38:52 -04:00
30003cd7c8
fix(speedtest) whoops! 2024-05-25 17:38:02 -04:00
b210fa8bb0
fix(speedtest): hopefully fixed an error 2024-05-25 17:37:35 -04:00
7806aa29c5
fix(speedtest): removed an await 2024-05-25 17:24:40 -04:00
8f55c6083a
fix(speedtest): improve an error message 2024-05-25 17:24:04 -04:00
4795df7dcc
fix(speedtest): added a error message for if speedtest is not installed 2024-05-25 17:21:33 -04:00
14dfafea2a
fix(speedtest): fixed an AttributeError 2024-05-25 17:20:33 -04:00
c3ab7593c7
fix(speedtest): bunch of fixes 2024-05-25 17:19:48 -04:00
ef591224cd
feat(speedtest): added the cog 2024-05-25 17:09:07 -04:00
4 changed files with 166 additions and 0 deletions

5
speedtest/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from .speedtest import Speedtest
async def setup(bot):
await bot.add_cog(Speedtest(bot))

14
speedtest/info.json Normal file
View file

@ -0,0 +1,14 @@
{
"author" : ["SeaswimmerTheFsh (seasw.)"],
"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, 10, 0],
"tags" : ["utility", "information"],
"requirements": ["pydantic"]
}

76
speedtest/models.py Normal file
View file

@ -0,0 +1,76 @@
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"
@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
macAddr: str
isVpn: bool
externalIp: str
class Ping(BaseModel):
jitter: float
latency: float
low: float
high: float
class Server(BaseModel):
id: int
name: str
location: str
country: str
host: str
port: int
ip: str
class Result(BaseModel):
id: str
url: str
persisted: bool
@property
def image(self) -> str:
return self.url + ".png"

71
speedtest/speedtest.py Normal file
View file

@ -0,0 +1,71 @@
# _____ _
# / ____| (_)
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
import asyncio
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
from .models import Speedtest as sp
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)
async def run_speedtest(self) -> str | sp:
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")
return sp.from_json(json.loads(stdout.decode("utf-8")))
@commands.command()
@commands.is_owner()
async def speedtest(self, ctx: commands.Context) -> None:
"""Run a speedtest."""
msg = await ctx.maybe_send_embed("Running speedtest...")
async with ctx.typing():
speedtest = await self.run_speedtest()
if await ctx.embed_requested():
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=speedtest.result.url, color=await ctx.embed_color())
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"**[Result]({speedtest.result.url})**")