💾 !dirsize

This commit is contained in:
NikolajDanger
2021-02-01 21:57:36 +01:00
parent 519885a435
commit 25366f016d
2 changed files with 55 additions and 1 deletions

View File

@ -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)