diff options
-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() |