~ubuntu-branches/ubuntu/karmic/imdbpy/karmic

« back to all changes in this revision

Viewing changes to imdb/parser/sql/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2008-04-20 19:51:14 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080420195114-csn8kwgswrqlr63h
Tags: 3.5-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
the imdb.IMDb function will return an instance of this class when
8
8
called with the 'accessSystem' argument set to "sql", "database" or "db".
9
9
 
10
 
Copyright 2005-2007 Davide Alberani <da@erlug.linux.it>
 
10
Copyright 2005-2008 Davide Alberani <da@erlug.linux.it>
11
11
 
12
12
This program is free software; you can redistribute it and/or modify
13
13
it under the terms of the GNU General Public License as published by
34
34
from dbschema import *
35
35
 
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
286
287
            raise IMDbError, 'personID "%s" can\'t be converted to integer' % \
287
288
                            personID
288
289
 
 
290
    def _normalize_characterID(self, characterID):
 
291
        """Normalize the given characterID."""
 
292
        try:
 
293
            return int(characterID)
 
294
        except (ValueError, OverflowError):
 
295
            raise IMDbError, 'characterID "%s" can\'t be converted to integer' \
 
296
                            % characterID
289
297
 
290
298
    def get_imdbMovieID(self, movieID):
291
299
        """Translate a movieID in an imdbID.
326
334
            except: pass
327
335
        return imdbID
328
336
 
 
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.
 
341
        """
 
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)
 
351
            except: pass
 
352
        return imdbID
 
353
 
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')
431
456
        if not res:
432
457
            raise IMDbDataAccessError, 'unable to get movieID "%s"' % movieID
433
458
        # Collect cast information.
434
 
        castdata = [[cd.personID, cd.personRole, cd.note, cd.nrOrder,
 
459
        castdata = [[cd.personID, cd.personRoleID, cd.note, cd.nrOrder,
435
460
                    self._role[cd.roleID]]
436
461
                    for cd in CastInfo.select(CastInfo.q.movieID == movieID)]
437
462
        for p in castdata:
444
469
        for group in castdata:
445
470
            duty = group[0][4]
446
471
            for pdata in group:
 
472
                curRole = pdata[1]
 
473
                curRoleID = None
 
474
                if curRole is not None:
 
475
                    robj = CharName.get(curRole)
 
476
                    curRole = robj.name
 
477
                    curRoleID = robj.id
447
478
                p = Person(personID=pdata[0], name=pdata[5],
448
 
                            currentRole=pdata[1] or u'', notes=pdata[2] or u'',
 
479
                            currentRole=curRole or u'',
 
480
                            roleID=curRoleID,
 
481
                            notes=pdata[2] or u'',
449
482
                            accessSystem='sql')
450
483
                if pdata[6]: p['imdbIndex'] = pdata[6]
451
484
                p.billingPos = pdata[3]
452
485
                res.setdefault(duty, []).append(p)
 
486
            if duty == 'cast':
 
487
                res[duty] = merge_roles(res[duty])
453
488
            res[duty].sort()
454
489
        # Info about the movie.
455
490
        minfo = [(self._info[m.infoTypeID], m.info, m.note)
612
647
            raise IMDbDataAccessError, \
613
648
                    'unable to search the database: "%s"' % str(e)
614
649
 
615
 
        resultsST = results
616
 
        if not self.doAdult: resultsST = 0
617
 
        res = scan_names(qr, name1, name2, name3, resultsST)
618
 
        if results > 0: res[:] = res[:results]
 
650
        res = scan_names(qr, name1, name2, name3, results)
619
651
        res[:] = [x[1] for x in res]
620
652
        # Purge empty imdbIndex.
621
653
        returnl = []
639
671
        if not res:
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, ...)
656
689
                    if orig_duty not in ('actor', 'actress'):
657
690
                        if note: note = ' %s' % note
658
691
                        note = '[%s]%s' % (orig_duty, note)
 
692
                curRole = mdata[1]
 
693
                curRoleID = None
 
694
                if curRole is not None:
 
695
                    robj = CharName.get(curRole)
 
696
                    curRole = robj.name
 
697
                    curRoleID = robj.id
659
698
                m = Movie(movieID=mdata[0], data=mdata[4],
660
 
                            currentRole=mdata[1] or u'',
 
699
                            currentRole=curRole or u'',
 
700
                            roleID=curRoleID,
661
701
                            notes=note, accessSystem='sql')
662
702
                if duty != 'episodes':
663
703
                    res.setdefault(duty, []).append(m)
670
710
            res['episodes'] = episodes
671
711
        for duty in seenDuties:
672
712
            if res.has_key(duty):
 
713
                if duty in ('actor', 'actress', 'himself', 'herself',
 
714
                            'themselves'):
 
715
                    res[duty] = merge_roles(res[duty])
673
716
                res[duty].sort()
674
717
        # XXX: is 'guest' still needed?  I think every GA reference in
675
718
        #      the biographies.list file was removed.
733
776
    get_person_episodes = get_person_main
734
777
 
735
778
    def _search_character(self, name, results):
736
 
        import warnings
737
 
        warnings.warn('Character objects still unsupported for "sql"')
738
 
        return []
739
 
 
740
 
    def get_character_main(self, characterID):
741
 
        import warnings
742
 
        warnings.warn('Character objects still unsupported for "sql"')
743
 
        return {}
 
779
        name = name.strip()
 
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)
 
789
        name2 = ''
 
790
        soundexName2 = None
 
791
        nsplit = s_name.split()
 
792
        if len(nsplit) > 1:
 
793
            name2 = '%s %s' % (nsplit[-1], ' '.join(nsplit[:-1]))
 
794
            if s_name == name2:
 
795
                name2 = ''
 
796
            else:
 
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,
 
804
                                                        soundexName2]),
 
