initial commit

This commit is contained in:
xenia 2021-06-23 03:04:51 -04:00
commit bd3fcc0ebe
6 changed files with 156 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/haskal-duckduckgo*
/meta.json
__pycache__

32
Makefile Normal file
View File

@ -0,0 +1,32 @@
.PHONY: all clean local-install
LOGO_URL=https://duckduckgo.com/assets/common/dax-logo.svg
ICON_EXISTS=$(shell ./scripts/check-icon.py duckduckgo)
ifeq ($(ICON_EXISTS),false)
export DDG_ICON_NAME=haskal-duckduckgo
all: meta.json haskal-duckduckgo.svg
else
export DDG_ICON_NAME=duckduckgo
all: meta.json
endif
haskal-duckduckgo.svg:
curl -SsLo "$@" "$(LOGO_URL)" || wget -nv -O "$@" "$(LOGO_URL)"
meta.json:
./scripts/make-meta.py > $@
clean:
$(RM) haskal-duckduckgo* meta.json
local-install: all
mkdir -p ~/.local/share/pop-shell/launcher/duckduckgo/
cp duckduckgo_search.py ~/.local/share/pop-shell/launcher/duckduckgo/
cp meta.json ~/.local/share/pop-shell/launcher/duckduckgo/
ifeq ($(ICON_EXISTS),false)
for size in 256 128 96 64 48 32 24 16; do \
inkscape -w $$size -h $$size haskal-duckduckgo.svg -o haskal-duckduckgo.png; \
xdg-icon-resource install --size $$size haskal-duckduckgo.png; \
done
endif

11
README.md Normal file
View File

@ -0,0 +1,11 @@
# pop-shell ddg
adds a duckduckgo capability to pop-shell's launcher
## installation
```bash
make local-install
```
then: log out and log back in

75
duckduckgo_search.py Executable file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env python3
import json
import sys
import webbrowser
from urllib.parse import urlencode, quote_plus
DDG_URL="https://duckduckgo.com/"
def lookup(query):
resp = requests.get(DDG_URL, params={"q":query, "format": "json", "no_html": "1"})
print(resp.text)
def send(event, obj=None):
if obj is None:
obj = {}
obj["event"] = event
json.dump(obj, sys.stdout)
sys.stdout.write("\n")
sys.stdout.flush()
def recv():
for line in sys.stdin:
if len(line.strip()) == 0:
continue
req = json.loads(line)
if not isinstance(req, dict):
send("noop", {"error": "invalid json"})
continue
event = req.get("event", None)
if not isinstance(event, str):
send("noop", {"error": "invalid event type"})
continue
yield event, req
query = ""
go_url = ""
for event, req in recv():
if event == "complete":
send("noop")
elif event == "query":
query = req.get("value", "")
if query.startswith("ddg "):
query = query[4:]
elif query.startswith("ddg"):
query = query[3:]
if len(query) > 0 and query != "!":
go_url = DDG_URL + "?" + urlencode({"q": query}, quote_via=quote_plus)
send("queried", {
"selections": [
{
"id": 0,
"name": f"search {query} on duckduckgo",
"description": go_url,
# "icon": "meow",
# "content_type": "meow"
}
]
})
else:
go_url = ""
send("queried", {"selections": []})
elif event == "submit":
send("close")
if len(go_url) > 0:
webbrowser.open(go_url)
elif event == "quit":
sys.exit(0)
else:
send("noop", {"error": "invalid event"})

20
scripts/check-icon.py Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
import sys
if len(sys.argv) < 2:
sys.stderr.write("usage: check-icon.py <icon name>\n")
sys.exit(-1)
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
theme = Gtk.IconTheme.get_default()
icon = theme.lookup_icon(sys.argv[1], 48, 0)
if icon:
print("true")
sys.exit(0)
else:
print("false")
sys.exit(1)

15
scripts/make-meta.py Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
import json
import sys
import os
meta = {
"name": "duckduckgo",
"description": "search duckduckgo",
"pattern": "^(ddg|!)",
"exec": "duckduckgo_search.py",
"icon": os.environ.get("DDG_ICON_NAME", "duckduckgo")
}
json.dump(meta, sys.stdout)