Fixes for wikipedia plugin

This commit is contained in:
James Booth
2016-03-16 00:00:21 +00:00
parent 821c43dcc8
commit 88fcd1d830
2 changed files with 50 additions and 28 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

@@ -3,6 +3,7 @@ import prof
import wikipedia
import os
import webbrowser
import sys
win = "Wikipedia"
page_ac = []
@@ -32,6 +33,7 @@ def _update_autocomplete():
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
@@ -41,8 +43,8 @@ def _search(search_terms):
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"))
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 + "\"")
@@ -60,14 +62,14 @@ def _summary(page_str):
prof.win_focus(win)
return
link_ac.append(page.url.encode("utf8")
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("utf8"))
prof.win_show_themed(win, "wikipedia", "summary.url", None, page.url.encode("utf8"))
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.summary", None, summary.encode("utf8"))
prof.win_show_themed(win, "wikipedia", "summary.text", None, summary.encode("utf-8"))
prof.win_show(win, "")
prof.win_focus(win)
@@ -80,8 +82,8 @@ def _page(page_str):
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_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)
@@ -96,10 +98,10 @@ def _images(page_str):
prof.win_focus(win)
return
prof.win_show_themed(win, "wikipedia", "images", None, "Images for \"" + page_str + "\"")
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.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)
@@ -107,7 +109,7 @@ def _images(page_str):
def _links(page_str):
global page_ac
page = wikipedia.page(page_str.encode("utf8"))
page = wikipedia.page(page_str)
create_win()
if not page:
prof.win_show_themed(win, "wikipedia", "links.nopage", None, "No such page: \"" + page_str + "\"")
@@ -115,42 +117,61 @@ def _links(page_str):
prof.win_focus(win)
return
prof.win_show_themed(win, "wikipedia", "links", None, "Links for \"" + page_str + "\"")
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
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):
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)
def prof_init(version, status):
synopsis = [
"/wikipedia search <text>",
"/wikipedia summary <num>",
"/wikipedia page <num>",
"/wikipedia images <num>",
"/wikipedia links <num>",
"/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 <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" ],
[ "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 = [
@@ -160,5 +181,5 @@ def prof_init(version, status):
prof.register_command("/wikipedia", 2, 2, synopsis, description, args, examples, cmd_wp)
prof.register_ac("/wikipedia",
[ "search", "summary", "page", "images", "links", "open" ]
[ "search", "summary", "page", "images", "links", "refs", "open" ]
)