diff options
author | wjoe <joe@lc8n.com> | 2013-09-05 11:16:32 +0100 |
---|---|---|
committer | wjoe <joe@lc8n.com> | 2013-09-05 11:16:32 +0100 |
commit | aeb86c7fec43c3bf5aea753d51bbb110c97e1f2a (patch) | |
tree | 00992958a1d04382e250f8858c642f3d504988b0 | |
parent | 3532f3b45c2aaf5ef17380cd8c0d5f3ebe5275e4 (diff) | |
parent | cb29aab2fa41b1ed0d01ada8df5352a93e1e5b8e (diff) |
Merge branch 'master' of https://bitbucket.org/Halogenic/wikiquery
-rwxr-xr-x | wikiquery | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -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() |