Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import configparser
- import os
- import sqlite3
- import json
- import requests
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.common.exceptions import WebDriverException
- from urllib.request import urlopen
- import time
- import re
- import urllib.parse
- # Cargar variables desde .env
- config = configparser.ConfigParser()
- config.read('config.ini')
- miusuario = config['DATABASE']['USUARIO']
- mipass = config['DATABASE']['PASSWORD']
- mibusqueda = config['DATABASE']['BUSQUEDA_ID']
- gurl = config['DATABASE']['GURL']
- def consultabd(eburl):
- try:
- con = sqlite3.connect('itemalert.db', isolation_level=None)
- cur = con.cursor()
- cur.execute("CREATE TABLE IF NOT EXISTS BD (id INTEGER PRIMARY KEY, EBURL text NOT NULL)")
- cur.execute("SELECT * FROM BD WHERE EBURL=?", (eburl,))
- row = cur.fetchone()
- if row is None:
- cur.execute("INSERT INTO BD (EBURL) VALUES (?)", (eburl,))
- return 0
- return 1
- except Exception as e:
- logWeb(f'Error en consultabd: {str(e)}', 1)
- return 1
- finally:
- con.close()
- def almacenabd():
- try:
- con = sqlite3.connect('itemalert.db', isolation_level=None)
- cur = con.cursor()
- cur.execute("DROP TABLE IF EXISTS BD")
- cur.execute("CREATE TABLE IF NOT EXISTS BD (id INTEGER PRIMARY KEY, EBURL text NOT NULL)")
- response = requests.get(gurl)
- data = response.json()
- array = json.loads(data)
- for item in array:
- cur.execute("INSERT INTO BD (EBURL) VALUES (?)", (str(item[0]),))
- except Exception as e:
- logWeb(f'Error en almacenabd: {str(e)}', 1)
- def publicaciones(html, items, precios, descs, imgs):
- try:
- regex = r'media result resultnew ohtoe m-b".*?>(.*?)<\/div>'
- publicaciones = re.findall(regex, html)
- npubs = int(len(publicaciones)/2)
- for i in range(npubs):
- pub = publicaciones[i]
- desc_regex = r'l l-d".*?>(.*?)<\/span>'
- desc_match = re.findall(desc_regex, pub)
- descs.append(desc_match[0] if desc_match else "")
- img_regex = r'images\/g\/(.*?)\/s-l225.jpg'
- img_match = re.findall(img_regex, pub)
- imgs.append(img_match[0] if img_match else "")
- item_regex = r'"p-r" href=.*?itm\/(.*?)\?'
- item_match = re.findall(item_regex, pub)
- items.append(item_match[0] if item_match else "")
- price_regex = r'Fixed Price @ (.*?) USD|Auction : (.*?) USD'
- price_match = re.findall(price_regex, pub)
- if price_match:
- price = price_match[0][0] or price_match[0][1]
- clean_price = re.sub(r"[(),' ]", "", str(price))
- precios.append(clean_price)
- else:
- precios.append("")
- return npubs
- except Exception as e:
- logWeb('Error en publicaciones: ' + str(e), 1)
- return 0
- def logWeb(_log, _prio=0):
- try:
- _log = urllib.parse.quote_plus(_log)
- url = gurl + "?log=" + _log
- if _prio == 1:
- urlopen(url)
- except Exception as e:
- print(f"Error en logWeb: {str(e)}")
- def crear_driver():
- options = webdriver.ChromeOptions()
- options.add_argument("--no-sandbox")
- options.add_argument("--disable-dev-shm-usage")
- options.add_argument("--disable-gpu")
- options.add_argument("--disable-extensions")
- options.add_argument("--disable-background-networking")
- options.add_argument("--disable-software-rasterizer")
- options.add_argument("--headless")
- return webdriver.Chrome(options=options)
- # Inicio del programa
- logWeb('Iniciando_ItemAlert.py', 1)
- items, precios, descs, imgs = [], [], [], []
- driver = crear_driver()
- def login_y_busqueda():
- try:
- driver.get("https://itemalert.com/login")
- time.sleep(2)
- 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)
- time.sleep(3)
- driver.get("https://itemalert.com/savedsearch")
- time.sleep(2)
- cv = driver.find_element(By.NAME, mibusqueda)
- driver.execute_script("arguments[0].click();", cv)
- time.sleep(1)
- go = driver.find_element(By.NAME, 'go')
- driver.execute_script("arguments[0].click();", go)
- time.sleep(3)
- except Exception as e:
- logWeb(f'Error en login_y_busqueda: {str(e)}', 1)
- almacenabd()
- login_y_busqueda()
- time.sleep(20)
- i = 15
- while True:
- try:
- try:
- html = driver.execute_script("return document.body.innerHTML")
- except WebDriverException as we:
- logWeb(f'Tab crashed, reiniciando driver: {str(we)}', 1)
- driver.quit()
- driver = crear_driver()
- login_y_busqueda()
- time.sleep(20)
- continue
- eterror = driver.find_element(By.XPATH, '//div[@id="error"]')
- errorat = re.sub('<.*?>', '', eterror.get_attribute('innerHTML'))
- errorat = "".join(char for char in errorat if char.isalnum() or char == " ")
- if i >= 15:
- i = 1
- logWeb(errorat, 1)
- else:
- i += 1
- npubs = publicaciones(html, items, precios, descs, imgs)
- for j in range(npubs):
- if j < len(items) and items[j]:
- if consultabd(items[j]) == 0:
- url = gurl + "?item=" + urllib.parse.quote_plus(str(items[j])) + \
- "&pre=" + urllib.parse.quote_plus(str(precios[j] if j < len(precios) else "")) + \
- "&des=" + urllib.parse.quote_plus(str(descs[j] if j < len(descs) else ""))+ \
- "&img=" + urllib.parse.quote_plus(str(imgs[j] if j < len(imgs) else ""))
- urlopen(url)
- driver.execute_script("arguments[0].click();", driver.find_element(By.ID, 'cleardom-btn'))
- items.clear()
- precios.clear()
- descs.clear()
- imgs.clear()
- time.sleep(20)
- except Exception as e:
- logWeb(f'Error en bucle principal: {str(e)}', 1)
- time.sleep(20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement