diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-12-31 14:34:47 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-12-31 18:24:34 +0100 |
commit | 843c3336314f942bb7139a2c7da7625f98db46ac (patch) | |
tree | 32a1d87b6b42e8a7535a1539e5e73f3eda3a90a4 | |
download | renfe-843c3336314f942bb7139a2c7da7625f98db46ac.tar.gz renfe-843c3336314f942bb7139a2c7da7625f98db46ac.tar.bz2 renfe-843c3336314f942bb7139a2c7da7625f98db46ac.zip |
Initial commit
-rw-r--r-- | .envrc | 1 | ||||
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | flake.lock | 27 | ||||
-rw-r--r-- | flake.nix | 36 | ||||
-rw-r--r-- | main.py | 88 |
5 files changed, 155 insertions, 0 deletions
diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a23f8a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env + +.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..26ddc95 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1735554305, + "narHash": "sha256-zExSA1i/b+1NMRhGGLtNfFGXgLtgo+dcuzHzaWA6w3Q=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0e82ab234249d8eee3e8c91437802b32c74bb3fd", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c72a6fc --- /dev/null +++ b/flake.nix @@ -0,0 +1,36 @@ +{ + description = "Renfe flake"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + }; + + outputs = { self, nixpkgs }: + let + systems = [ "x86_64-darwin" "aarch64-darwin" "x86_64-linux" ]; + createDevShell = system: + let + pkgs = import nixpkgs { system = "${system}"; config.allowUnfree = true; }; + my-python = pkgs.python3; + python-with-my-packages = my-python.withPackages (p: with p; [ + selenium + python-dotenv + ]); + in + pkgs.mkShell { + buildInputs = [ + python-with-my-packages + + # Chrome driver and google-chrome dependencies + pkgs.chromedriver + pkgs.google-chrome + + # Create a script to run google-chrome-stable + (pkgs.writeShellScriptBin "google-chrome" "exec -a $0 ${pkgs.google-chrome}/bin/google-chrome-stable $@") + ]; + }; + in + { + devShell = nixpkgs.lib.genAttrs systems createDevShell; + }; +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..d1e1bff --- /dev/null +++ b/main.py @@ -0,0 +1,88 @@ +from selenium.webdriver.common.by import By +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from time import sleep +import logging +import os +from dotenv import load_dotenv + +def main(): + logging.basicConfig( + format='%(asctime)s %(levelname)-8s %(message)s', + level=logging.INFO, + datefmt='%Y-%m-%d %H:%M:%S' + ) + + load_dotenv() + + test_ua = 'Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36' + + options = Options() + + # options.add_argument("--headless") # Remove this if you want to see the browser (Headless makes the chromedriver not have a GUI) + # options.add_argument("--window-size=1920,1080") + + options.add_argument(f'--user-agent={test_ua}') + + options.add_argument('--no-sandbox') + options.add_argument("--disable-extensions") + + driver = webdriver.Chrome(options=options) + + driver.get('https://venta.renfe.com/vol/loginParticular.do') + + sleep(5) + + driver.find_element(By.ID, "num_tarjeta").send_keys(os.getenv('RENFE_EMAIL')) + driver.find_element(By.ID, "pass-login").send_keys(os.getenv('RENFE_PASSWORD')) + driver.find_element(By.ID, "loginButtonId").click() + + sleep(15) + + driver.get("https://venta.renfe.com/vol/myPassesCard.do") + + sleep(5) + + element = driver.find_element(By.XPATH, "//a[contains(@onclick, 'submitNew')]") + driver.execute_script("arguments[0].scrollIntoView(true);", element) + sleep(2) + driver.execute_script("arguments[0].click();", element) + + sleep(5) + + #todo: select radio button ida o vuelta + + datepicker = driver.find_element(By.ID, "fecha1") + driver.execute_script(f"arguments[0].value = '06/01/2025';", datepicker) + + element = driver.find_element(By.ID, "submitSiguiente") + driver.execute_script("arguments[0].scrollIntoView(true);", element) + sleep(2) + driver.execute_script("arguments[0].click();", element) + + sleep(5) + + select_button = None + while True: + try: + row = driver.find_element(By.XPATH, f"//tr[td[contains(text(), '18.50')]]") + select_button = row.find_element(By.XPATH, ".//button[contains(@id, 'continuar')]") + break + except Exception: + logging.info("No available places - refreshing...") + driver.refresh() + sleep(15) + continue + driver.execute_script("arguments[0].scrollIntoView(true);", select_button) + sleep(2) + driver.execute_script("arguments[0].click();", select_button) + + sleep(5) + + element = driver.find_element(By.ID, "submitSiguiente") + driver.execute_script("arguments[0].click();", element) + + logging.info("TICKET BOUGHT!!") + +if __name__ == "__main__": + main() |