From bc818206f73da8b22f5d7f757007d3fca3e87215 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Wed, 28 Aug 2013 12:03:53 +0200 Subject: better output printing (encodes to utf-8 on output) --- wikiquery | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/wikiquery b/wikiquery index 0597cd9..243bec2 100755 --- a/wikiquery +++ b/wikiquery @@ -4,8 +4,16 @@ import argparse import sys import wikipedia +from functools import partial + version = "0.0.1" +def display(o): + if isinstance(o, list): + print "'%s'" % "', '".join(map(partial(unicode.encode, encoding="utf-8", errors="replace"), o)) + else: + print o.encode("utf-8", "replace") + def parse_args(): parser = argparse.ArgumentParser(prog="!wiki", usage="%(prog)s [--search|--url]") @@ -23,17 +31,18 @@ def main(): print version sys.exit(0) - if args.search: - results = wikipedia.search(args.topic) - if len(results) == 1: - print wikipedia.summary(results[0]) + if args.topic: + if args.search: + results = wikipedia.search(args.topic) + if len(results) == 1: + display(wikipedia.summary(results[0])) + else: + display(results) + elif args.url: + page = wikipedia.page(args.topic) + display(page.url) else: - print results - elif args.url: - page = wikipedia.page(args.topic) - print page.url - elif args.topic: - print wikipedia.summary(args.topic) + display(wikipedia.summary(args.topic)) if __name__ == "__main__": main() -- cgit v1.2.3