Last active 1451356142

Get total size of files indicated by a list of URLs

getsizefromurls.py Raw
1from urllib2 import urlopen
2import sys
3from os import sep
4
5filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1])
6
7
8def sizeof_fmt(num):
9 for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']:
10 if num < 1024.0 and num > -1024.0:
11 return "%3.1f%s" % (num, x)
12 num /= 1024.0
13 return "%3.1f%s" % (num, 'YB')
14
15
16def main(inp='list.txt', out=filename+'output.txt'):
17 bytes = 0
18 with open(inp) as f:
19 urls = [url.strip() for url in f.readlines()]
20 with open(out,'w') as output:
21 for x in urls:
22 a = urlopen(x)
23 bytes = bytes + int(a.headers["Content-Length"])
24 name = x.split("/")[-1]
25 out = "File: {0:35} | Size: {1:7} | Subtotal: {2}\n".format(name[:32]+"..." if len(name)>35 else name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes))
26 output.write(out)
27 print out.strip()
28 print "Total: " + sizeof_fmt(bytes)
29 return 0
30
31
32if __name__ == "__main__":
33 if len(sys.argv) > 1:
34 if len(sys.argv) >= 3:
35 sys.exit(main(inp=sys.argv[1], out=sys.argv[2]))
36 elif len(sys.argv) >= 2:
37 sys.exit(main(inp=sys.argv[1]))
38 else:
39 sys.exit(main())
40