diff options
Diffstat (limited to 'gen_names.py')
| -rw-r--r-- | gen_names.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/gen_names.py b/gen_names.py new file mode 100644 index 00000000..bad1987a --- /dev/null +++ b/gen_names.py @@ -0,0 +1,83 @@ +import json +import datetime + +# Load JSON data from a file +def load_json(file_path): + with open(file_path, 'r', encoding='utf-8') as json_file: + return json.load(json_file) + + +def get_list_of_last_names(data): + processed_data = [] + for item in data: + name_split = item.get('Name').split(" ") + name_split_len = len(name_split) + name = name_split[-1] + if name not in processed_data and name_split_len > 1: + processed_data.append(name) + + processed_data.sort() + return processed_data + +def get_people_by_lastname(data, lastname): + processed_data = [] + for item in data: + name_split = item.get('Name').split(" ") + last_name = name_split[-1] + name_split_len = len(name_split) + + if name_split_len == 1 and lastname == '[empty]': + processed_data.append(item) + + if last_name == lastname: + processed_data.append(item) + + return processed_data + +def generate_html(data, output_file): + last_names = get_list_of_last_names(data) + + # last_names.insert(0, '[empty]') + + html_content = '' + for lastname in last_names: + html_content += '<h2>'+ lastname +'</h2>' + people = get_people_by_lastname(data, lastname) + for person in people: + name_split = person.get('Name').split(" ") + first_and_middle_names = " ".join(name_split[:-1]) + last_name = name_split[-1] + + birthdate = datetime.datetime.strptime(person.get('Birthday'), "%Y-%m-%dT%H:%M:%S") + dieddate = datetime.datetime.strptime(person.get('DiedDay'), "%Y-%m-%dT%H:%M:%S") + + if birthdate.year == 1 and dieddate.year == 1: + html_content += '<p><a href="people/'+str(person.get('Id'))+'.html">' + last_name + ', ' + first_and_middle_names + '</a></p>' + else: + html_content += '<p><a href="people/'+str(person.get('Id'))+'.html">' + last_name + ', ' + first_and_middle_names + '</a> (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')</p>' + + html_content += '<br><p>This list contains ' + str(len(data)) + ' people</p>' + html_content += '<p>Last updated ' + datetime.datetime.now().strftime('%d-%m-%Y') + '</p>' + + html_template = '' + with open('templates/names_template.html', 'r') as file: + html_template = file.read() + + html_template = html_template.replace('{{NAME_LIST}}', html_content) + + # Write the HTML content to a file + with open(output_file, 'w', encoding='utf-8') as f: + f.write(html_template) + +def main(): + json_file_path = 'list.json' + output_html_file = 'names.html' + + # Load JSON data directly into an array + json_data = load_json(json_file_path) + + # Generate HTML from the loaded data + generate_html(json_data, output_html_file) + +if __name__ == '__main__': + main() |
