Last active 1451356142

Get total size of files indicated by a list of URLs

Revision d9ee2b3ba7481af68922ff0cc4ea97c1df07f148

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 output.write("File: %s | Size: %s | Subtotal: %s\n" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes)))
46 print "File: %s | Size: %s | Subtotal: %s" % (name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes))
47 print "Total: " + sizeof_fmt(bytes)
48 return 0
49
50
51if __name__ == "__main__":
52 if len(sys.argv) > 1:
53 if len(sys.argv) >= 3:
54 sys.exit(main(inp=sys.argv[1], out=sys.argv[2]))
55 elif len(sys.argv) >= 2:
56 sys.exit(main(inp=sys.argv[1]))
57 else:
58 sys.exit(main())