Last active 1451356142

Get total size of files indicated by a list of URLs

Revision a7cde82da156dd808ef2bd37c9425ba35ebbe5d5

getsizefromurls.py Raw
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
21from urllib2 import urlopen
22import sys
23from os import sep
24
25def 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
32def 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
46if __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)