Last active 1451356142

Get total size of files indicated by a list of URLs

Revision 6d0414f4addbcf7a1a5b37016e046bf15e303c1d

getsizefromurls.py Raw
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
21from urllib2 import urlopen
22import sys
23from os import sep
24
25filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1])
26
27
28def 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
36def 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
48 print "Total: " + sizeof_fmt(bytes)
49 return 0
50
51
52if __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