1
0
Fork 0
mirror of https://github.com/Findus23/acronomy-krunner.git synced 2024-09-19 12:43:44 +02:00

search locally

This commit is contained in:
Lukas Winkler 2020-09-22 20:37:59 +02:00
parent 4b777a8935
commit 761298717c
Signed by: lukas
GPG key ID: 54DE4D798D244853
3 changed files with 33 additions and 19 deletions

View file

@ -13,10 +13,7 @@ The `acronomy.py` script needs to run permanently in the background to fetch the
One easy way to accomplish this is to copy the `acronomy.desktop` to `~/.config/autostart/`.
You can replace the search keyword (default `acr`) with the `--keyword` parameter.
If you enable the `--less-privacy` mode, you don't need to specify any search keyword, but as a result all krunner searches are sent to the Astroacro API. (instead of only the ones starting with the search keyword)
No search queries are sent to the Acronomy server. Instead a list of all acronyms is fetched and searched locally.
This plugin uses the [Astroacro API](https://acronomy.lw1.at/api/). ([Privacy Policy](http://lw1.at/i))

View file

@ -1,6 +1,7 @@
#!/bin/env python3
import argparse
import webbrowser
from datetime import datetime, timedelta
import dbus.service
import requests
@ -12,33 +13,51 @@ DBusGMainLoop(set_as_default=True)
objpath = "/acronomy"
iface = "org.kde.krunner1"
s = requests.Session()
s.headers.update({'User-Agent': 'Acronomy Krunner'})
class LocalData:
acronyms = {}
last_updated = datetime.now()
def __init__(self):
self.fetch_data()
def fetch_data(self):
print("fetching data")
r = s.get("https://acronomy.lw1.at/api/acronym/")
self.acronyms = {}
self.last_updated = datetime.now()
for acro in r.json():
self.acronyms[acro["name"].lower()] = acro
def search(self, query: str):
age = datetime.now() - self.last_updated
if age > timedelta(hours=3):
self.fetch_data()
query = query.lower()
for name, acro in self.acronyms.items():
if query in name:
yield acro
class Runner(dbus.service.Object):
def __init__(self, args):
self.args = args
self.data = LocalData()
dbus.service.Object.__init__(self, dbus.service.BusName("net.acronomy", dbus.SessionBus()), objpath)
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
def Match(self, query: str):
if not self.args.less_privacy:
if not query.startswith(self.args.keyword):
return []
query = query.replace(self.args.keyword + " ", "")
if " " in query:
return []
runners = []
r = s.get("https://acronomy.lw1.at/api/acronym/", params={"search": query})
icon = "plasmagik"
type = 100 # (Plasma::QueryType)
relevance = 0.2 # 0-1
for result in r.json():
for result in self.data.search(query):
data = result["slug"]
display_text = result["name"] + ": " + result["full_name"]
properties = {
@ -61,8 +80,6 @@ class Runner(dbus.service.Object):
def main():
parser = argparse.ArgumentParser(description="acrronomy krunner background task")
parser.add_argument("-k", "--keyword", action="store", default="acr")
parser.add_argument("-l", "--less-privacy", action="store_true", default=False)
args = parser.parse_args()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 88 KiB