Last active
August 29, 2015 14:02
-
-
Save pzp1997/42c03ce0a0f630270b8f to your computer and use it in GitHub Desktop.
Easily find random Wikipedia articles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
from urllib2 import urlopen, URLError | |
from json import loads | |
from webbrowser import open_new_tab | |
def getArticles(): | |
print "Fetching articles..." | |
try: | |
wiki = urlopen("http://en.wikipedia.org/w/api.php?action=query&list=" | |
"random&rnnamespace=0&rnlimit=10&format=json") | |
except URLError: | |
print "The Wikipedia API is not accessible at the moment. Please try" \ | |
" again later." | |
raise SystemExit | |
return loads(wiki.read())["query"]["random"] | |
def main(): | |
print "WikiRand.py by pzp1997 (06/14/14)" | |
print "Input anything to read an article, nothing to skip an article," \ | |
" and 'exit' to quit." | |
i = "" | |
while True: | |
articles = getArticles() | |
for x in range(len(articles)): | |
try: | |
i = raw_input(articles[x]["title"] + ": ") | |
if i == "exit": | |
print "Goodbye!" | |
raise SystemExit | |
if not i == "": | |
open_new_tab("http://en.wikipedia.org/wiki?curid=" | |
+ str(articles[x]["id"])) | |
except UnicodeEncodeError: | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment