def add_from_whois(cls, domain, just_new=False, mock_from_txt_file=None): """ create or update a domain from WhoIs info Returns None if error or the domain object """ logger.info(f'Adding from WhoIs {domain}') wa = WhoAre() domain_name, zone = wa.detect_zone(domain) zona, _ = Zona.objects.get_or_create(nombre=zone) if just_new: dominio = cls.get_from_full_domain(domain) if dominio is not None and dominio.estado == STATUS_NO_DISPONIBLE: # Already is in the database and will be updated by priority later return True, 'Already exists', None try: wa.load(domain, mock_from_txt_file=mock_from_txt_file) except TooManyQueriesError: return False, 'Too many queries', None except Exception as e: return False, str(e), None # bad domain, don't needed if just_new and wa.domain.is_free: return True, 'Is a free domain', None dominio, dominio_created = Dominio.objects.get_or_create(nombre=wa.domain.base_name, zona=zona) # create a domain after being sure we don't have any whoare errors logger.info(f' - Dominio {dominio} Created: {dominio_created}') # return the changes list return True, None, dominio.update_from_wa_object(wa, just_created=dominio_created)
def get_from_full_domain(cls, full_domain): wa = WhoAre() domain_name, zone = wa.detect_zone(full_domain) zona, _ = Zona.objects.get_or_create(nombre=zone) dominios = Dominio.objects.filter(nombre=domain_name, zona=zona) if dominios.count() > 0: return dominios[0] return None
def save(self, **kwargs): # si ya existe como dominio, omitir wa = WhoAre() domain_name, zone = wa.detect_zone(self.dominio) try: zona = Zona.objects.get(nombre=zone) except: logger.error(f'Bad zone {zone}') self.id = 0 return dominios = Dominio.objects.filter(nombre=domain_name, zona=zona) if dominios.count() > 0: # TODO, este injerto no parece bueno self.id = 0 # TODO si trate de agregarlo como predominio y existe pero esta disponible # es posible que se haya vuelto a registrar. Debería aqui darle mas prioridad # a su actualizacion else: return super().save(**kwargs)
def get_from_predomain(self): nuevos = PreDominio.objects.filter(priority__gt=0) nuevos = nuevos.order_by('-priority', 'dominio')[:100] random_item = random.choice(nuevos) if random_item.priority == 0: # se acabaron return self.get_from_domain() random_item.priority = 0 random_item.save() # si ya existe en dominios, omitir wa = WhoAre() domain_name, zone = wa.detect_zone(random_item.dominio) zona = Zona.objects.get(nombre=zone) dominios = Dominio.objects.filter(nombre=domain_name, zona=zona) if dominios.count() > 0: return self.get_from_domain() self.serializer_class = FlatPreDominioSerializer res = PreDominio.objects.filter(pk=random_item.id) return res