summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xwikiquery29
1 files 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 <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()