Skip to content

Instantly share code, notes, and snippets.

@kbagher
Created December 25, 2023 09:45
Show Gist options
  • Save kbagher/9ba5b9fae3e73fdaaf8c87cadbde279a to your computer and use it in GitHub Desktop.
Save kbagher/9ba5b9fae3e73fdaaf8c87cadbde279a to your computer and use it in GitHub Desktop.
Simple script to change the title field in safari export passwords for consistency
import pandas as pd
import tldextract
def extract_domain(url):
extracted = tldextract.extract(url)
return "{}.{}".format(extracted.domain, extracted.suffix)
def consolidate_credentials(csv_path):
# Read the CSV file
df = pd.read_csv(csv_path)
# Extract main domains
df['MainDomain'] = df['URL'].apply(extract_domain)
# Function to check if OTPAuth matches or is empty
def otpauth_match(group):
otpauths = group['OTPAuth'].dropna().unique()
return len(otpauths) <= 1
# Group by main domain, username, and password
grouped = df.groupby(['MainDomain', 'Username', 'Password'])
# Consolidate entries
consolidated = []
for name, group in grouped:
main_domain, username, password = name
if otpauth_match(group):
notes = group['Notes'].iloc[0] # Assuming first note is representative
otpauth = group['OTPAuth'].iloc[0] if pd.notna(group['OTPAuth']).any() else ''
# Create a separate entry for each URL
for url in group['URL']:
consolidated.append({
'Title': main_domain,
'URL': url,
'Username': username,
'Password': password,
'Notes': notes,
'OTPAuth': otpauth
})
# Create a new DataFrame
consolidated_df = pd.DataFrame(consolidated)
# Save to a new CSV file
consolidated_df.to_csv('consolidated_credentials.csv', index=False)
# Example usage
consolidate_credentials('Passwords.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment