Created
May 11, 2009 06:41
-
-
Save csytan/109892 to your computer and use it in GitHub Desktop.
wikipedia helper module
This file contains 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
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