File size: 6,166 Bytes
4c4be9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from playwright.sync_api import sync_playwright
import threading
import time
from langdetect import detect
import glob
import csv
import os

from datetime import datetime

def parse_fecha_limite(dia, mes_texto, anio):
    # Construimos un string tipo "24 octubre 2025"
    fecha_str = f"{dia} {mes_texto} {anio}"
    # Parseamos con strptime (en español funciona si tu locale está en es_ES)
    try:
        fecha = datetime.strptime(fecha_str, "%d %B %Y")
        return fecha.strftime("%d/%m/%Y")
    except ValueError:
        # Si falla porque el locale no reconoce el mes, puedes mapearlo manualmente
        meses = {
            "enero": "01", "febrero": "02", "marzo": "03", "abril": "04",
            "mayo": "05", "junio": "06", "julio": "07", "agosto": "08",
            "septiembre": "09", "octubre": "10", "noviembre": "11", "diciembre": "12"
        }
        mes_num = meses.get(mes_texto.lower(), "01")
        return f"{dia}/{mes_num}/{anio}"


def log_fallida(url):
    with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ofertas", f"fallidas.txt"), "a", encoding="utf-8") as f:
        f.write(url + "\n")

def append_to_csv(data, archivo_csv):
    campos = [
        "url",
        "fecha_publicacion",
        "fecha_limite",
        "titulo",
        "empresa",
        "ocupacion",
        "educacion",
        "descripcion",
        "pais",
        "region",
        "duracion_jornada",
        "tipo_contrato"
    ]

    # Crear el archivo con cabecera si no existe
    archivo_nuevo = not os.path.exists(archivo_csv)
    with open(archivo_csv, mode="a", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=campos)
        if archivo_nuevo:
            writer.writeheader()
        writer.writerow(data)

def clean_language(url):
    if url[-2:] == "en":
        url = url[:-2] + "es"
    # print(url)
    return url

def cargar_urls_por_bloques(directorio="links", bloque=1000):
    archivos = sorted(glob.glob(os.path.join(directorio, "*.txt")))
    buffer = []

    for archivo in archivos:
        with open(archivo, "r") as f:
            for linea in f:
                url = linea.strip()
                if url:
                    buffer.append(url)
                    if len(buffer) == bloque:
                        yield buffer
                        buffer = []
    if buffer:
        yield buffer  # Último bloque, aunque tenga menos de 1000

def scrape_offer(url):
    """
    Extrae campos clave de una oferta EURES usando Playwright.
    """
    data = {"url": url}

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context()
        page = context.new_page()
        page.goto(url, wait_until="networkidle")
        time.sleep(1.5)

        def safe_text(selector, by="id"):
            try:
                if by == "id":
                    return page.locator(f"#{selector}").first.inner_text().strip()
                elif by == "class":
                    return page.locator(f".{selector}").first.inner_text().strip()
            except:
                return None

        # Fecha: primero intenta el id normal, si no, prueba el alternativo
        fecha = safe_text("jv-lastModificationDate")
        if not fecha:
            fecha = safe_text("jv-lastModificationDate-no-title")

        data["fecha_publicacion"] = fecha
        data["titulo"] = safe_text("jv-title")
        data["empresa"] = safe_text("jv-details-employer-name")
        data["descripcion"] = safe_text("jv-details-job-description")
        data["pais"] = safe_text("jv-address-country", by="class")
        data["region"] = safe_text("jv-address-region", by="class")
        data["duracion_jornada"] = safe_text("jv-position-schedule-result-0")
        data["tipo_contrato"] = safe_text("jv-position-type-code-result")
        data["ocupacion"] = safe_text("jv-job-categories-codes-result-0")
        data["educacion"] = safe_text("ecl-description-list__definition", by="class")

        try:
            dia = page.locator(".ecl-date-block__day").first.inner_text().strip()
            mes = page.locator(".ecl-date-block__month").first.get_attribute("title").strip()
            anio = page.locator(".ecl-date-block__year").first.inner_text().strip()
            data["fecha_limite"] = parse_fecha_limite(dia, mes, anio)
        except:
            data["fecha_limite"] = None


        browser.close()

    return data


def scrape_batch(urls, batch_size=3, sleep_time=2.5):
    """
    Procesa las URLs en grupos de `batch_size` en paralelo.
    """
    results = []

    def worker(url):
        try:
            result = scrape_offer(url)
            results.append(result)
        except Exception as e:
            print(f"[✗] Falló: {url}{e}")
            log_fallida(url)


    for i in range(0, len(urls), batch_size):
        threads = []
        for url in urls[i:i+batch_size]:
            t = threading.Thread(target=worker, args=(url,))
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
        time.sleep(sleep_time)  # pausa entre lotes para evitar bloqueo

    return results


if __name__ == "__main__":

    inicio = datetime.now()
    script_dir = os.path.dirname(os.path.abspath(__file__))
    timestamp = datetime.now().strftime("%d_%H%M%S")
    links_path = os.path.join(script_dir, "links")
    file_path = os.path.join(script_dir, "ofertas", f"ofertas{timestamp}.csv")
    leidos_path = os.path.join(script_dir, "ofertas", f"leidas.txt")
    print("Starting")
    for i, urls in enumerate(cargar_urls_por_bloques(links_path, 100)):
        print(f"Procesando bloque {i + 1} con {len(urls)} URLs", end="")

        resultados = scrape_batch([clean_language(url) for url in urls], batch_size=50, sleep_time=0.25)
        for data in resultados:
            append_to_csv(data, file_path)
        
        with open(leidos_path, "a") as f:
            f.writelines([url + "\n" for url in urls])
    
    fin = datetime.now()
    duracion = fin - inicio
    print("Finalizado!")
    print(f"Duración en segundos: {duracion.total_seconds()}")