getsizefromurls.py
· 2.3 KiB · Python
Raw
# getsizefromurls.py
# Gets total size of files indicated by a list of urls
# Usage:
# getsizefromurls.py <-d>|<input filename> [output filename]
# -d forces 'list.txt' as input and 'output.txt' as 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
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='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]
output.write("File: %s | Size: %s | Subtotal: %s\n" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes)))
print "File: %s | Size: %s | Subtotal: %s" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes))
print "Total: " + sizeof_fmt(bytes)
return 0
if __name__ == "__main__":
if len(sys.argv) > 1:
if sys.argv[1] == "-d":
sys.exit(main())
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:
filename = sys.argv[0].split(sep)[-1]
print "%s: No parameters supplied." % filename
print "Default parameters are to use list.txt as input, and output.txt as output."
print "If you'd like to use these, please use '{filename} -d'. Otherwise, '{filename} <inputfile> [outputfile]'".format(filename=filename)
| 1 | # getsizefromurls.py |
| 2 | # Gets total size of files indicated by a list of urls |
| 3 | # Usage: |
| 4 | # getsizefromurls.py <-d>|<input filename> [output filename] |
| 5 | # -d forces 'list.txt' as input and 'output.txt' as 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 | def sizeof_fmt(num): |
| 26 | for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']: |
| 27 | if num < 1024.0 and num > -1024.0: |
| 28 | return "%3.1f%s" % (num, x) |
| 29 | num /= 1024.0 |
| 30 | return "%3.1f%s" % (num, 'YB') |
| 31 | |
| 32 | def main(inp='list.txt', out='output.txt'): |
| 33 | bytes = 0 |
| 34 | with open(inp) as f: |
| 35 | urls = [url.strip() for url in f.readlines()] |
| 36 | with open(out,'w') as output: |
| 37 | for x in urls: |
| 38 | a = urlopen(x) |
| 39 | bytes = bytes + int(a.headers["Content-Length"]) |
| 40 | name = x.split("/")[-1] |
| 41 | output.write("File: %s | Size: %s | Subtotal: %s\n" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes))) |
| 42 | print "File: %s | Size: %s | Subtotal: %s" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes)) |
| 43 | print "Total: " + sizeof_fmt(bytes) |
| 44 | return 0 |
| 45 | |
| 46 | if __name__ == "__main__": |
| 47 | if len(sys.argv) > 1: |
| 48 | if sys.argv[1] == "-d": |
| 49 | sys.exit(main()) |
| 50 | if len(sys.argv) >= 3: |
| 51 | sys.exit(main(inp=sys.argv[1], out=sys.argv[2])) |
| 52 | elif len(sys.argv) >= 2: |
| 53 | sys.exit(main(inp=sys.argv[1])) |
| 54 | else: |
| 55 | filename = sys.argv[0].split(sep)[-1] |
| 56 | print "%s: No parameters supplied." % filename |
| 57 | print "Default parameters are to use list.txt as input, and output.txt as output." |
| 58 | print "If you'd like to use these, please use '{filename} -d'. Otherwise, '{filename} <inputfile> [outputfile]'".format(filename=filename) |