2008-04-29 13:54:08 +00:00
|
|
|
# -*- 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 ''
|
2008-04-29 14:14:06 +00:00
|
|
|
result = find("imdb_id "+imdbId)
|
2008-04-29 13:54:08 +00:00
|
|
|
if result:
|
|
|
|
url = result[0][1]
|
2008-04-29 14:14:06 +00:00
|
|
|
data = getUrl(url)
|
|
|
|
if 'title/tt%s' % imdbId in data:
|
|
|
|
return url
|
2008-04-29 13:54:08 +00:00
|
|
|
return ''
|
|
|
|
|
2008-05-10 09:10:23 +00:00
|
|
|
def getUrlByAmbId(amg_id):
|
|
|
|
query = '"amg_id = %s"'% amg_id
|
|
|
|
result = find(query)
|
|
|
|
if result:
|
|
|
|
url = result[0][1]
|
|
|
|
return url
|
|
|
|
return ''
|
|
|
|
|
2008-04-29 13:54:08 +00:00
|
|
|
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)
|
2008-05-10 09:10:23 +00:00
|
|
|
if not data:
|
|
|
|
data = getUrl(url, timeout=0)
|
2008-04-29 13:54:08 +00:00
|
|
|
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
|
|
|
|
|