summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xwikiquery68
1 files changed, 44 insertions, 24 deletions
diff --git a/wikiquery b/wikiquery
index 7d1ed5f..b05ccd9 100755
--- a/wikiquery
+++ b/wikiquery
@@ -7,7 +7,7 @@ import wikipedia
from functools import partial
-version = "0.1.0"
+version = "0.1.1"
def stringify(o):
if isinstance(o, list):
@@ -21,6 +21,45 @@ def stringify(o):
def display(o):
print stringify(o)
+
+def search(topic, index):
+ results = wikipedia.search(topic)
+ if len(results) == 1:
+ display(wikipedia.summary(results[0]))
+ else:
+ 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)
+
+def url(topic, index):
+ try:
+ page = wikipedia.page(topic)
+ except wikipedia.exceptions.DisambiguationError as e:
+ if index is not None:
+ try:
+ display(wikipedia.page(e.options[index]).url)
+ except IndexError:
+ print "index out of range, options are: [%s]" % stringify(e.options)
+ else:
+ display(e.options)
+ else:
+ display(page.url)
+
+def summary(topic, index):
+ try:
+ display(wikipedia.summary(topic))
+ except wikipedia.exceptions.DisambiguationError as e:
+ 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)
def parse_args():
args = sys.argv[1:]
@@ -48,31 +87,12 @@ def main():
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:
- 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)
+ search(topic, index)
elif options.url:
- page = wikipedia.page(topic)
- display(page.url)
+ url(topic, index)
else:
- try:
- display(wikipedia.summary(topic))
- except wikipedia.exceptions.DisambiguationError as e:
- 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)
+ summary(topic, index)
+
if __name__ == "__main__":
main()