getsizefromurls.py
· 2.0 KiB · Python
Raw
# getsizefromurls.py
# Gets total size of files indicated by a list of urls
# Usage:
# getsizefromurls.py [input filename] [output filename]
# If parameters aren't provided, defaults to list.txt for input and <scriptfilename>output.txt for output
# Licensed under the MIT license because I couldn't find anything to do this using Google
# Outputs:
# File: live_user_moltov_1385511049.flv | Size: 167.6MB | Subtotal: 167.6MB
# File: live_user_moltov_1385512849.flv | Size: 172.6MB | Subtotal: 340.2MB
# File: live_user_moltov_1385514650.flv | Size: 182.1MB | Subtotal: 522.3MB
# File: live_user_moltov_1385516452.flv | Size: 181.8MB | Subtotal: 704.1MB
# File: live_user_moltov_1385518253.flv | Size: 8.6MB | Subtotal: 712.8MB
# to file, and prints:
# Total: 712.8MB
# in the terminal after those lines.
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 | # getsizefromurls.py |
| 2 | # Gets total size of files indicated by a list of urls |
| 3 | # Usage: |
| 4 | # getsizefromurls.py [input filename] [output filename] |
| 5 | # If parameters aren't provided, defaults to list.txt for input and <scriptfilename>output.txt for output |
| 6 | # Licensed under the MIT license because I couldn't find anything to do this using Google |
| 7 | # Outputs: |
| 8 | |
| 9 | # File: live_user_moltov_1385511049.flv | Size: 167.6MB | Subtotal: 167.6MB |
| 10 | # File: live_user_moltov_1385512849.flv | Size: 172.6MB | Subtotal: 340.2MB |
| 11 | # File: live_user_moltov_1385514650.flv | Size: 182.1MB | Subtotal: 522.3MB |
| 12 | # File: live_user_moltov_1385516452.flv | Size: 181.8MB | Subtotal: 704.1MB |
| 13 | # File: live_user_moltov_1385518253.flv | Size: 8.6MB | Subtotal: 712.8MB |
| 14 | |
| 15 | # to file, and prints: |
| 16 | |
| 17 | # Total: 712.8MB |
| 18 | |
| 19 | # in the terminal after those lines. |
| 20 | |
| 21 | from urllib2 import urlopen |
| 22 | import sys |
| 23 | from os import sep |
| 24 | |
| 25 | filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1]) |
| 26 | |
| 27 | |
| 28 | def sizeof_fmt(num): |
| 29 | for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']: |
| 30 | if num < 1024.0 and num > -1024.0: |
| 31 | return "%3.1f%s" % (num, x) |
| 32 | num /= 1024.0 |
| 33 | return "%3.1f%s" % (num, 'YB') |
| 34 | |
| 35 | |
| 36 | def main(inp='list.txt', out=filename+'output.txt'): |
| 37 | bytes = 0 |
| 38 | with open(inp) as f: |
| 39 | urls = [url.strip() for url in f.readlines()] |
| 40 | with open(out,'w') as output: |
| 41 | for x in urls: |
| 42 | a = urlopen(x) |
| 43 | bytes = bytes + int(a.headers["Content-Length"]) |
| 44 | name = x.split("/")[-1] |
| 45 | 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)) |
| 46 | output.write(out) |
| 47 | print out.strip() |
| 48 | print "Total: " + sizeof_fmt(bytes) |
| 49 | return 0 |
| 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | if len(sys.argv) > 1: |
| 54 | if len(sys.argv) >= 3: |
| 55 | sys.exit(main(inp=sys.argv[1], out=sys.argv[2])) |
| 56 | elif len(sys.argv) >= 2: |
| 57 | sys.exit(main(inp=sys.argv[1])) |
| 58 | else: |
| 59 | sys.exit(main()) |
| 60 |