Skip to content

Instantly share code, notes, and snippets.

@banan314
Last active March 10, 2024 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

banan314 commented Jul 8, 2023

total of removed tags: 471

--- 528.98 seconds ---

@banan314
Copy link
Author

banan314 commented Jul 8, 2023

from joppy.api import Api

API_TOKEN = "token"


def get_api():
    # Create a new Api instance.
    api = Api(token=API_TOKEN)

    return api

@marph91
Copy link

marph91 commented Mar 4, 2024

Nice script. Please note, if you use a newer version of joppy (from 0.1.0 on), the API responses are objects and not dicts anymore. You can use for example tag.title instead of tag['title'].

@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