torrentdirsum.py
· 1.1 KiB · Python
Raw
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))
| 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 | cache[a] = tot |
| 31 | else: |
| 32 | tot = cache[a] |
| 33 | total.append(tot) |
| 34 | print str(cache[a]) + " %s/%s" % (x, len(dirlist)) |
| 35 | x += 1 |
| 36 | finally: |
| 37 | with open('cache.json', 'w') as f: |
| 38 | f.write(json.dumps(cache)) |
| 39 | return total |
| 40 | |
| 41 | if __name__ == "__main__": |
| 42 | dirlist = listdir('.') |
| 43 | total = main(dirlist) |
| 44 | print sizeof_fmt(sum(total)) |