summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlasdair <alnessy@hotmail.com>2013-09-05 12:14:50 +0200
committerAlasdair <alnessy@hotmail.com>2013-09-05 12:14:50 +0200
commitcb29aab2fa41b1ed0d01ada8df5352a93e1e5b8e (patch)
tree00992958a1d04382e250f8858c642f3d504988b0
parentba8ee3a6be4049c2bccc03976b7396d7e11b89ca (diff)
now accepts an index argument as second arg to choose a specific result from lists of arguments; changed to use double quotes instead
-rwxr-xr-xwikiquery30
1 files changed, 23 insertions, 7 deletions
diff --git a/wikiquery b/wikiquery
index 7682d78..10356dd 100755
--- a/wikiquery
+++ b/wikiquery
@@ -8,17 +8,19 @@ from functools import partial
version = "0.0.2"
-def display(o):
+def stringify(o):
if isinstance(o, list):
-
output = []
for item in o:
output.append(item.encode("utf-8", "replace"))
- print "'%s'" % "', '".join(output)
+ return '"%s"' % '", "'.join(output)
else:
- print o.encode("utf-8", "replace")
+ return o.encode("utf-8", "replace")
+def display(o):
+ print stringify(o)
+
def parse_args():
parser = optparse.OptionParser(usage="!wiki <topic> [--search|--url]")
@@ -36,13 +38,21 @@ def main():
sys.exit(0)
if args:
- topic = args[0]
+ topic = args.pop(0)
+ index = int(args.pop(0)) if args else None
+
if options.search:
results = wikipedia.search(topic)
if len(results) == 1:
display(wikipedia.summary(results[0]))
else:
- display(results)
+ if index is not None:
+ try:
+ display(wikipedia.summary(results[index]))
+ except IndexError:
+ print "index out of range, options are: [%s]" % stringify(results)
+ else:
+ display(results)
elif options.url:
page = wikipedia.page(topic)
display(page.url)
@@ -50,7 +60,13 @@ def main():
try:
display(wikipedia.summary(topic))
except wikipedia.exceptions.DisambiguationError as e:
- display(e.options)
+ if index is not None:
+ try:
+ display(wikipedia.summary(e.options[index]))
+ except IndexError:
+ print "index out of range, options are: [%s]" % stringify(e.options)
+ else:
+ display(e.options)
if __name__ == "__main__":
main()