Added wikipedia plugin

This commit is contained in:
James Booth
2016-03-15 22:36:00 +00:00
parent 5110af0bb5
commit 821c43dcc8
2 changed files with 182 additions and 11 deletions

View File

@@ -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 <command>",
"/system exec <comman>",
"/system send <command>"
]
description = "Run a system command, calling with no arguments will open or focus the system window."
args = [
[ "send <command>", "Send the result of the command to the current recipient or room" ],
[ "<command>", "The system command to run" ]
[ "exec <command>", "Execute a command" ],
[ "send <command>", "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" ])

164
wikipedia-prof.py Normal file
View File

@@ -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 <text>",
"/wikipedia summary <num>",
"/wikipedia page <num>",
"/wikipedia images <num>",
"/wikipedia links <num>",
"/wikipedia open <url>"
]
description = "Interact with wikipedia."
args = [
[ "search <text>", "Search for pages" ],
[ "summary <page>", "Show summary for page" ],
[ "page <page>", "Show the whole page" ],
[ "images <page>", "Show images URLs for page" ],
[ "links <page>", "Show links to other pages from page" ],
[ "open <url>", "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" ]
)