You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
import json
|
|
|
|
def load_social_media_data():
|
|
# Load social media data from JSON file
|
|
with open("./templates/sites.json", "r") as file:
|
|
return json.load(file)
|
|
|
|
def extract_domain(link):
|
|
# Extract the domain from a link (e.g., "https://mastodon.social" -> "mastodon.social")
|
|
return link.split("//")[-1].split("/")[0]
|
|
|
|
def detect_platform(link, social_media_data):
|
|
# Extract the domain from the link
|
|
domain = extract_domain(link)
|
|
|
|
# Check each platform's domains list
|
|
for platform, data in social_media_data.items():
|
|
if "domains" in data and domain in data["domains"]:
|
|
return platform
|
|
|
|
# Fallback: Check if the platform name is in the link (e.g., "twitter.com" -> "twitter")
|
|
for platform in social_media_data:
|
|
if platform in link:
|
|
return platform
|
|
|
|
return None
|
|
|
|
def generate_html(links, social_media_data):
|
|
# Read the template file
|
|
with open("./templates/template.html", "r") as file:
|
|
template = file.read()
|
|
|
|
# Replace the {name} placeholder with the user's name
|
|
template = template.replace("{name}", name)
|
|
|
|
# Generate button elements for each link
|
|
buttons = ""
|
|
for link in links:
|
|
platform = detect_platform(link, social_media_data)
|
|
if platform:
|
|
icon_url = social_media_data[platform]["icon"]
|
|
platform_name = social_media_data[platform]["name"]
|
|
buttons += f'''
|
|
<a href="{link}" class="button">
|
|
<img src="{icon_url}" alt="{platform} icon">
|
|
{platform_name}
|
|
</a>\n
|
|
'''
|
|
else:
|
|
# If the platform is not recognized, use the domain name as the button text
|
|
domain = extract_domain(link)
|
|
buttons += f'<a href="{link}" class="button">{domain}</a>\n'
|
|
|
|
# Replace the placeholder with the generated buttons
|
|
template = template.replace("{links}", buttons)
|
|
|
|
# Save the new HTML file
|
|
output_file = "./generated/visicard.html"
|
|
with open(output_file, "w") as file:
|
|
file.write(template)
|
|
|
|
print(f"Your Visicard '{output_file}' has been generated successfully!")
|
|
|
|
if __name__ == "__main__":
|
|
# Ask the user for their name
|
|
name = input("Please enter your name. This will be shown in the Header of the Page:")
|
|
|
|
# Load social media data
|
|
social_media_data = load_social_media_data()
|
|
|
|
# Ask the user how many links they want to add
|
|
try:
|
|
num_links = int(input("How many links do you want to add? "))
|
|
except ValueError:
|
|
print("Please enter a valid number.")
|
|
exit()
|
|
|
|
# Ask the user for each link
|
|
links = []
|
|
for i in range(1, num_links + 1):
|
|
link = input(f"Please enter Link {i}: ")
|
|
links.append(link)
|
|
|
|
# Generate the HTML file
|
|
generate_html(links, social_media_data) |