805
                            IN(CharName.q.surnamePcode, [soundexCode,
 
806
                                                        soundexName2]))
 
807
            else:
 
808
                condition = OR(surnameSoundex == CharName.q.surnamePcode,
 
809
                            IN(soundexCode, [CharName.q.namePcodeNf,
 
810
                                            CharName.q.surnamePcode]))
 
811
        else:
 
812
            condition = ISNULL(Name.q.namePcodeNf)
 
813
        try:
 
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.
 
823
        returnl = []
 
824
        for x in res:
 
825
            tmpd = x[1]
 
826
            if tmpd['imdbIndex'] is None:
 
827
                del tmpd['imdbIndex']
 
828
            returnl.append((x[0], tmpd))
 
829
        return returnl
 
830
 
 
831
    def get_character_main(self, characterID, results=1000):
 
832
        # Every person information is retrieved from here.
 
833
        infosets = self.get_character_infoset()
 
834
        try:
 
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']
 
841
        if not res:
 
842
            raise IMDbDataAccessError, 'unable to get characterID "%s"' % \
 
843
                                        characterID
 
844
        # Collect filmography information.
 
845
        items = CastInfo.select(CastInfo.q.personRoleID == characterID)
 
846
        if results > 0:
 
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')]
 
851
        fdata = []
 
852
        for f in filmodata:
 
853
            curRole = None
 
854
            curRoleID = f[1]
 
855
            note = f[2] or u''
 
856
            if curRoleID is not None:
 
857
                robj = Name.get(curRoleID)
 
858
                curRole = robj.name
 
859
            m = Movie(movieID=f[0], data=f[3],
 
860
                        currentRole=curRole or u'',
 
861
                        roleID=curRoleID, roleIsPerson=True,
 
862
                        notes=note, accessSystem='sql')
 
863
            fdata.append(m)
 
864
        fdata = merge_roles(fdata)
 
865
        fdata.sort()
 
866
        if fdata:
 
867
            res['filmography'] = fdata
 
868
        return {'data': res, 'info sets': infosets}
 
869
 
744
870
    get_character_filmography = get_character_main
745
871
    get_character_biography = get_character_main
746
872