Finalizado la primera version del wrapper de Mysql. Empezado a trabajar en el cuerpo del explorer
This commit is contained in:
parent
0278ea68a0
commit
b1b7de13f8
6 changed files with 125 additions and 15 deletions
2
__init__.py
Normal file
2
__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
2
core/__init__.py
Normal file
2
core/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
BIN
core/__pycache__/__init__.cpython-36.pyc
Normal file
BIN
core/__pycache__/__init__.cpython-36.pyc
Normal file
Binary file not shown.
BIN
core/__pycache__/mysql_wrapper.cpython-36.pyc
Normal file
BIN
core/__pycache__/mysql_wrapper.cpython-36.pyc
Normal file
Binary file not shown.
|
|
@ -1,32 +1,29 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Contiene las clases necesarias para conectar con la Base de Datos mysql.
|
||||
#
|
||||
# =============================================================================
|
||||
|
||||
import mysql.connector
|
||||
|
||||
anuncios_db_parameters = {'host': '46.183.115.154',
|
||||
'database': 'anuncios',
|
||||
'user': 'pablo',
|
||||
'password': 'noesfacilvivirsinpin'}
|
||||
|
||||
class DatabaseInstance():
|
||||
|
||||
class DatabaseWrapper():
|
||||
|
||||
def __init__(self, connection_parameters):
|
||||
self.host = connection_parameters['host']
|
||||
self.database = connection_parameters['database']
|
||||
self.user = connection_parameters['user']
|
||||
self.password = connection_parameters['password']
|
||||
|
||||
self.connection = None
|
||||
|
||||
self.ping()
|
||||
|
||||
def connect(self):
|
||||
try:
|
||||
self.connection = mysql.connector.connect(host = self.host,
|
||||
database = self.database,
|
||||
user = self.user,
|
||||
password = self.password)
|
||||
except Error as e:
|
||||
except Exception as e:
|
||||
print("Could not connect to the database.")
|
||||
print(e)
|
||||
|
||||
|
|
@ -40,9 +37,20 @@ class DatabaseInstance():
|
|||
self.connect()
|
||||
self.disconnect()
|
||||
|
||||
def query(self, query, parameters):
|
||||
def query(self, query_statement, query_parameters = None, dictionary = False):
|
||||
self.connect()
|
||||
if self.connection.is_connected():
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute()
|
||||
#SEGUIR AQUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
|
||||
execution_cursor = self.connection.cursor(dictionary = dictionary)
|
||||
execution_cursor.execute(query_statement, query_parameters)
|
||||
self.disconnect()
|
||||
return execution_cursor
|
||||
|
||||
|
||||
def query_dict(self, query_statement, query_parameters = None):
|
||||
return self.query(query_statement, query_parameters, dictionary = True)
|
||||
|
||||
def get_anunciosdb():
|
||||
return DatabaseWrapper(anuncios_db_parameters)
|
||||
|
||||
|
||||
|
||||
98
explorer/explorer.py
Normal file
98
explorer/explorer.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
# -*- 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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue