diff --git a/src/currency.ini b/src/currency.ini index af775fe..1040de6 100644 --- a/src/currency.ini +++ b/src/currency.ini @@ -6,7 +6,7 @@ [defaults] # Should the plugin try to convert currencies directly in search? -# * If no, you will have to select the catalog item before entering the +# * If false, you will have to select the catalog item before entering the # currencies to convert # * Default: true #always_evaluate = true diff --git a/src/currency.py b/src/currency.py index 742e547..2556246 100644 --- a/src/currency.py +++ b/src/currency.py @@ -89,19 +89,12 @@ def on_start(self): def on_catalog(self): catalog = [] - catalog.append(self.create_item( - category=self.ITEMCAT_UPDATE, - label='Update Currency', - short_desc='Last updated at ' + self.broker.last_update.isoformat(), - target="updatecurrency", - args_hint=kp.ItemArgsHint.FORBIDDEN, - hit_hint=kp.ItemHitHint.IGNORE)) - if self.default_item_enabled: catalog.append(self._create_translate_item( label=self.default_item_label)) self.set_catalog(catalog) + self._update_update_item() def on_suggest(self, user_input, items_chain): suggestions = [] @@ -120,6 +113,8 @@ def on_suggest(self, user_input, items_chain): if not query['from_cur'] or not query['to_cur'] or not user_input: return + if self.broker.tryUpdate(): + self._update_update_item() results = self.broker.convert(query['amount'], query['from_cur'], query['to_cur']) for result in results: @@ -143,13 +138,7 @@ def on_suggest(self, user_input, items_chain): def on_execute(self, item, action): if item.category() == self.ITEMCAT_UPDATE: self.broker.update() - self.merge_catalog([self.create_item( - category=self.ITEMCAT_UPDATE, - label='Update Currency', - short_desc='Last updated at ' + self.broker.last_update.isoformat(), - target="updatecurrency", - args_hint=kp.ItemArgsHint.FORBIDDEN, - hit_hint=kp.ItemHitHint.IGNORE)]) + self._update_update_item() return if item.category() != self.ITEMCAT_RESULT: return @@ -213,6 +202,15 @@ def _parse_and_merge_input(self, user_input=None, empty=False): query['amount'] = float(m.group('amount').rstrip().replace(',', '.')) return query + def _update_update_item(self): + self.merge_catalog([self.create_item( + category=self.ITEMCAT_UPDATE, + label='Update Currency', + short_desc='Last updated at ' + self.broker.last_update.isoformat(), + target="updatecurrency", + args_hint=kp.ItemArgsHint.FORBIDDEN, + hit_hint=kp.ItemHitHint.IGNORE)]) + def _create_translate_item(self, label): def joinCur(lst): diff --git a/src/exchange.py b/src/exchange.py index 0582480..05e39ed 100644 --- a/src/exchange.py +++ b/src/exchange.py @@ -16,19 +16,28 @@ class ExchangeRates(): _file_path = None last_update = None + update_freq = None _currencies = {} def __init__(self, path, update_freq): + self.update_freq = update_freq self._file_path = os.path.join(path, 'rates.json') if os.path.exists(self._file_path): - self.load_from_file() - else: - self.update() + try: + self.load_from_file() + except Exception as e: + self.update() + self.tryUpdate() + + def tryUpdate(self): time_diff = datetime.now() - self.last_update - if (update_freq.value == UpdateFreq.HOURLY.value and time_diff.total_seconds() >= 3600) or (update_freq.value == UpdateFreq.DAILY.value and time_diff.days >= 1): + if (self.update_freq.value == UpdateFreq.HOURLY.value and time_diff.total_seconds() >= 3600) or (self.update_freq.value == UpdateFreq.DAILY.value and time_diff.days >= 1): self.update() + return True + else: + return False def update(self): self.load_from_url()