Untitled

mail@pastecode.io avatar
unknown
python
a year ago
4.1 kB
2
Indexable
Never
#!/usr/bin/python3
import random
import string
import uuid
import json
import sys
import argparse
from datetime import timedelta, datetime

SECONDS_IN_A_DAY = 24 * 60 * 60

class Identity:

    def __init__(self, input_filepath, history_path,  seed):
        self.history    = history_path
        self.filepath   = input_filepath
        self.data       = self.read_data()
        self.seed       = self.generate_seed() \
        if seed == "" else seed
        self.random     = random.Random(self.seed)
        self.username   = self.generate_username()
        self.name       = self.generate_name()
        self.dob, \
        self.age        = self.generate_dob()
        self.email      = self.return_email()

    def generate_seed(self):
        letters = string.ascii_letters
        seed = ""
        for letter in range(1,32):
            seed+=(random.choice(letters))
        return seed

    def read_data(self):
        with open(self.filepath, 'r') as f:
            data = f.read()
        return json.loads(data)

    def generate_name(self):
        first_names         = self.data["fnames"]
        last_names          = self.data["lnames"]
        name = f"{self.random.choice(first_names)} {self.random.choice(last_names)}"
        return name

    def generate_username(self):
        username_list       = self.data["usernames"]
        generated_username  =   ""
        for _ in range(2):
            generated_username += self.random\
                    .choice(username_list)
        return generated_username

    def generate_dob(self, start=1950, end=2002):
        start_date = datetime(start, 1, 1)
        end_date = datetime(end, 12, 31)

        delta = end_date - start_date
        int_delta = delta.days * SECONDS_IN_A_DAY
        random_second = random.randint(0, int_delta)
        random_dob = start_date + timedelta(seconds=random_second)

        current_date = datetime.now()
        age = current_date.year - random_dob.year - \
        ((current_date.month, current_date.day) < (random_dob.month, random_dob.day))

        formatted_dob = random_dob.strftime("%d.%m.%Y")

        return formatted_dob, age

    def print_information(self):
        string_info = f"Name: {self.name}\nAge: {self.age}\nDate of birth: {self.dob}\nUsername: {self.username}\nEmail: {self.email}\nSeed: {self.seed}"
        print(string_info)

    def return_email(self):
        email_list = self.data["emails"]
        return self.random.choice(email_list)

    def save_result(self):
            # Read existing JSON content from "output.json" or initialize an empty dictionary if the file doesn't exist
            try:
                with open(self.history, "r") as f:
                    data = json.load(f)
            except FileNotFoundError:
                data = {'results': [], 'duplicates': []}

            current_time = datetime.now()
            formatted_time = current_time.strftime("%H.%M %d.%m.%Y")

            # Create a new result dictionary with the UID
            new_result = {
                "uid": str(uuid.uuid4()),
                "time": formatted_time,
                "seed": self.seed
            }

            # Check if the seed has been used before
            seed_used = any(result['seed'] == self.seed for result in data['results'])

            if seed_used:
                data['duplicates'].append(new_uid)
            data['results'].append(new_result)

            with open(self.history, "w") as f:
                json.dump(data, f, indent=4)

def main():

    parser = argparse.ArgumentParser(description="Generate a profile using input data")

    parser.add_argument("--input", \
    help="Path to the input JSON file", default="information.json")
    parser.add_argument("--history", \
    help="Path to the output JSON file", default=".identityhis.json")
    parser.add_argument("--seed", \
    help="Specify a seed for profile generation", type=str, default="")

    args = parser.parse_args()


    me = Identity(args.input, args.history, args.seed)
    me.print_information()
    me.save_result()

if __name__ == '__main__':
    main()