98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import sys
|
||
|
|
sys.path.append('..')
|
||
|
|
from time import sleep
|
||
|
|
|
||
|
|
from core.mysql_wrapper import get_anunciosdb
|
||
|
|
|
||
|
|
class Explorer():
|
||
|
|
|
||
|
|
sleep_time_no_work = 60
|
||
|
|
sleep_time_no_service = 600
|
||
|
|
|
||
|
|
|
||
|
|
def __init__(self):
|
||
|
|
try:
|
||
|
|
self.anunciosdb = get_anunciosdb()
|
||
|
|
except:
|
||
|
|
print("Could not connect to DB")
|
||
|
|
|
||
|
|
self.max_db_retries = 3
|
||
|
|
self.db_retries = 0
|
||
|
|
self.max_queue_retries = 3
|
||
|
|
self.queue_retries = 0
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def start(self):
|
||
|
|
#Arrancar el servicio
|
||
|
|
|
||
|
|
while True:
|
||
|
|
if not self.there_is_work():
|
||
|
|
sleep(sleep_time_no_work)
|
||
|
|
continue
|
||
|
|
|
||
|
|
if not self.database_is_up():
|
||
|
|
break
|
||
|
|
|
||
|
|
if not self.queue_is_up():
|
||
|
|
break
|
||
|
|
|
||
|
|
self.compose_listing_url
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
self.stop()
|
||
|
|
|
||
|
|
|
||
|
|
def stop(self):
|
||
|
|
#Detener el servicio
|
||
|
|
|
||
|
|
def there_is_work(self):
|
||
|
|
#Comprueba si hay trabajo por hacer
|
||
|
|
|
||
|
|
def database_is_up(self):
|
||
|
|
while self.db_retries <= self.max_db_retries:
|
||
|
|
try:
|
||
|
|
self.anunciosdb.ping()
|
||
|
|
self.db_retries = 0
|
||
|
|
return True
|
||
|
|
except:
|
||
|
|
sleep(sleep_time_no_service)
|
||
|
|
self.db_retries = self.db_retries + 1
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
def queue_is_up(self):
|
||
|
|
#WIP
|
||
|
|
while self.queue_retries <= self.max_queue_retries:
|
||
|
|
try:
|
||
|
|
#codigo que testea si redis esta vivo
|
||
|
|
self.queue_retries = 0
|
||
|
|
return True
|
||
|
|
except:
|
||
|
|
sleep(sleep_time_no_service)
|
||
|
|
self.queue_retries = self.queue_retries + 1
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
def compose_listing_url(self):
|
||
|
|
#Decide que url hay que componer y la compone
|
||
|
|
|
||
|
|
raiz, orden = componer_raiz(p_orden, tipo_anuncios, ciudad)
|
||
|
|
cantidad = calcular_cantidad_listado(anuncios_por_capturar)
|
||
|
|
|
||
|
|
lista_urls = []
|
||
|
|
|
||
|
|
for num in range(primera_pagina, cantidad + primera_pagina):
|
||
|
|
lista_urls.append(raiz + 'pagina-' + str(num) + '.htm' + orde$
|
||
|
|
|
||
|
|
|
||
|
|
return lista_urls
|
||
|
|
|
||
|
|
#funcion que compone los strings de url de idealista
|
||
|
|
#tiene en cuenta cuantos anuncios quedan por capturar
|
||
|
|
#para decidir la cantidad de URLs a generar
|
||
|
|
|