From 25366f016ddac118979cc66a61bec0aa51f7a60e Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Mon, 1 Feb 2021 21:57:36 +0100 Subject: [PATCH] :floppy_disk: !dirsize --- cogs/MiscCog.py | 5 ++++ funcs/other/bedreNetflix.py | 51 ++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/cogs/MiscCog.py b/cogs/MiscCog.py index 5fb7265..4fe8746 100644 --- a/cogs/MiscCog.py +++ b/cogs/MiscCog.py @@ -136,6 +136,11 @@ class MiscCog(commands.Cog): async def downloading(self,ctx): await self.bedreNetflix.downloading(ctx) + #Calculates how much space is used on the server + @commands.command(aliases = ["dirsize","storage"]) + async def directorySizes(self,ctx): + await self.bedreNetflix.directorySizes(ctx) + #Looks up on Wolfram Alpha @commands.command() async def wolf(self, ctx, *, content): diff --git a/funcs/other/bedreNetflix.py b/funcs/other/bedreNetflix.py index d4c882a..cd50bbf 100644 --- a/funcs/other/bedreNetflix.py +++ b/funcs/other/bedreNetflix.py @@ -1,4 +1,4 @@ -import requests, imdb, discord, json, math, time, asyncio +import requests, imdb, discord, json, math, time, asyncio, os from funcs import logThis radarrURL = "http://localhost:7878/api/v3/" @@ -7,6 +7,30 @@ qbittorrentURL = "http://localhost:1340/api/v2/" moviePath = "/media/plex/Server/movies/" showPath = "/media/plex/Server/Shows/" +async def getSize(startPath,pathLength): + logThis("Getting the size of "+startPath) + totalSize = 0 + for dirpath, dirnames, filenames in os.walk(startPath): + for f in filenames: + fp = os.path.join(dirpath, f) + # skip if it is symbolic link + if not os.path.islink(fp): + totalSize += os.path.getsize(fp) + + return totalSize + +async def formatSize(size): + if size >= 1024**4: + return ("%.1f" % (size/(1024**4))) + "TB" + elif size >= 1024**3: + return ("%.1f" % (size/(1024**3))) + "GB" + elif size >= 1024**2: + return ("%.1f" % (size/(1024**2))) + "MB" + elif size >= 1024: + return ("%.1f" % (size/(1024))) + "KB" + else: + return str(size)+"b" + class BedreNetflix(): def __init__(self,bot): self.bot = bot @@ -249,3 +273,28 @@ class BedreNetflix(): await oldMessage.edit(content = messageText) else: await ctx.send(messageText) + + async def directorySizes(self,ctx): + logThis("Calculating directory size") + message = await ctx.send("Calculating directory size. This might take a while.") + startPath = ".." + pathLength = len(startPath) + directories = [] + for f in os.listdir(startPath): + fp = os.path.join(startPath, f) + if os.path.isfile(fp): + size = os.path.getsize(fp) + else: + size = await getSize(fp,pathLength) + directories.append((str(f),size)) + directories.sort(key=lambda tup: tup[1]) + directories.reverse() + returnText = "```" + total = 0 + for directory, size in directories: + total += size + sizeText = await formatSize(size) + returnText += "\n"+directory+": "+sizeText + returnText += "\nTotal: "+await formatSize(total)+"```" + + await message.edit(content=returnText)