Created
May 5, 2023 16:19
-
-
Save htlin222/5e762e12144bfb97a6f18378b98c8625 to your computer and use it in GitHub Desktop.
Batch export simplenote note tagged "Anki" to Anki, and change the tag to "added_to_anki"
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
# title: simplenote_to_anki | |
# date: "2023-03-03" | |
# @raycast.title Simplenote to Anki | |
# @raycast.author HTLin the 🦎 | |
# @raycast.authorURL https://github.com/htlin222 | |
# @raycast.description Sync to Anki | |
# @raycast.icon 🔃 | |
# @raycast.mode fullOutput | |
# @raycast.packageName System | |
# @raycast.schemaVersion 1 | |
import simplenote | |
import requests | |
import json | |
import openai | |
# Define the Simplenote credentials and tag to filter for | |
SIMPLENOTE_EMAIL = 'YOUR_MAIL' | |
SIMPLENOTE_PASSWORD = 'YOUR_PASSWORD' | |
SIMPLENOTE_TAG = 'anki' | |
# Define the AnkiConnect API endpoint and deck to use | |
ANKICONNECT_ENDPOINT = 'http://localhost:8765' | |
ANKI_DECK_NAME = 'Default' | |
# Define the Anki note type and fields | |
ANKI_NOTE_TYPE = 'Basic' | |
ANKI_FRONT_FIELD = 'Front' | |
ANKI_BACK_FIELD = 'Back' | |
def get_sn(): | |
# Authenticate with Simplenote | |
sn = simplenote.Simplenote(SIMPLENOTE_EMAIL, SIMPLENOTE_PASSWORD) | |
# Get all notes with the specified tag | |
notes = sn.get_note_list(tags=[SIMPLENOTE_TAG]) | |
notes = notes[0] | |
for note in notes: | |
content = note['content'] + '\n\n🤗' | |
lines = content.split('\n') | |
title = lines[0] | |
back = '<br>'.join(lines[1:]) | |
print("--------") | |
# Define the AnkiConnect note payload | |
sent_to_anki(title, back) | |
# change tags | |
note['tags'] = ['added_to_anki' if x == 'anki' else x for x in note['tags']] | |
sn.update_note(note) | |
def sent_to_anki(title, back): | |
payload = { | |
'action': 'addNote', | |
'version': 6, | |
'params': { | |
'note': { | |
'deckName': ANKI_DECK_NAME, | |
'modelName': ANKI_NOTE_TYPE, | |
'fields': { | |
ANKI_FRONT_FIELD: title, | |
ANKI_BACK_FIELD: back, | |
}, | |
'options': { | |
'allowDuplicate': False, | |
}, | |
'tags': ['from_simplenote'], | |
}, | |
}, | |
} | |
# Send the AnkiConnect request | |
response = requests.post(ANKICONNECT_ENDPOINT, data=json.dumps(payload)) | |
print(f'{title} done') | |
# Print the response | |
print(response.content.decode()) | |
if __name__ == '__main__': | |
get_sn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment