Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
2.5 kB
2
Indexable
Never
send_email: trimite un email.
get_current_price: "vizitează" site-ul și "citește" prețul acțiunii.
check_price_change: verifică dacă prețul s-a schimbat suficient de mult pentru a trimite un email.
main: rulează tot codul in loop, repetând verificarea la fiecare 5 minute

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, WebDriverException
import smtplib
from email.mime.text import MIMEText
import time

previous_price = None

# Configurare
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
GMAIL_USERNAME = 'your_email@gmail.com'
GMAIL_PASSWORD = 'your_email_password'
CHECK_INTERVAL_SECONDS = 300  # Check every 5 minutes

def send_email(subject, body):
    with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
        server.starttls()
        server.login(GMAIL_USERNAME, GMAIL_PASSWORD)
        msg = MIMEText(body)
        msg['From'] = GMAIL_USERNAME
        msg['To'] = GMAIL_USERNAME
        msg['Subject'] = subject
        server.sendmail(GMAIL_USERNAME, GMAIL_USERNAME, msg.as_string())

def get_current_price():
    try:
        # Set up driver (replace path with driver's path)
        driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
        driver.get("https://primet.arenaxt.ro/")
        driver.implicitly_wait(10)
        
        # Localizam FP unde se afla prin inspect element
        price_element = driver.find_element_by_xpath("//YOUR_XPATH_HERE")
        price = float(price_element.text.replace(",", "."))  # Convert to float for calculations
        return price
    except NoSuchElementException:
        print("Could not find the element. Maybe the website structure has changed.")
    except WebDriverException as e:
        print(f"WebDriver Error: {e}")
    except Exception as e:
        print(f"Error fetching price: {e}")
    finally:
        driver.quit()

def check_price_change(threshold=0.05):
    global previous_price
    current_price = get_current_price()
    if current_price is None:  # If there was an error fetching the price
        return
    
    if previous_price:
        change = (current_price - previous_price) / previous_price
        if abs(change) >= threshold:
            send_email("FP Stock Alert!", f"FP moved by {change*100:.2f}%")
    
    previous_price = current_price

def main():
    while True:
        check_price_change(0.05)
        time.sleep(CHECK_INTERVAL_SECONDS)

if __name__ == "__main__":
    main()