Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active August 29, 2015 13:55
Show Gist options
  • Save blha303/8708035 to your computer and use it in GitHub Desktop.
Save blha303/8708035 to your computer and use it in GitHub Desktop.
Sum up the total size of the contents of a folder of torrents
import libtorrent as lt
from os import listdir
import json
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB']:
if num < 1024.0 and num > -1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'TB')
x = 1
dirlist = listdir('.')
def main(dirlist):
total = []
x = 1
try:
with open('cache.json') as f:
cache = json.loads(f.read())
except:
cache = {}
try:
for a in dirlist:
if not a.split(".")[-1] == "torrent":
continue
if not a in cache:
info = lt.torrent_info(lt.bdecode(open(a, 'rb').read()))
tot = info.total_size()
cache[a] = tot
else:
tot = cache[a]
total.append(tot)
print str(cache[a]) + " %s/%s" % (x, len(dirlist))
x += 1
finally:
with open('cache.json', 'w') as f:
f.write(json.dumps(cache))
return total
if __name__ == "__main__":
dirlist = listdir('.')
total = main(dirlist)
print sizeof_fmt(sum(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment