Created
August 2, 2021 10:35
-
-
Save pauladams8/325b39daf4c96eff1df687d4e848c879 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
Author
pauladams8
commented
Aug 2, 2021
- Install the packages
- Run the script
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment