100 lines
3.3 KiB
Python
100 lines
3.3 KiB
Python
from core.mysql_wrapper import get_anunciosdb
|
|
|
|
|
|
class CapturasInterface():
|
|
|
|
def __init__(self):
|
|
|
|
self.anunciosdb = get_anunciosdb()
|
|
|
|
def insert_captura(self, ad_data):
|
|
|
|
columns = ', '.join(ad_data.keys())
|
|
placeholders_string = ', '.join(['%s'] * len(ad_data))
|
|
|
|
query_statement = """ INSERT INTO capturas
|
|
( fecha_captura, {} )
|
|
VALUES( NOW(), {} )""".format(columns, placeholders_string)
|
|
|
|
query_parameters = tuple([v for v in ad_data.values()])
|
|
|
|
self.anunciosdb.query(query_statement, query_parameters)
|
|
|
|
def old_ads_exist(self):
|
|
query_statement = """
|
|
SELECT uc.referencia
|
|
FROM anuncios.ultima_captura_full as uc
|
|
LEFT JOIN (SELECT cl.ad_url as ad_url
|
|
FROM tasks.capturing_last as cl
|
|
WHERE cl.status = 'Dead ad') as da
|
|
ON da.ad_url LIKE CONCAT('%', uc.referencia, '%')
|
|
WHERE uc.fecha_captura BETWEEN (NOW() - INTERVAL 90 day) AND (NOW() - INTERVAL 10 day)
|
|
AND da.ad_url is null
|
|
"""
|
|
|
|
cursor_result = self.anunciosdb.query(query_statement)
|
|
resultados = cursor_result.fetchall()
|
|
if resultados:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def get_old_ad(self):
|
|
query_statement = """
|
|
SELECT uc.referencia
|
|
FROM anuncios.ultima_captura_full as uc
|
|
LEFT JOIN (SELECT cl.ad_url as ad_url
|
|
FROM tasks.capturing_last as cl
|
|
WHERE cl.status = 'Dead ad') as da
|
|
ON da.ad_url LIKE CONCAT('%', uc.referencia, '%')
|
|
WHERE uc.fecha_captura BETWEEN (NOW() - INTERVAL 90 day) AND (NOW() - INTERVAL 10 day)
|
|
AND da.ad_url is null
|
|
ORDER BY RAND()
|
|
LIMIT 1
|
|
"""
|
|
cursor_result = self.anunciosdb.query(query_statement, dictionary=True)
|
|
try:
|
|
return cursor_result.fetchone()
|
|
except:
|
|
return None
|
|
|
|
def not_geocoded_captura_exists(self):
|
|
query_statement = """
|
|
SELECT COUNT(referencia) as cantidad
|
|
FROM anuncios.capturas
|
|
WHERE `precision` IS NULL"""
|
|
|
|
cursor_result = self.anunciosdb.query(query_statement)
|
|
result = cursor_result.fetchone()[0]
|
|
|
|
return result > 0
|
|
|
|
|
|
def get_not_geocoded_captura(self):
|
|
query_statement = """
|
|
SELECT *
|
|
FROM anuncios.capturas
|
|
WHERE `precision` IS NULL
|
|
LIMIT 1
|
|
"""
|
|
cursor_result = self.anunciosdb.query(query_statement, dictionary=True)
|
|
return cursor_result.fetchone()
|
|
|
|
def update_geo_data(self, referencia, fecha_captura, latitude, longitude, precision):
|
|
query_statement = """
|
|
UPDATE anuncios.capturas
|
|
SET latitud = %(latitud)s, longitud = %(longitud)s, `precision` = %(precision)s
|
|
WHERE referencia = %(referencia)s AND fecha_captura = %(fecha_captura)s
|
|
"""
|
|
query_parameters = {'referencia': referencia,
|
|
'fecha_captura': fecha_captura,
|
|
'latitud': latitude,
|
|
'longitud': longitude,
|
|
'precision': precision}
|
|
|
|
self.anunciosdb.query(query_statement, query_parameters)
|
|
|
|
|
|
capturas_interface = CapturasInterface()
|
|
|
|
|