from selenium import webdriver
import re
import requests
import os


creator_name = input("Enter the Snapchat creator's name: ").strip().replace(" ", "_")
snapchat_url = input("Enter the Snapchat profile URL: ").strip()

main_folder = "snapchat_photos"
if not os.path.exists(main_folder):
    os.makedirs(main_folder)

creator_folder = os.path.join(main_folder, creator_name)
if not os.path.exists(creator_folder):
    os.makedirs(creator_folder)

print(f"Opening Snapchat profile: {snapchat_url}")
driver = webdriver.Chrome()  
driver.get(snapchat_url)

import time
time.sleep(2)  

html_content = driver.page_source
driver.quit()

pattern = r'"mediaPreviewUrl":\{"value":"(https://.*?)"}'
matches = re.findall(pattern, html_content)

if matches:
    print(f"Extracted Embed Links and Saving Photos to '{creator_folder}':")
    for idx, link in enumerate(matches):
        try:
            
            response = requests.get(link)
            if response.status_code == 200:
                filename = os.path.join(creator_folder, f"photo{idx + 1}.jpg")
                with open(filename, 'wb') as f:
                    f.write(response.content)
                print(f"Saved: {filename}")
            else:
                print(f"Failed to download: {link}")
        except Exception as e:
            print(f"Error downloading {link}: {e}")
else:
    print("No embed links were found.")
