Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Created September 23, 2020 13:42
Show Gist options
  • Save haridutt12/6169bb8da356776614f4c4733eb0fc52 to your computer and use it in GitHub Desktop.
Save haridutt12/6169bb8da356776614f4c4733eb0fc52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 23 16:41:22 2020
@author: Hari Dutt Parashar
"""
import json
from datetime import datetime
import re
from dateutil.parser import parse
import pytz
import argparse
import argparse
import os
import sys
def parse_args():
# Create the parser
my_parser = argparse.ArgumentParser(
description='List the content of a folder')
# Add the arguments
my_parser.add_argument('-j',
'--json-filepath',
type=str,
help='the path to json',
required=True)
my_parser.add_argument('-c',
'--created-before',
metavar='created_before',
type=int,
help='Days',
required=True)
my_parser.add_argument('-p',
'--prefix',
metavar='prefix',
type=str,
help='prefix',
required=True)
return my_parser.parse_args()
def run(filepath, created_before, prefix):
if not os.path.exists(filepath):
print('The path specified does not exist')
sys.exit()
with open(filepath) as f:
records = json.load(f)
now = pytz.UTC.normalize(datetime.now(pytz.timezone('Asia/Kolkata')))
total_count, prefix_count = 0, 0
for record in records:
staleness = (now - pytz.UTC.normalize(
parse(record['creationTimestamp']))).days
if staleness > created_before:
total_count += 1
if record["name"].startswith(prefix):
prefix_count += 1
print(f'Total number of images older than {created_before} days: {total_count}')
print(f'Total number of images with prefix {prefix} older than {created_before} days : {prefix_count}')
if __name__ == "__main__":
args = parse_args()
run(args.json_filepath, args.created_before, args.prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment