getsizefromurls.py
· 1.2 KiB · Python
Raw
from urllib2 import urlopen
import sys
from os import sep
filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1])
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']:
if num < 1024.0 and num > -1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'YB')
def main(inp='list.txt', out=filename+'output.txt'):
bytes = 0
with open(inp) as f:
urls = [url.strip() for url in f.readlines()]
with open(out,'w') as output:
for x in urls:
a = urlopen(x)
bytes = bytes + int(a.headers["Content-Length"])
name = x.split("/")[-1]
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))
output.write(out)
print out.strip()
print "Total: " + sizeof_fmt(bytes)
return 0
if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) >= 3:
sys.exit(main(inp=sys.argv[1], out=sys.argv[2]))
elif len(sys.argv) >= 2:
sys.exit(main(inp=sys.argv[1]))
else:
sys.exit(main())
| 1 | from urllib2 import urlopen |
| 2 | import sys |
| 3 | from os import sep |
| 4 | |
| 5 | filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1]) |
| 6 | |
| 7 | |
| 8 | def 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 | |
| 16 | def 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 | |
| 32 | if __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 |