Skip to content

Instantly share code, notes, and snippets.

@pauladams8
Created August 2, 2021 10:35
Show Gist options
  • Save pauladams8/325b39daf4c96eff1df687d4e848c879 to your computer and use it in GitHub Desktop.
Save pauladams8/325b39daf4c96eff1df687d4e848c879 to your computer and use it in GitHub Desktop.
import json
import shlex
import argparse
import tldextract
import subprocess
def run(cmd):
try:
return subprocess.run(shlex.split(cmd), check=True, capture_output=True, text=True).stdout
except subprocess.CalledProcessError as e:
print(e.stderr.strip() or f'Error running command `{cmd}`')
exit()
def domain_parts(item):
return [tldextract.extract(url=node['u']) for node in item.get('overview', {}).get('URLs', [])]
def root_domains(item):
return list(set('.'.join(p for p in (d.domain, d.suffix) if p) for d in domain_parts(item)))
parser = argparse.ArgumentParser(description='Rename logins in your 1Password vault by domain')
parser.add_argument('--vault', metavar='[vault id]', help='Only rename items in the given vault')
parser.add_argument('--tag', metavar='[tag id]', action='append', help='Only rename items with the given tags')
args = parser.parse_args()
cmd = 'op list items --categories Login'
if args.vault:
cmd += f' --vault {args.vault}'
if args.tag:
cmd += f' --tags {",".join(args.tag)}'
items = json.loads(run(cmd))
logins = (item for item in items if int(item['templateUuid']) == 1)
for item in logins:
rd = root_domains(item)
if len(rd) == 1:
name = rd[0]
else:
continue
if item['overview']['title'] == name:
continue
print(f'Renaming item {item["overview"]["title"]} to {name}')
run(f'op edit item {item["uuid"]} title={name}')
@pauladams8
Copy link
Author

  • Install the packages
pip install tldextract
  • Run the script
python 1password_rename_by_domain.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment