Last active
August 29, 2015 13:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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