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.
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
![]()
2 months ago
|
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 detect_platform(link, social_media_data):
|
||
|
# Detect the platform from the link
|
||
|
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()
|
||
|
|
||
|
# 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 = link.split("//")[-1].split("/")[0] # Extract domain from 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 Linktree '{output_file}' has been generated successfully!")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
# 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)
|