34
34
from dbschema import *
36
36
from imdb.parser.common.locsql import IMDbLocalAndSqlAccessSystem, \
37
scan_names, scan_titles, titleVariations, nameVariations
38
from imdb.utils import canonicalTitle, canonicalName, normalizeTitle, \
39
normalizeName, build_title, build_name, \
40
analyze_name, analyze_title, re_episodes, _articles
37
scan_names, scan_titles, titleVariations, \
38
nameVariations, merge_roles
39
from imdb.utils import normalizeTitle, normalizeName, build_title, \
40
build_name, analyze_name, analyze_title, \
41
re_episodes, _articles
41
42
from imdb.Person import Person
42
43
from imdb.Movie import Movie
43
44
from imdb._exceptions import IMDbDataAccessError, IMDbError
337
def get_imdbCharacterID(self, characterID):
338
"""Translate a characterID in an imdbID.
339
If not in the database, try an Exact Primary Name search on IMDb;
340
return None if it's unable to get the imdbID.
342
try: character = CharName.get(characterID)
343
except SQLObjectNotFound: return None
344
imdbID = character.imdbID
345
if imdbID is not None: return '%07d' % imdbID
346
n_dict = {'name': character.name, 'imdbIndex': character.imdbIndex}
347
namline = build_name(n_dict, canonical=1)
348
imdbID = self.character2imdbID(namline)
349
if imdbID is not None:
350
try: character.imdbID = int(imdbID)
329
354
def do_adult_search(self, doAdult):
330
355
"""If set to 0 or False, movies in the Adult category are not
331
356
episodeOf = title_dict.get('episode of')
640
672
raise IMDbDataAccessError, 'unable to get personID "%s"' % personID
641
673
# Collect cast information.
642
castdata = [(cd.movieID, cd.personRole, cd.note, self._role[cd.roleID],
674
castdata = [(cd.movieID, cd.personRoleID, cd.note,
675
self._role[cd.roleID],
643
676
get_movie_data(cd.movieID, self._kind))
644
677
for cd in CastInfo.select(CastInfo.q.personID == personID)]
645
678
# Regroup by role/duty (cast, writer, director, ...)
733
776
get_person_episodes = get_person_main
735
778
def _search_character(self, name, results):
737
warnings.warn('Character objects still unsupported for "sql"')
740
def get_character_main(self, characterID):
742
warnings.warn('Character objects still unsupported for "sql"')
780
if not name: return []
781
s_name = analyze_name(name)['name']
782
if not s_name: return []
783
if isinstance(s_name, UnicodeType):
784
s_name = s_name.encode('ascii', 'ignore')
785
s_name = normalizeName(s_name)
786
soundexCode = soundex(s_name)
787
surname = s_name.split(' ')[-1]
788
surnameSoundex = soundex(surname)
791
nsplit = s_name.split()
793
name2 = '%s %s' % (nsplit[-1], ' '.join(nsplit[:-1]))
797
soundexName2 = soundex(name2)
798
# If the soundex is None, compare only with the first
799
# phoneticCode column.
800
if soundexCode is not None:
801
if soundexName2 is not None:
802
condition = OR(surnameSoundex == CharName.q.surnamePcode,
803
IN(CharName.q.namePcodeNf, [soundexCode,
805
IN(CharName.q.surnamePcode, [soundexCode,
808
condition = OR(surnameSoundex == CharName.q.surnamePcode,
809
IN(soundexCode, [CharName.q.namePcodeNf,
810
CharName.q.surnamePcode]))
812
condition = ISNULL(Name.q.namePcodeNf)
814
qr = [(q.id, {'name': q.name, 'imdbIndex': q.imdbIndex})
815
for q in CharName.select(condition)]
816
except SQLObjectNotFound, e:
817
raise IMDbDataAccessError, \
818
'unable to search the database: "%s"' % str(e)
819
res = scan_names(qr, s_name, name2, '', results,
820
_scan_character=True)
821
res[:] = [x[1] for x in res]
822
# Purge empty imdbIndex.
826
if tmpd['imdbIndex'] is None:
827
del tmpd['imdbIndex']
828
returnl.append((x[0], tmpd))
831
def get_character_main(self, characterID, results=1000):
832
# Every person information is retrieved from here.
833
infosets = self.get_character_infoset()
835
c = CharName.get(characterID)
836
except SQLObjectNotFound, e:
837
raise IMDbDataAccessError, \
838
'unable to get characterID "%s": "%s"' % (characterID, e)
839
res = {'name': c.name, 'imdbIndex': c.imdbIndex}
840
if res['imdbIndex'] is None: del res['imdbIndex']
842
raise IMDbDataAccessError, 'unable to get characterID "%s"' % \
844
# Collect filmography information.
845
items = CastInfo.select(CastInfo.q.personRoleID == characterID)
847
items = items[:results]
848
filmodata = [(cd.movieID, cd.personID, cd.note,
849
get_movie_data(cd.movieID, self._kind)) for cd in items
850
if self._role[cd.roleID] in ('actor', 'actress')]
856
if curRoleID is not None:
857
robj = Name.get(curRoleID)
859
m = Movie(movieID=f[0], data=f[3],
860
currentRole=curRole or u'',
861
roleID=curRoleID, roleIsPerson=True,
862
notes=note, accessSystem='sql')
864
fdata = merge_roles(fdata)
867
res['filmography'] = fdata
868
return {'data': res, 'info sets': infosets}
744
870
get_character_filmography = get_character_main
745
871
get_character_biography = get_character_main