Last active 1440820549

Sum up the total size of the contents of a folder of torrents

Steven Smith revised this gist 1391089192. Go to revision

1 file changed, 3 insertions, 1 deletion

torrentdirsum.py

@@ -27,8 +27,10 @@ def main(dirlist):
27 27 if not a in cache:
28 28 info = lt.torrent_info(lt.bdecode(open(a, 'rb').read()))
29 29 tot = info.total_size()
30 - total.append(tot)
31 30 cache[a] = tot
31 + else:
32 + tot = cache[a]
33 + total.append(tot)
32 34 print str(cache[a]) + " %s/%s" % (x, len(dirlist))
33 35 x += 1
34 36 finally:

Steven Smith revised this gist 1391087541. Go to revision

1 file changed, 42 insertions

torrentdirsum.py(file created)

@@ -0,0 +1,42 @@
1 + import libtorrent as lt
2 + from os import listdir
3 + import json
4 +
5 + def sizeof_fmt(num):
6 + for x in ['bytes','KB','MB','GB']:
7 + if num < 1024.0 and num > -1024.0:
8 + return "%3.1f%s" % (num, x)
9 + num /= 1024.0
10 + return "%3.1f%s" % (num, 'TB')
11 +
12 + x = 1
13 + dirlist = listdir('.')
14 +
15 + def main(dirlist):
16 + total = []
17 + x = 1
18 + try:
19 + with open('cache.json') as f:
20 + cache = json.loads(f.read())
21 + except:
22 + cache = {}
23 + try:
24 + for a in dirlist:
25 + if not a.split(".")[-1] == "torrent":
26 + continue
27 + if not a in cache:
28 + info = lt.torrent_info(lt.bdecode(open(a, 'rb').read()))
29 + tot = info.total_size()
30 + total.append(tot)
31 + cache[a] = tot
32 + print str(cache[a]) + " %s/%s" % (x, len(dirlist))
33 + x += 1
34 + finally:
35 + with open('cache.json', 'w') as f:
36 + f.write(json.dumps(cache))
37 + return total
38 +
39 + if __name__ == "__main__":
40 + dirlist = listdir('.')
41 + total = main(dirlist)
42 + print sizeof_fmt(sum(total))
Newer Older