diff options
author | Alasdair <alnessy@hotmail.com> | 2013-08-28 12:03:53 +0200 |
---|---|---|
committer | Alasdair <alnessy@hotmail.com> | 2013-08-28 12:03:53 +0200 |
commit | bc818206f73da8b22f5d7f757007d3fca3e87215 (patch) | |
tree | 89e795d85d91458451592f33535bc792052f5730 | |
parent | fd72b60cfd6f84545b6c8ec5d7396a29356c7fbb (diff) |
better output printing (encodes to utf-8 on output)
-rwxr-xr-x | wikiquery | 29 |
1 files changed, 19 insertions, 10 deletions
@@ -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 <topic> [--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() |