Skip to content

Instantly share code, notes, and snippets.

@banan314
Last active March 10, 2024 17:47
Show Gist options
  • Save banan314/63a22ade45e58e726efc56bb3a3e6e1d to your computer and use it in GitHub Desktop.
Save banan314/63a22ade45e58e726efc56bb3a3e6e1d to your computer and use it in GitHub Desktop.
remove duplicated tags from Joplin
from api import get_api
import time
start_time = time.time()
api = get_api()
f = open('duplicates-logs.txt', 'w')
all_tags = api.get_all_tags()
duplicated_tags = {}
# extract tags that are duplicated
unique_titles = {}
for tag in all_tags:
if tag.title in unique_titles:
if tag.title in duplicated_tags:
duplicated_tags[tag.title] += [tag]
else:
duplicated_tags[tag.title] = unique_titles[tag.title]
duplicated_tags[tag.title] += [tag]
else:
unique_titles[tag.title] = [tag]
for title, tags in duplicated_tags.items():
main_tag = tags[0]
tags_to_remove = tags[1:]
tagged_notes = []
for tag in tags:
tag_id = tag.id
tagged_notes += api.get_all_notes(tag_id=tag_id)
for note in tagged_notes:
note_tags = api.get_all_tags(note_id=note.id)
if not main_tag in note_tags:
print(f"adding tag {main_tag.title}, id {main_tag.id} to note {note.title}, id {note.id}", file=f)
api.add_tag_to_note(main_tag.id, note.id)
for tag in tags_to_remove:
print(f"deleting tag {tag.title}, id {tag.id}", file=f)
api.delete_tag(tag.id)
print(file=f)
print(f"duplicated tags: {duplicated_tags}", file=f)
print(f"total of titles: {len(duplicated_tags)}", file=f)
print(f"total of removed tags: {sum([len(tags) for tags in duplicated_tags.values()]) - len(duplicated_tags)}", file=f)
print(file=f)
print("--- %.2f seconds ---" % (time.time() - start_time), file=f)
f.close()
@banan314
Copy link
Author

@marph91 thanks, I updated the script

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