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

@ -6,17 +6,14 @@ This plugin allows to quickly look up an acronym on [Acronomy](https://acronomy.
#### Installation #### Installation
- copy the `plasma-runner-acronomy.desktop` file to `~/.local/share/kservices5/` - copy the `plasma-runner-acronomy.desktop` file to `~/.loc al/share/kservices5/`
- restart krunner: `kquitapp5 krunner; kstart5 krunner` - restart krunner: `kquitapp5 krunner; kstart5 krunner`
The `acronomy.py` script needs to run permanently in the background to fetch the results. The `acronomy.py` script needs to run permanently in the background to fetch the results.
One easy way to accomplish this is to copy the `acronomy.desktop` to `~/.config/autostart/`. 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. No search queries are sent to the Acronomy server. Instead a list of all acronyms is fetched and searched locally.
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)
This plugin uses the [Astroacro API](https://acronomy.lw1.at/api/). ([Privacy Policy](http://lw1.at/i)) 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 #!/bin/env python3
import argparse import argparse
import webbrowser import webbrowser
from datetime import datetime, timedelta
import dbus.service import dbus.service
import requests import requests
@ -12,33 +13,51 @@ DBusGMainLoop(set_as_default=True)
objpath = "/acronomy" objpath = "/acronomy"
iface = "org.kde.krunner1" iface = "org.kde.krunner1"
s = requests.Session() 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): class Runner(dbus.service.Object):
def __init__(self, args): def __init__(self, args):
self.args = args self.args = args
self.data = LocalData()
dbus.service.Object.__init__(self, dbus.service.BusName("net.acronomy", dbus.SessionBus()), objpath) 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})") @dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
def Match(self, query: str): 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 = [] runners = []
r = s.get("https://acronomy.lw1.at/api/acronym/", params={"search": query})
icon = "plasmagik" icon = "plasmagik"
type = 100 # (Plasma::QueryType) type = 100 # (Plasma::QueryType)
relevance = 0.2 # 0-1 relevance = 0.2 # 0-1
for result in r.json(): for result in self.data.search(query):
data = result["slug"] data = result["slug"]
display_text = result["name"] + ": " + result["full_name"] display_text = result["name"] + ": " + result["full_name"]
properties = { properties = {
@ -61,8 +80,6 @@ class Runner(dbus.service.Object):
def main(): def main():
parser = argparse.ArgumentParser(description="acrronomy krunner background task") 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() args = parser.parse_args()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 88 KiB