Skip to content

Instantly share code, notes, and snippets.

@frederik-elwert
Created March 27, 2020 14:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frederik-elwert/5930d3c886080e245b2f7d1532c84478 to your computer and use it in GitHub Desktop.
Save frederik-elwert/5930d3c886080e245b2f7d1532c84478 to your computer and use it in GitHub Desktop.
from pathlib import Path
def sizeof_fmt(num, suffix='B'):
"""Readable file size"""
for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def calculate_size(data):
attachments = []
for item in data['items']:
for attachment in item['attachments']:
if 'path' in attachment:
attachment['size'] = Path(attachment['path']
).stat().st_size
attachments.append({
'path': attachment['path'],
'size': attachment['size'],
'uri': attachment['uri'],
'item': {
'title': item['title'],
'creators': item['creators'],
'date': item['date'],
}
})
return attachments
def main():
import argparse
import json
from operator import itemgetter
argparser = argparse.ArgumentParser()
argparser.add_argument('-o', '--outfile',
type=argparse.FileType('w'))
argparser.add_argument('-p', '--print-big',
action='store_true')
argparser.add_argument('-m', '--min-size',
type=int, default=1024 * 1024)
argparser.add_argument('-s', '--print-sum',
action='store_true')
argparser.add_argument('infile', type=argparse.FileType())
args = argparser.parse_args()
data = json.load(args.infile)
attachments = calculate_size(data)
if args.outfile:
json.dump(data, args.outfile, indent=2)
if args.print_sum:
print(sizeof_fmt(sum(
[att['size'] for att in attachments])))
if args.print_big:
attachments.sort(key=itemgetter('size'), reverse=True)
for attachment in attachments:
if attachment['size'] < args.min_size:
break
print(sizeof_fmt(attachment['size']),
attachment['uri'].split('/')[-1])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment