Skip to content

Commit

Permalink
Add sandbox support
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTetrahedron committed Dec 30, 2024
1 parent dd9259f commit 1c00e9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions jasima.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
API_URL = "https://api.linku.la/v1/"
PATH_LANGUAGES = "languages"
PATH_WORDS = "words"
PATH_SANDBOX = "sandbox"
QUERY_LANGUAGE = "lang"

class JasimaCache:
Expand All @@ -17,32 +18,52 @@ def __init__(self):
self._bundle = {}
self._last_refresh = {}
self._refresh(self._language_key(DEFAULT_LANGUAGE))
self._refresh(self._language_key_sandbox(DEFAULT_LANGUAGE))

def definitions(self, language=DEFAULT_LANGUAGE):
self._refresh_if_needed(self._language_key(language))
return self._bundle[self._language_key(language)]

def sandbox_definitions(self, language=DEFAULT_LANGUAGE):
self._refresh_if_needed(self._language_key_sandbox(language))
return self._bundle[self._language_key_sandbox(language)]

@property
def languages(self):
self._refresh_if_needed(PATH_LANGUAGES)
return self._bundle[PATH_LANGUAGES]

def get_word_entry(self, word, language=DEFAULT_LANGUAGE):
entry = self.definitions(language).get(word)
entry = self._find_word_entry(word, self.definitions(language))
if entry:
return entry
f = [v for (k, v) in self.definitions(language).items() if k.lower() == word]
entry = self._find_word_entry(word, self.sandbox_definitions(language))
return entry

@staticmethod
def _find_word_entry(word, dictionary):
entry = dictionary.get(word)
if entry:
return entry
f = [v for (k, v) in dictionary.items() if k.lower() == word]
if len(f) == 1:
return f[0]
return None

def get_by_prefix(self, prefix, language=DEFAULT_LANGUAGE):
return {k: v for (k, v) in self.definitions(language).items() if k.lower().startswith(prefix)}
entries = {k: v for (k, v) in self.definitions(language).items() if k.lower().startswith(prefix)}
if entries:
return entries
entries = {k: v for (k, v) in self.sandbox_definitions(language).items() if k.lower().startswith(prefix)}
return entries

@staticmethod
def _language_key(lang):
return "{}?{}={}".format(PATH_WORDS, QUERY_LANGUAGE, lang)

@staticmethod
def _language_key_sandbox(lang):
return "{}?{}={}".format(PATH_SANDBOX, QUERY_LANGUAGE, lang)

def _refresh_if_needed(self, path):
if (datetime.datetime.now() - self._last_refresh.get(path, datetime.datetime.min)) > datetime.timedelta(hours=MAX_AGE_HOURS):
self._refresh(path)
Expand Down
2 changes: 1 addition & 1 deletion linkubot.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def handle_inline_query(self, update, context):
inline_results.append(
InlineQueryResultArticle(
id=word,
title=word,
title=word + (" (very obscure)" if results[word]["usage_category"] == "sandbox" else ""),
input_message_content=InputTextMessageContent(
parse_mode='Markdown',
message_text=self._get_definition_for_user(
Expand Down

0 comments on commit 1c00e9b

Please sign in to comment.