Skip to content

Instantly share code, notes, and snippets.

@csytan
Created May 11, 2009 06:41
Show Gist options
  • Save csytan/109892 to your computer and use it in GitHub Desktop.
Save csytan/109892 to your computer and use it in GitHub Desktop.
wikipedia helper module
import urllib
from django.utils import simplejson
def suggest(prefix, lang='en'):
url = 'http://' + lang + '.wikipedia.org/w/api.php?action=opensearch'
url += '&search=' + urllib.quote(prefix) + '&format=json'
json = urllib.urlopen(url).read()
data = simplejson.loads(json)
results = data[1]
return results
def translate_title(title, source='en', target='fr'):
url = 'http://' + source + '.wikipedia.org/w/api.php?action=query'
url += '&prop=langlinks&lllimit=500&redirects&format=json'
url += '&titles=' + urllib.quote(title)
json = urllib.urlopen(url).read()
data = simplejson.loads(json)
langlinks = data['query']['pages'].values()[0].get('langlinks', [])
for link in langlinks:
if link['lang'] == target:
return link['*'].encode('utf-8')
def check_title(title):
url = 'http://en.wikipedia.org/w/api.php?action=query'
url += '&redirects&format=json'
url += '&titles=' + urllib.quote(title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment