From f7947fb72add45ae157e02854367d17812fad6bd Mon Sep 17 00:00:00 2001 From: j Date: Tue, 29 Apr 2008 15:54:08 +0200 Subject: [PATCH] some wikipedia --- ox/wikipedia.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ox/wikipedia.py diff --git a/ox/wikipedia.py b/ox/wikipedia.py new file mode 100644 index 0000000..9a81133 --- /dev/null +++ b/ox/wikipedia.py @@ -0,0 +1,37 @@ +# -*- Mode: Python; -*- +# -*- coding: utf-8 -*- +# vi:si:et:sw=2:sts=2:ts=2 +from urllib import urlencode + +import simplejson +from oxutils.cache import getUrl + + +def getMovieId(title, director='', year=''): + query = '"%s" film %s %s' % (title, director, year) + result = find(query, 1) + if result: + return result[0][1] + return '' + +def getUrlByImdb(imdbId): + if len(imdbId) != 7: return '' + result = find(imdbId) + if result: + url = result[0][1] + return url + return '' + +def find(query, max_results=10): + query = {'action': 'query', 'list':'search', 'format': 'json', + 'srlimit': max_results, 'srwhat': 'text', 'srsearch': query.encode('utf-8')} + url = "http://en.wikipedia.org/w/api.php?" + urlencode(query) + data = getUrl(url) + result = simplejson.loads(data) + results = [] + for r in result['query']['search']: + title = r['title'] + url = "http://en.wikipedia.org/wiki/%s" % title.replace(' ', '_') + results.append((title, url, '')) + return results +