# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import urllib2
from urllib import quote, unquote
import re
import os
import time
import ox
from ox import findRe, stripTags
from ox.normalize import normalizeTitle, normalizeImdbId
from siteparser import SiteParser
import google
class Imdb(SiteParser):
regex = {
'alternative_titles': {
'page': 'releaseinfo',
're': [
'name="akas".*?
(.*?)',
"td>(.*?)\n\n(.*?) | "
],
'type': 'list'
},
'cast': {
'page': 'combined',
're': [
'.*?>(.*?).*? | (.*?) | ',
lambda ll: [stripTags(l) for l in ll]
],
'type': 'list'
},
'cinematographers': {
'page': 'combined',
're': [
lambda data: data.split('Series Crew')[0],
'Cinematography by(.*?)',
'(.*?)'
],
'type': 'list'
},
'connections': {
'page': 'movieconnections',
're': '(.*?)
(.*?)\n\n',
'type': 'list'
},
'countries': {
'page': 'combined',
're': '(.*?)',
'type': 'list'
},
'directors': {
'page': 'combined',
're': [
lambda data: data.split('Series Crew')[0],
'Directed by(.*?)',
'(.*?)'
],
'type': 'list'
},
'editors': {
'page': 'combined',
're': [
lambda data: data.split('Series Crew')[0],
'Film Editing by(.*?)',
'(.*?)'
],
'type': 'list'
},
'episode_title': {
'page': 'combined',
're': '.*?
(.*?)',
'type': 'string'
},
'filming_locations': {
'page': 'locations',
're': '
(.*?)',
'type': 'list'
},
'genres': {
'page': 'combined',
're': '
(.*?)',
'type': 'list'
},
'keywords': {
'page': 'keywords',
're': '
(.*?)',
'type': 'list'
},
'languages': {
'page': 'combined',
're': '
(.*?)',
'type': 'list'
},
'original_title': {
'page': 'combined',
're': '',
'type': 'string'
},
'plot': {
'page': 'plotsummary',
're': '
.*?(.*?)',
'type': 'string'
},
'poster_id': {
'page': 'combined',
're': '/primary-photo/media/rm(.*?)/tt',
'type': 'list'
},
'poster_ids': {
'page': 'posters',
're': '/unknown-thumbnail/media/rm(.*?)/tt',
'type': 'list'
},
'producers': {
'page': 'combined',
're': [
lambda data: data.split('Series Crew')[0],
'Produced by(.*?)',
'(.*?)'
],
'type': 'list'
},
'rating': {
'page': 'combined',
're': '.*?
([\d,.]?)/10',
'type': 'float'
},
'release date': {
'page': 'releaseinfo',
're': '
.*? ',
'type': 'date'
},
'reviews': {
'page': 'externalreviews',
're': [
'(.*?)
',
'(.*?)'
],
'type': 'list'
},
'runtime': {
'page': 'combined',
're': 'Runtime:
.*?([0-9]+ sec|[0-9]+ min).*?
',
'type': 'string'
},
'season': {
'page': 'combined',
're': [
'Original Air Date:
.*?(.*?)
',
'\(Season (\d+), Episode \d+\)',
],
'type': 'int'
},
'episode': {
'page': 'combined',
're': [
'Original Air Date:
.*?(.*?)
',
'\(Season \d+, Episode (\d+)\)',
],
'type': 'int'
},
'series': {
'page': 'combined',
're': 'TV Series:
.*?(.*?)
',
'type': 'list',
},
'votes': {
'page': 'combined',
're': '([\d,]*?) votes',
'type': 'string'
},
'writers': {
'page': 'combined',
're': [
lambda data: data.split('Series Crew')[0],
'Writing credits(.*?)',
'
(.*?)'
],
'type': 'list'
},
'year': {
'page': 'combined',
're': '
').findall(data)
self['connections'] = cc
for key in ('countries', 'genres'):
if key in self:
self[key] = filter(lambda x: x.lower() != 'home', self[key])
if 'series' in self:
if 'episode_title' in self:
self['series_title'] = self['title']
self['title'] = "%s: %s" % (self['series_title'], self['episode_title'])
if 'episode_title' in self and 'season' in self and 'episode' in self:
self['title'] = "%s (S%02dE%02d) %s" % (
self['series_title'], self['season'], self['episode'], self['episode_title'])
else:
for key in ('series_title', 'episode_title', 'season', 'episode'):
if key in self:
del self[key]
class ImdbCombined(Imdb):
def __init__(self, id, timeout=-1):
_regex = {}
for key in self.regex:
if self.regex[key]['page'] == 'combined':
_regex[key] = self.regex[key]
self.regex = _regex
super(ImdbCombined, self).__init__(id, timeout)
def getMovieId(title, director='', year=''):
'''
>>> getMovieId('The Matrix')
'0133093'
'''
if year:
title = "%s (%s)" % (title, year)
if director:
query = 'site:imdb.com %s "%s"' % (director, title)
else:
query = 'site:imdb.com "%s"' % title
for (name, url, desc) in google.find(query, 5, timeout=-1):
if url.startswith('http://www.imdb.com/title/tt'):
return url[28:35]
return ''
def guess(title, director='', timeout=google.DEFAULT_TIMEOUT):
#FIXME: proper file -> title
title = title.split('-')[0]
title = title.split('(')[0]
title = title.split('.')[0]
title = title.strip()
imdb_url = 'http://www.imdb.com/find?q=%s' % quote(title.encode('utf-8'))
return_url = ''
#lest first try google
#i.e. site:imdb.com Michael Stevens "Sin"
if director:
search = 'site:imdb.com %s "%s"' % (director, title)
else:
search = 'site:imdb.com "%s"' % title
for (name, url, desc) in google.find(search, 2, timeout=timeout):
if url.startswith('http://www.imdb.com/title/tt'):
return normalizeImdbId(int(ox.intValue(url)))
try:
req = urllib2.Request(imdb_url, None, ox.net.DEFAULT_HEADERS)
u = urllib2.urlopen(req)
data = u.read()
return_url = u.url
u.close()
except:
return None
if return_url.startswith('http://www.imdb.com/title/tt'):
return return_url[28:35]
if data:
imdb_id = findRe(data.replace('\n', ' '), 'Popular Results.*?
- .*?