From 821c43dcc86bd11df6041d052c6f83fbcedd25fc Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 15 Mar 2016 22:36:00 +0000 Subject: [PATCH] Added wikipedia plugin --- syscmd.py | 29 ++++---- wikipedia-prof.py | 164 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 11 deletions(-) create mode 100644 wikipedia-prof.py diff --git a/syscmd.py b/syscmd.py index 8efe78b..d8db732 100644 --- a/syscmd.py +++ b/syscmd.py @@ -20,7 +20,10 @@ def create_win(): prof.win_create(system_win, _handle_win_input) def _cmd_system(arg1=None, arg2=None): - if arg1 == "send": + if not arg1: + create_win() + prof.win_focus(system_win) + elif arg1 == "send": if arg2 == None: prof.cons_bad_cmd_usage("/system") else: @@ -32,26 +35,30 @@ def _cmd_system(arg1=None, arg2=None): else: result = _get_result(arg2) prof.send_line(u'\u000A' + result) + elif arg1 == "exec": + if arg2 == None: + prof.cons_bad_cmd_usage("/system") + else: + create_win() + prof.win_focus(system_win) + _handle_win_input(system_win, arg2) else: - create_win() - prof.win_focus(system_win) - if arg1: - _handle_win_input(system_win, arg1) + prof.cons_bad_cmd_usage("/system") def prof_init(version, status): synopsis = [ "/system", - "/system ", + "/system exec ", "/system send " ] description = "Run a system command, calling with no arguments will open or focus the system window." args = [ - [ "send ", "Send the result of the command to the current recipient or room" ], - [ "", "The system command to run" ] + [ "exec ", "Execute a command" ], + [ "send ", "Send the result of the command to the current recipient or room" ] ] examples = [ - "/system \"ls -l\"", - "/system send \"uname -a\"" + "/system exec ls -l", + "/system send uname -a" ] prof.register_command("/system", 0, 2, synopsis, description, args, examples, _cmd_system) - prof.register_ac("/system", [ "send" ]) + prof.register_ac("/system", [ "exec", "send" ]) diff --git a/wikipedia-prof.py b/wikipedia-prof.py new file mode 100644 index 0000000..d02eddb --- /dev/null +++ b/wikipedia-prof.py @@ -0,0 +1,164 @@ +import prof + +import wikipedia +import os +import webbrowser + +win = "Wikipedia" +page_ac = [] +link_ac = [] + +def _open_browser(url): + savout = os.dup(1) + saverr = os.dup(2) + os.close(1) + os.close(2) + os.open(os.devnull, os.O_RDWR) + try: + webbrowser.open(url, new=2) + finally: + os.dup2(savout, 1) + os.dup2(saverr, 2) + +def _handle_win_input(): + pass + +def create_win(): + if prof.win_exists(win) == False: + prof.win_create(win, _handle_win_input) + +def _update_autocomplete(): + prof.register_ac("/wikipedia page", page_ac) + prof.register_ac("/wikipedia summary", page_ac) + prof.register_ac("/wikipedia images", page_ac) + prof.register_ac("/wikipedia links", page_ac) + +def _search(search_terms): + global page_ac + + results = wikipedia.search(search_terms) + create_win() + if len(results) > 0: + prof.win_show_themed(win, "wikipedia", "search", None, "Search results for \"" + search_terms + "\":") + for index, result in enumerate(results): + page_ac.append(result.encode("utf8")) + prof.win_show_themed(win, "wikipedia", "search.results", None, result.encode("utf8")) + _update_autocomplete() + else: + prof.win_show_themed(win, "wikipedia", "search.noresults", None, "No search results found for \"" + search_terms + "\"") + prof.win_show(win, "") + prof.win_focus(win) + +def _summary(page_str): + global link_ac + + page = wikipedia.page(page_str) + create_win() + if not page: + prof.win_show_themed(win, "wikipedia", "summary.nopage", None, "No such page: \"" + page_str + "\"") + prof.win_show(win, "") + prof.win_focus(win) + return + + link_ac.append(page.url.encode("utf8") + prof.register_ac("/wikipedia open", link_ac) + + prof.win_show_themed(win, "wikipedia", "summary.title", None, page.title.encode("utf8")) + prof.win_show_themed(win, "wikipedia", "summary.url", None, page.url.encode("utf8")) + + summary = wikipedia.summary(page_str) + prof.win_show_themed(win, "wikipedia", "summary.summary", None, summary.encode("utf8")) + prof.win_show(win, "") + prof.win_focus(win) + +def _page(page_str): + page = wikipedia.page(page_str) + create_win() + if not page: + prof.win_show_themed(win, "wikipedia", "page.nopage", None, "No such page: \"" + page_str + "\"") + prof.win_show(win, "") + prof.win_focus(win) + return + + prof.win_show_themed(win, "wikipedia", "page.title", None, page.title.encode("utf8")) + prof.win_show_themed(win, "wikipedia", "page.content", None, page.content.encode("utf8")) + prof.win_show(win, "") + prof.win_focus(win) + +def _images(page_str): + global link_ac + + page = wikipedia.page(page_str) + create_win() + if not page: + prof.win_show_themed(win, "wikipedia", "images.nopage", None, "No such page: \"" + page_str + "\"") + prof.win_show(win, "") + prof.win_focus(win) + return + + prof.win_show_themed(win, "wikipedia", "images", None, "Images for \"" + page_str + "\"") + for image in page.images: + prof.win_show_themed(win, "wikipedia", "images.url", None, image.encode("utf8")) + link_ac.append(image.encode("utf8") + prof.register_ac("/wikipedia open", link_ac) + prof.win_show(win, "") + prof.win_focus(win) + +def _links(page_str): + global page_ac + + page = wikipedia.page(page_str.encode("utf8")) + create_win() + if not page: + prof.win_show_themed(win, "wikipedia", "links.nopage", None, "No such page: \"" + page_str + "\"") + prof.win_show(win, "") + prof.win_focus(win) + return + + prof.win_show_themed(win, "wikipedia", "links", None, "Links for \"" + page_str + "\"") + + i = 0 + while i < len(page.links): + link = page.links[i] + prof.win_show_themed(win, "wikipedia", "links.link", None, link.encode("utf8")) + page_ac.append(link.encode("utf8") + i += 1 + _update_autocomplete() + prof.win_show(win, "") + prof.win_focus(win) + +def cmd_wp(subcmd, arg): + if subcmd == "search": _search(arg) + elif subcmd == "summary": _summary(arg) + elif subcmd == "page": _page(arg) + elif subcmd == "images": _images(arg) + elif subcmd == "links": _links(arg) + elif subcmd == "open": _open_browser(arg) + +def prof_init(version, status): + synopsis = [ + "/wikipedia search ", + "/wikipedia summary ", + "/wikipedia page ", + "/wikipedia images ", + "/wikipedia links ", + "/wikipedia open " + ] + description = "Interact with wikipedia." + args = [ + [ "search ", "Search for pages" ], + [ "summary ", "Show summary for page" ], + [ "page ", "Show the whole page" ], + [ "images ", "Show images URLs for page" ], + [ "links ", "Show links to other pages from page" ], + [ "open ", "Open the a URL in the browser" ] + ] + examples = [ + "/wikipedia search Iron Maiden" + ] + + prof.register_command("/wikipedia", 2, 2, synopsis, description, args, examples, cmd_wp) + + prof.register_ac("/wikipedia", + [ "search", "summary", "page", "images", "links", "open" ] + )