Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ItemAlert v4
- #itemalert4.py
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from datetime import datetime
- from urllib.parse import quote
- from urllib.request import urlopen
- import time
- import re
- import configparser
- import logging
- import sys
- def consultabd(eburl):
- import sqlite3
- con = sqlite3.connect('itemalert.db', isolation_level = None)
- cur = con.cursor()
- res = cur.execute("CREATE TABLE IF NOT EXISTS BD (id INTEGER PRIMARY KEY,EBURL text NOT NULL)")
- res = cur.execute("SELECT * FROM BD WHERE EBURL='" + eburl + "'")
- row = res.fetchone()
- if row is None:
- cur.execute("INSERT INTO BD (EBURL) VALUES ('"+ eburl +"')")
- return 0
- else:
- return 1
- con.close()
- def enviadatos(micodigo,midispositivo,mensaje,desc,eburl,precio,imagen):
- from urllib.request import urlopen
- import urllib.parse
- import logging
- import sys
- try:
- url = "https://script.google.com/macros/s/" + micodigo + "/exec?disp=" + str(midispositivo) + "&mensaje=" + mensaje + "&desc=" + urllib.parse.quote(desc) + "&eburl=" + eburl + "&precio=" + urllib.parse.quote(precio) + "&imagen=" + imagen
- print(url)
- req = urlopen(url)
- except Exception:
- e = sys.exc_info()[1]
- print (e)
- pass
- logging.basicConfig(filename='ia.log', filemode='a', level=logging.INFO, format='%(asctime)s :: %(levelname)s :: %(message)s')
- logging.info("Iniciando ...")
- print ("Iniciando ...")
- logging.info("Consultandeo configuracion local ...")
- print ("Consultandeo configuracion local ...")
- config = configparser.ConfigParser()
- config.read('config.ini')
- midispositivo = config['DISPOSITIVO']['ID']
- url = config['GSHEET']['URL']
- logging.info("Consultandeo configuracion online ...")
- print ("Consultandeo configuracion online ...")
- with urllib.request.urlopen(url) as response:
- html = response.read().decode('utf-8')
- regex = r"---.*?--(.*?)--.*?---"
- r1 = re.findall(regex, html)
- miusuario = r1[3]
- mipass = r1[4]
- mibusqueda = r1[5]
- micodigo = r1[6]
- options = webdriver.ChromeOptions()
- options.add_argument("--no-sandbox")
- options.add_argument("--disable-dev-shm-usage")
- options.add_argument("--headless=new")
- driver = webdriver.Chrome(options=options)
- driver.get("https://itemalert.com/login")
- usuario = driver.find_element(By.NAME, 'username')
- pswrd = driver.find_element(By.NAME, 'password')
- usuario.send_keys(miusuario)
- pswrd.send_keys(mipass)
- pswrd.send_keys(Keys.RETURN)
- logging.info("Logeado en itemalert")
- driver.get("https://itemalert.com/savedsearch")
- cv = driver.find_element(By.NAME, mibusqueda)
- cv.click()
- go = driver.find_element(By.NAME, 'go')
- driver.execute_script("arguments[0].click();", go)
- #go.click()
- email = driver.find_element(By.XPATH,'//p[@data-original-title="new result email"]')
- email.click()
- eterror = driver.find_element(By.XPATH,'//div[@id="error"]')
- errorat = eterror.get_attribute('innerHTML')
- logging.info("Monitoreo iniciado, mensaje: " + errorat)
- j = 0
- while True:
- symbols = str(datetime.now())
- nomimg = "captura-" + "".join(symbols) + ".png"
- nomimg = nomimg.replace(":","")
- nomimg = nomimg.replace(" ","")
- eterror = driver.find_element(By.XPATH,'//div[@id="error"]')
- errorat = eterror.get_attribute('innerHTML')
- errorat = re.sub(r'<[^>]*?>', '', errorat)
- errorat = errorat.replace(" ","")
- errorat = quote(errorat)
- logging.info("Error: " + errorat)
- if(errorat == "NoeBayErrors%21"):
- if(j == 15):
- j = 1
- print("Enviando aviso de funcionamiento")
- enviadatos(micodigo,midispositivo,errorat,"","","","")
- else:
- j += 1
- print ("ItemAlert operando sin errores: " + str(j) + " de 15")
- html = driver.execute_script("return document.body.innerHTML")
- regex = 'l l-d".*?>(.*?)<\/span>'#Descripciones
- descs = re.findall(regex, html)
- regex = '"p-r" href=.*?itm\/(.*?)\?'#Clave ebay
- eburls = re.findall(regex, html)
- regex = 'Fixed Price @ (.*?) USD|Auction : (.*?) USD'#Precio
- precios = re.findall(regex, html)
- regex = 'images\/g\/(.*?)\/s-l225.jpg'#Imagen
- imagenes = re.findall(regex, html)
- length = len(descs)
- logging.info("Registros encontrados: " + str(length))
- for i in range(length//2):
- desc = descs[i]
- eburl = eburls[i]
- precio = precios[i][0] + precios[i][1]
- imagen = imagenes[i]
- nregs = consultabd(eburl)
- if nregs == 0:
- print ("Item " + eburl + " NO encontrado en BD")
- enviadatos(micodigo,midispositivo,"Nuevo_item_encontrado:_" + eburl,desc,eburl,precio,imagen)
- time.sleep(5)
- else:
- print ("Item " + eburl + " encontrado en BD")
- time.sleep(20)
- else:
- logging.info("NO Entro al if error, error:" + errorat)
- print("NO Entro al if error")
- enviadatos(micodigo,midispositivo,errorat,"","","","")
- time.sleep(300)
- driver.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement