Exemple #1
0
    def run(self, person_id, save_to_db=False,  **kwargs):
        from utils.helper import translate_with_wiki
        from persons.models import Actor
        from persons.views import save_mugshot
        info = {}
        
        person, ok = do_long_imdb_operation(getperson_by_id, args=person_id, timeout=4)
        if ok:
            
            print person.keys()
            try: info['name'] = person['name'].replace(' - IMDb','')
            except KeyError: pass

            try: info['bio'] = person['mini biography'][0]
            except KeyError: pass

            try: info['headshot'] = person['headshot']
            except KeyError: pass

            try: info['imdb_url'] = "http://www.imdb.com/name/nm"+person_id+"/"
            except KeyError: pass

            try :  info['birth_date'] = person.data['birth date']
            except KeyError: pass
           
            if save_to_db:
                
                act, created = Actor.objects.get_or_create(name_en=info['name'] )

                if 'bio' in info.keys():  
                    act.bio_en = unicode(info['bio'])

                he_name =  translate_with_wiki('he',info['name'])
                if he_name is not None:
                    act.name_he = he_name.decode('unicode_escape')

                ru_name =  translate_with_wiki('ru',info['name'])
                if ru_name is not None:
                    act.name_ru = ru_name.decode('unicode_escape')

                act.imdb_url = unicode(info['imdb_url'])

                if 'headshot' in info.keys(): 
                    save_mugshot(act, info['headshot'] )

                act.save()
        else:
            info['error'] = True

        return info
Exemple #2
0
def edb_translate_name(request, format, name):
    '''
    translate the actor name into hebrew
    '''
    if request.is_ajax():
        info = {}
        '''
        # set the socket timeout to 1sec
        import socket 
        socket.setdefaulttimeout(1)
        # max retrys on this request
        MAX_RETRY = 3 
        #regex to catch the hebrew name
        regex =r'<div id="title_container">.*?<h1><a.*?>(?P<name>.*?[^<])</a>.*?</div>'
        url="http://www.edb.co.il/find.php?%s"

        params = urllib.urlencode({'s': name.encode('utf-8') })
        logger.debug(url % params) 
        
        fetch = True
        retries = 0
        data = ''
        while fetch:
            if retries >= MAX_RETRY: break
            try: 
                retries += 1
                f = urllib.urlopen(url % params )
                data = f.read()
                fetch = False
            except socket.timeout: pass
            except IOError: pass
        
        #reset 
        socket.setdefaulttimeout(None)
        
        found = re.search(regex, data, re.UNICODE | re.DOTALL)
        if found is not None:
            info['he_name'] = found.group("name")
        else:
            info['error'] = True
        '''
        he_name = translate_with_wiki('he', name)
        if he_name is not None:
            info['he_name'] = he_name.decode('unicode_escape')
        else: 
            info['error'] = True
        if format == 'xml':
            mimetype = 'application/xml'
            #TODO xml serialize
            data = 'Not implemented'
        if format == 'json':
            mimetype = 'application/javascript'
            data = json.dumps(info)

        return HttpResponse(data ,mimetype)
    # If you want to prevent non XHR calls    
    else:
        return HttpResponse(status=400)