|
| 1 | +"""Canada OPC collector — scrapes Office of the Privacy Commissioner investigation pages. |
| 2 | +
|
| 3 | +Scrapes two sources: |
| 4 | + 1. Investigations into businesses (PIPEDA): ~164 cases |
| 5 | + 2. Investigations into federal institutions (Privacy Act): ~132 cases |
| 6 | +
|
| 7 | +All content is inline HTML (no PDFs). Pagination uses ?o=d&Page=N&Filter=True. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +from urllib.parse import urljoin |
| 14 | + |
| 15 | +import requests |
| 16 | +from bs4 import BeautifulSoup |
| 17 | + |
| 18 | +from pipeline.collectors.base import BaseCollector |
| 19 | +from pipeline.config import HTTP_TIMEOUT |
| 20 | +from pipeline.models import DiscoveredDoc |
| 21 | + |
| 22 | +log = logging.getLogger(__name__) |
| 23 | + |
| 24 | +_SOURCES = [ |
| 25 | + { |
| 26 | + "name": "businesses", |
| 27 | + "base_url": "https://www.priv.gc.ca/en/opc-actions-and-decisions/investigations/investigations-into-businesses/", |
| 28 | + "base_path": "/en/opc-actions-and-decisions/investigations/investigations-into-businesses/", |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": "federal", |
| 32 | + "base_url": "https://www.priv.gc.ca/en/opc-actions-and-decisions/investigations/investigations-into-federal-institutions/", |
| 33 | + "base_path": "/en/opc-actions-and-decisions/investigations/investigations-into-federal-institutions/", |
| 34 | + }, |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +class CanadaOPCCollector(BaseCollector): |
| 39 | + """Discover OPC investigation reports from both business and federal pages.""" |
| 40 | + |
| 41 | + def discover(self) -> list[DiscoveredDoc]: |
| 42 | + docs: list[DiscoveredDoc] = [] |
| 43 | + seen_urls: set[str] = set() |
| 44 | + |
| 45 | + for source in _SOURCES: |
| 46 | + page_num = 1 |
| 47 | + while True: |
| 48 | + if page_num == 1: |
| 49 | + url = source["base_url"] |
| 50 | + else: |
| 51 | + url = f"{source['base_url']}?o=d&Page={page_num}&Filter=True" |
| 52 | + |
| 53 | + try: |
| 54 | + resp = requests.get(url, headers=self.get_headers(), timeout=HTTP_TIMEOUT) |
| 55 | + resp.raise_for_status() |
| 56 | + except requests.RequestException as e: |
| 57 | + log.error(f"OPC {source['name']} page {page_num} fetch failed: {e}") |
| 58 | + break |
| 59 | + |
| 60 | + soup = BeautifulSoup(resp.text, "lxml") |
| 61 | + page_docs = self._extract_cases(soup, source["base_path"], seen_urls) |
| 62 | + |
| 63 | + if not page_docs: |
| 64 | + break |
| 65 | + |
| 66 | + docs.extend(page_docs) |
| 67 | + log.info(f"OPC {source['name']} page {page_num}: found {len(page_docs)} cases (total: {len(docs)})") |
| 68 | + |
| 69 | + # Check for next page by looking for a Page=N+1 link |
| 70 | + has_next = False |
| 71 | + for a in soup.select(f'a[href*="Page="]'): |
| 72 | + href_val = a.get("href", "") |
| 73 | + if f"Page={page_num + 1}" in href_val and "/en/" in href_val: |
| 74 | + has_next = True |
| 75 | + break |
| 76 | + if not has_next: |
| 77 | + break |
| 78 | + |
| 79 | + page_num += 1 |
| 80 | + if page_num > 25: # Safety limit |
| 81 | + break |
| 82 | + |
| 83 | + log.info(f"OPC discovery complete: {len(docs)} documents") |
| 84 | + return docs |
| 85 | + |
| 86 | + def _extract_cases(self, soup: BeautifulSoup, base_path: str, seen: set[str]) -> list[DiscoveredDoc]: |
| 87 | + """Extract case links from a listing page.""" |
| 88 | + docs: list[DiscoveredDoc] = [] |
| 89 | + |
| 90 | + for link in soup.select("a[href]"): |
| 91 | + href = link.get("href", "").strip() |
| 92 | + if not href: |
| 93 | + continue |
| 94 | + |
| 95 | + # Must be under the investigations path and have a case identifier |
| 96 | + if base_path not in href: |
| 97 | + continue |
| 98 | + |
| 99 | + # Skip the listing page itself |
| 100 | + remainder = href.split(base_path)[-1].strip("/") |
| 101 | + if not remainder: |
| 102 | + continue |
| 103 | + |
| 104 | + # Must have a year/identifier pattern (e.g., 2026/pipeda-2026-001) |
| 105 | + if "/" not in remainder: |
| 106 | + continue |
| 107 | + |
| 108 | + full_url = urljoin("https://www.priv.gc.ca", href) |
| 109 | + if full_url in seen: |
| 110 | + continue |
| 111 | + seen.add(full_url) |
| 112 | + |
| 113 | + title = link.get_text(strip=True) |
| 114 | + if not title or len(title) < 10: |
| 115 | + continue |
| 116 | + |
| 117 | + docs.append(DiscoveredDoc( |
| 118 | + case_title=title, |
| 119 | + source_page_url=full_url, |
| 120 | + document_url=full_url, |
| 121 | + file_type="html", |
| 122 | + )) |
| 123 | + |
| 124 | + return docs |
0 commit comments