|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +''' |
| 4 | +Estrae le tesi da archivio UniPi filtrate per relatore e genera: |
| 5 | +- assets/theses_pollacci.json |
| 6 | +- assets/theses_pollacci.html (frammento HTML) |
| 7 | +Pensato per essere eseguito da GitHub Actions. |
| 8 | +''' |
| 9 | +import json, time, os, sys |
| 10 | +from urllib.parse import urljoin, urlencode, urlparse |
| 11 | +import requests |
| 12 | +from bs4 import BeautifulSoup |
| 13 | + |
| 14 | +BASE = "https://etd.adm.unipi.it/ETD-db/ETD-search/search_by_advisor" |
| 15 | +ADVISOR = os.getenv("ETD_ADVISOR", "Pollacci") # cambialo se serve |
| 16 | +RATE_DELAY = float(os.getenv("RATE_DELAY", "0.6")) |
| 17 | +UA = "thesis-list-updater/1.0 (+contact: youremail@example.com)" |
| 18 | + |
| 19 | +session = requests.Session() |
| 20 | +session.headers.update({"User-Agent": UA, "Accept-Language": "it,en;q=0.8"}) |
| 21 | + |
| 22 | +def build_url(advisor, extra=None): |
| 23 | + qs = {"advisor_name": advisor} |
| 24 | + if extra: |
| 25 | + qs.update(extra) |
| 26 | + return f"{BASE}?{urlencode(qs)}" |
| 27 | + |
| 28 | +def fetch(url): |
| 29 | + r = session.get(url, timeout=30) |
| 30 | + r.raise_for_status() |
| 31 | + return BeautifulSoup(r.text, "html.parser"), r.url |
| 32 | + |
| 33 | +def parse_rows(soup): |
| 34 | + """ |
| 35 | + Cerca una tabella risultati e ne estrae le righe. È scritto in modo 'resiliente' |
| 36 | + rispetto a piccoli cambi di markup. |
| 37 | + """ |
| 38 | + rows = [] |
| 39 | + table = soup.find("table") |
| 40 | + if not table: |
| 41 | + return rows |
| 42 | + # salta l'header |
| 43 | + for tr in table.select("tr")[1:]: |
| 44 | + tds = tr.find_all("td") |
| 45 | + if len(tds) < 2: |
| 46 | + continue |
| 47 | + author = tds[0].get_text(" ", strip=True) |
| 48 | + title = tds[1].get_text(" ", strip=True) |
| 49 | + degree = tds[2].get_text(" ", strip=True) if len(tds) > 2 else "" |
| 50 | + committee = tds[3].get_text(" ", strip=True) if len(tds) > 3 else "" |
| 51 | + link = None |
| 52 | + a = tr.find("a", href=True) |
| 53 | + if a: |
| 54 | + link = urljoin(BASE, a["href"]) |
| 55 | + rows.append({ |
| 56 | + "author": author, |
| 57 | + "title": title, |
| 58 | + "degree": degree, |
| 59 | + "committee": committee, |
| 60 | + "url": link |
| 61 | + }) |
| 62 | + return rows |
| 63 | + |
| 64 | +def find_next(soup): |
| 65 | + """ |
| 66 | + Trova il link 'successiva/next' o un link di paginazione compatibile. |
| 67 | + """ |
| 68 | + # 1) Cerca ancore con testo tipico |
| 69 | + for a in soup.find_all("a", href=True): |
| 70 | + label = a.get_text(" ", strip=True).lower() |
| 71 | + if label in {"successiva", "next", ">", "»", ">>"}: |
| 72 | + href = a["href"] |
| 73 | + if "search_by_advisor" in href: |
| 74 | + return urljoin(BASE, href) |
| 75 | + # 2) Fallback: qualunque link allo stesso endpoint con parametri di pagina |
| 76 | + for a in soup.find_all("a", href=True): |
| 77 | + href = a["href"] |
| 78 | + if ("search_by_advisor" in href and "advisor_name=" in href |
| 79 | + and any(p in href for p in ("offset", "start", "from", "page", "first"))): |
| 80 | + return urljoin(BASE, href) |
| 81 | + return None |
| 82 | + |
| 83 | +def ensure_dirs(): |
| 84 | + os.makedirs("assets", exist_ok=True) |
| 85 | + |
| 86 | +def render_html(items): |
| 87 | + out = [] |
| 88 | + out.append("<ul class=\"theses-list\">") |
| 89 | + for r in items: |
| 90 | + li = f"<li><strong>{r['author']}</strong> — «{r['title']}»" |
| 91 | + if r.get("degree"): |
| 92 | + li += f" <em>({r['degree']})</em>" |
| 93 | + if r.get("url"): |
| 94 | + li += f" — <a href=\"{r['url']}\" target=\"_blank\" rel=\"noopener\">scheda</a>" |
| 95 | + li += "</li>" |
| 96 | + out.append(li) |
| 97 | + out.append("</ul>") |
| 98 | + return "\n".join(out) |
| 99 | + |
| 100 | +def main(): |
| 101 | + ensure_dirs() |
| 102 | + url = build_url(ADVISOR) |
| 103 | + seen = set() |
| 104 | + items = [] |
| 105 | + |
| 106 | + while url and url not in seen: |
| 107 | + seen.add(url) |
| 108 | + soup, final_url = fetch(url) |
| 109 | + items.extend(parse_rows(soup)) |
| 110 | + nxt = find_next(soup) |
| 111 | + url = nxt |
| 112 | + time.sleep(RATE_DELAY) |
| 113 | + if not url: |
| 114 | + break |
| 115 | + |
| 116 | + # ordina alfabeticamente per autore (puoi cambiare qui il criterio) |
| 117 | + items.sort(key=lambda x: (x.get("author","").lower(), x.get("title","").lower())) |
| 118 | + |
| 119 | + with open("assets/theses_pollacci.json", "w", encoding="utf-8") as f: |
| 120 | + json.dump(items, f, ensure_ascii=False, indent=2) |
| 121 | + |
| 122 | + with open("assets/theses_pollacci.html", "w", encoding="utf-8") as f: |
| 123 | + f.write(render_html(items)) |
| 124 | + |
| 125 | + print(f"Scritti {len(items)} record.") |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + sys.exit(main()) |
0 commit comments