Created
April 12, 2020 12:36
-
-
Save tizot/171f4a260851d5175a3cf3a9a05aaf50 to your computer and use it in GitHub Desktop.
Alfred workflow to search on Wikiwand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
""" | |
This workflow requires the python module `alfred-workflow` (https://github.com/deanishe/alfred-workflow). | |
To install it on your system, follow the tutorial on `alfred-workflow` documentation website and | |
replace the python script by this one. | |
""" | |
import sys | |
from workflow import Workflow3 as Workflow, ICON_WEB, ICON_WARNING, web | |
def main(wf): | |
prefix = u"https://www.wikiwand.com/api/search" | |
def get_results(lang, query): | |
resp = web.get(prefix + u"/{}/{}".format(lang, query)) | |
resp.raise_for_status() | |
raw_results = resp.json()["results"] | |
for res in raw_results: | |
url = u"https://www.wikiwand.com/{}/{}".format(lang, res["T"]) | |
wf.add_item( | |
title=res["T"], | |
subtitle=res.get("d", res.get("t")), | |
arg=url, | |
valid=True, | |
icon=ICON_WEB | |
) | |
return len(raw_results) | |
query = wf.args[0] | |
num_fr_results = get_results("fr", query) | |
num_en_results = get_results("en", query) | |
if num_fr_results + num_en_results == 0: | |
wf.add_item(title="No result found", icon=ICON_WARNING) | |
wf.send_feedback() | |
return 0 | |
if __name__ == "__main__": | |
wf = Workflow() | |
sys.exit(wf.run(main)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment