Merge remote-tracking branch 'origin/master'

This commit is contained in:
James Booth
2016-03-16 20:33:07 +00:00
3 changed files with 245 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ cp browser.py ~/.local/share/profanity/plugins/.
cp platform-info.py ~/.local/share/profanity/plugins/.
cp say.py ~/.local/share/profanity/plugins/.
cp whoami.py ~/.local/share/profanity/plugins/.
cp wikipedia-prof.py ~/.local/share/profanity/plugins/.
cp tests/python-test.py ~/.local/share/profanity/plugins/.
cd pid

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" ])

226
wikipedia-prof.py Normal file
View File

@@ -0,0 +1,226 @@
"""
Requires wikipedia API
pip install wikipedia
Example theme options:
[wikipedia]
error=red
search=bold_green
search.noresult=red
search.results=green
summary.nopage=red
summary.title=bold_green
summary.url=magenta
summary.text=cyan
page.nopage=red
page.title=bold_green
page.text=cyan
images.nopage=red
images=bold_green
images.url=magenta
links.nopage=red
links=bold_green
links.link=green
refs.nopage=red
refs=bold_green
refs.url=magenta
"""
import prof
import wikipedia
import os
import webbrowser
import requests
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)
prof.register_ac("/wikipedia refs", 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("utf-8"))
prof.win_show_themed(win, "wikipedia", "search.results", None, result.encode("utf-8"))
_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("utf-8"))
prof.register_ac("/wikipedia open", link_ac)
prof.win_show_themed(win, "wikipedia", "summary.title", None, page.title.encode("utf-8"))
prof.win_show_themed(win, "wikipedia", "summary.url", None, page.url.encode("utf-8"))
summary = wikipedia.summary(page_str)
prof.win_show_themed(win, "wikipedia", "summary.text", None, summary.encode("utf-8"))
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("utf-8"))
prof.win_show_themed(win, "wikipedia", "page.text", None, page.content.encode("utf-8"))
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("utf-8"))
link_ac.append(image.encode("utf-8"))
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)
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)
for link in page.links:
prof.win_show_themed(win, "wikipedia", "links.link", None, link.encode("utf-8"))
page_ac.append(link.encode("utf-8"))
_update_autocomplete()
prof.win_show(win, "")
prof.win_focus(win)
def _refs(page_str):
global link_ac
page = wikipedia.page(page_str)
create_win()
if not page:
prof.win_show_themed(win, "wikipedia", "refs.nopage", None, "No such page: \"" + page_str + "\"")
prof.win_show(win, "")
prof.win_focus(win)
return
prof.win_show_themed(win, "wikipedia", "refs", None, "References for " + page_str)
for ref in page.references:
prof.win_show_themed(win, "wikipedia", "refs.url", None, ref.encode("utf-8"))
link_ac.append(ref.encode("utf-8"))
prof.register_ac("/wikipedia open", link_ac)
prof.win_show(win, "")
prof.win_focus(win)
def cmd_wp(subcmd, arg):
try:
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 == "refs": _refs(arg)
elif subcmd == "open": _open_browser(arg)
except wikipedia.exceptions.WikipediaException as e:
create_win()
prof.win_show_themed(win, "wikipedia", "error", None, str(e))
prof.win_show(win, "")
prof.win_focus(win)
except requests.exceptions.ConnectionError:
create_win()
prof.win_show_themed(win, "wikipedia", "error", None, "Connection Error")
prof.win_show(win, "")
prof.win_focus(win)
def prof_init(version, status):
synopsis = [
"/wikipedia search <text>",
"/wikipedia summary <title>",
"/wikipedia page <title>",
"/wikipedia images <title>",
"/wikipedia links <title>",
"/wikipedia refs <title>",
"/wikipedia open <url>"
]
description = "Interact with wikipedia."
args = [
[ "search <text>", "Search for pages" ],
[ "summary <title>", "Show summary for page" ],
[ "page <title>", "Show the whole page" ],
[ "images <title>", "Show images URLs for page" ],
[ "links <title>", "Show links to other pages from page" ],
[ "refs <title>", "Show external references for 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", "refs", "open" ]
)