Last active 1451356142

Get total size of files indicated by a list of URLs

Steven Smith revised this gist 1391824133. Go to revision

1 file changed, 20 deletions

getsizefromurls.py

@@ -1,23 +1,3 @@
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 1 from urllib2 import urlopen
22 2 import sys
23 3 from os import sep

Steven Smith revised this gist 1385526782. Go to revision

1 file changed, 1 insertion, 1 deletion

getsizefromurls.py

@@ -44,7 +44,7 @@ def main(inp='list.txt', out=filename+'output.txt'):
44 44 name = x.split("/")[-1]
45 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 46 output.write(out)
47 - print out
47 + print out.strip()
48 48 print "Total: " + sizeof_fmt(bytes)
49 49 return 0
50 50

Steven Smith revised this gist 1385525625. Go to revision

1 file changed, 4 insertions, 3 deletions

getsizefromurls.py

@@ -42,8 +42,9 @@ def main(inp='list.txt', out=filename+'output.txt'):
42 42 a = urlopen(x)
43 43 bytes = bytes + int(a.headers["Content-Length"])
44 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))
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
47 48 print "Total: " + sizeof_fmt(bytes)
48 49 return 0
49 50
@@ -55,4 +56,4 @@ if __name__ == "__main__":
55 56 elif len(sys.argv) >= 2:
56 57 sys.exit(main(inp=sys.argv[1]))
57 58 else:
58 - sys.exit(main())
59 + sys.exit(main())

Steven Smith revised this gist 1385524800. Go to revision

1 file changed, 9 insertions, 9 deletions

getsizefromurls.py

@@ -1,8 +1,8 @@
1 1 # getsizefromurls.py
2 2 # Gets total size of files indicated by a list of urls
3 3 # Usage:
4 - # getsizefromurls.py <-d>|<input filename> [output filename]
5 - # -d forces 'list.txt' as input and 'output.txt' as output
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 6 # Licensed under the MIT license because I couldn't find anything to do this using Google
7 7 # Outputs:
8 8
@@ -22,6 +22,9 @@ from urllib2 import urlopen
22 22 import sys
23 23 from os import sep
24 24
25 + filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1])
26 +
27 +
25 28 def sizeof_fmt(num):
26 29 for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']:
27 30 if num < 1024.0 and num > -1024.0:
@@ -29,7 +32,8 @@ def sizeof_fmt(num):
29 32 num /= 1024.0
30 33 return "%3.1f%s" % (num, 'YB')
31 34
32 - def main(inp='list.txt', out='output.txt'):
35 +
36 + def main(inp='list.txt', out=filename+'output.txt'):
33 37 bytes = 0
34 38 with open(inp) as f:
35 39 urls = [url.strip() for url in f.readlines()]
@@ -43,16 +47,12 @@ def main(inp='list.txt', out='output.txt'):
43 47 print "Total: " + sizeof_fmt(bytes)
44 48 return 0
45 49
50 +
46 51 if __name__ == "__main__":
47 52 if len(sys.argv) > 1:
48 - if sys.argv[1] == "-d":
49 - sys.exit(main())
50 53 if len(sys.argv) >= 3:
51 54 sys.exit(main(inp=sys.argv[1], out=sys.argv[2]))
52 55 elif len(sys.argv) >= 2:
53 56 sys.exit(main(inp=sys.argv[1]))
54 57 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)
58 + sys.exit(main())

Steven Smith revised this gist 1385524086. Go to revision

1 file changed, 58 insertions

getsizefromurls.py(file created)

@@ -0,0 +1,58 @@
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)
Newer Older