-
-
Save lenamax2355/a5dc9496519b26a289f137f550bde683 to your computer and use it in GitHub Desktop.
Pythonista script to read from Safari and write to Supabase
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 appex | |
| import json | |
| import requests | |
| LANGUAGE = "en-gb" | |
| OXFORD_ID = "9acc1234" | |
| OXFORD_KEY = "6baccf388cd6456456326e85054f30aba" | |
| OXFORD_URL = "https://od-api.oxforddictionaries.com/api/v2" | |
| OXFORD_HEADERS = { | |
| "app_id": OXFORD_ID, | |
| "app_key": OXFORD_KEY | |
| } | |
| SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlhdCI6MTYxNzE4NjQ0" | |
| SUPABASE_URL = "https://zbowexzxwkfvehmevgvl.supabase.co/rest/v1" | |
| SUPABASE_HEADERS = { | |
| "apikey": SUPABASE_KEY, | |
| "Authorization": f"Bearer {SUPABASE_KEY}" | |
| } | |
| def insert_entry(word: str, definition: str): | |
| """ Insert entry into Supabase database """ | |
| if not isinstance(word, str): | |
| return | |
| post_headers = { | |
| "Content-Type": "application/json", | |
| "Prefer": "return=representation" | |
| } | |
| data = { | |
| "word": word, | |
| "definition": definition | |
| } | |
| return requests.post(f"{SUPABASE_URL}/Words", data=json.dumps(data), headers={**SUPABASE_HEADERS, **post_headers}) | |
| def get_words(): | |
| """ Retrieve the existing words from the words table """ | |
| list_resp = requests.get(f"{SUPABASE_URL}/Words?select=word", headers=SUPABASE_HEADERS) | |
| return list_resp.json() | |
| def get_definition(word: str): | |
| """ Get the definition from the Oxford dictionary """ | |
| url = f"{OXFORD_URL}/entries/{LANGUAGE}/{word.lower()}" | |
| params = { | |
| 'fields': 'definitions' | |
| } | |
| resp = requests.get(url, headers=OXFORD_HEADERS, params=params).json() | |
| return resp['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions'][0] | |
| def main(): | |
| """ Main function """ | |
| copied_text = appex.get_text() | |
| if not copied_text: | |
| print("No input found") | |
| return | |
| definition = get_definition(copied_text) | |
| insert_entry(word=copied_text, definition=definition) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment