|
| 1 | +"""UFC Que Choisir price scraper - Fetches tariffs from UFC Que Choisir partnership with Octopus Energy |
| 2 | +
|
| 3 | +This scraper fetches the "Energie Moins Chère Ensemble" (EMCE) offer from UFC Que Choisir, |
| 4 | +which is powered by Octopus Energy. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import List |
| 8 | +import httpx |
| 9 | +import re |
| 10 | +from io import BytesIO |
| 11 | +from pdfminer.high_level import extract_text |
| 12 | +from datetime import datetime, UTC |
| 13 | + |
| 14 | +from .base import BasePriceScraper, OfferData, run_sync_in_thread |
| 15 | + |
| 16 | + |
| 17 | +def _extract_pdf_text(content: bytes) -> str: |
| 18 | + """Extract text from PDF content (runs in thread pool)""" |
| 19 | + return extract_text(BytesIO(content)) |
| 20 | + |
| 21 | + |
| 22 | +class UFCQueChoisirScraper(BasePriceScraper): |
| 23 | + """Scraper for UFC Que Choisir offers (Energie Moins Chère Ensemble)""" |
| 24 | + |
| 25 | + # UFC Que Choisir / Octopus Energy EMCE pricing PDF URL |
| 26 | + TARIFF_PDF_URL = "https://a.storyblok.com/f/151412/x/60a52916f7/grille-tarifaire-emce-2025.pdf" |
| 27 | + |
| 28 | + # Fallback: Manual pricing data TTC (updated 2025-12-05 from PDF) |
| 29 | + # Source: Grille tarifaire EMCE 2025 - Applicable au 30/10/2025 |
| 30 | + # Offer: Energie Moins Chère Ensemble 2025 (100% verte via Octopus Energy) |
| 31 | + FALLBACK_PRICES = { |
| 32 | + "EMCE_BASE": { |
| 33 | + # All 36 power levels from the PDF |
| 34 | + # Format: power_kva: {"subscription": monthly_ttc, "kwh": ttc} |
| 35 | + # kWh TTC: 0.1616 €/kWh (same for all powers) |
| 36 | + 3: {"subscription": 11.72, "kwh": 0.1616}, |
| 37 | + 6: {"subscription": 15.45, "kwh": 0.1616}, |
| 38 | + 9: {"subscription": 19.38, "kwh": 0.1616}, |
| 39 | + 12: {"subscription": 23.30, "kwh": 0.1616}, |
| 40 | + 15: {"subscription": 27.04, "kwh": 0.1616}, |
| 41 | + 18: {"subscription": 30.74, "kwh": 0.1616}, |
| 42 | + 24: {"subscription": 38.75, "kwh": 0.1616}, |
| 43 | + 30: {"subscription": 46.40, "kwh": 0.1616}, |
| 44 | + 36: {"subscription": 55.00, "kwh": 0.1616}, |
| 45 | + }, |
| 46 | + "EMCE_HC_HP": { |
| 47 | + # HC/HP available from 6 kVA |
| 48 | + # HP TTC: 0.1717 €/kWh, HC TTC: 0.1365 €/kWh |
| 49 | + 6: {"subscription": 15.73, "hp": 0.1717, "hc": 0.1365}, |
| 50 | + 9: {"subscription": 20.19, "hp": 0.1717, "hc": 0.1365}, |
| 51 | + 12: {"subscription": 24.26, "hp": 0.1717, "hc": 0.1365}, |
| 52 | + 15: {"subscription": 28.13, "hp": 0.1717, "hc": 0.1365}, |
| 53 | + 18: {"subscription": 32.11, "hp": 0.1717, "hc": 0.1365}, |
| 54 | + 24: {"subscription": 40.50, "hp": 0.1717, "hc": 0.1365}, |
| 55 | + 30: {"subscription": 48.30, "hp": 0.1717, "hc": 0.1365}, |
| 56 | + 36: {"subscription": 54.57, "hp": 0.1717, "hc": 0.1365}, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + def __init__(self, scraper_urls: list[str] | None = None): |
| 61 | + super().__init__("UFC Que Choisir") |
| 62 | + # Use URLs from database if provided, otherwise use default |
| 63 | + self.scraper_urls = scraper_urls or [self.TARIFF_PDF_URL] |
| 64 | + |
| 65 | + async def fetch_offers(self) -> List[OfferData]: |
| 66 | + """ |
| 67 | + Fetch UFC Que Choisir EMCE tariffs - Download and parse PDF, fallback to manual data if needed |
| 68 | +
|
| 69 | + Returns: |
| 70 | + List[OfferData]: List of UFC Que Choisir offers |
| 71 | + """ |
| 72 | + errors = [] |
| 73 | + |
| 74 | + try: |
| 75 | + # Download PDF |
| 76 | + pdf_url = self.scraper_urls[0] if self.scraper_urls else self.TARIFF_PDF_URL |
| 77 | + async with httpx.AsyncClient(timeout=30.0, follow_redirects=True) as client: |
| 78 | + response = await client.get(pdf_url) |
| 79 | + if response.status_code != 200: |
| 80 | + error_msg = f"Échec du téléchargement du PDF UFC Que Choisir (HTTP {response.status_code})" |
| 81 | + self.logger.warning(error_msg) |
| 82 | + errors.append(error_msg) |
| 83 | + else: |
| 84 | + # Parse PDF in thread pool to avoid blocking event loop |
| 85 | + text = await run_sync_in_thread(_extract_pdf_text, response.content) |
| 86 | + offers = self._parse_pdf(text) |
| 87 | + |
| 88 | + if not offers: |
| 89 | + error_msg = "Échec du parsing du PDF UFC Que Choisir - aucune offre extraite" |
| 90 | + self.logger.warning(error_msg) |
| 91 | + errors.append(error_msg) |
| 92 | + else: |
| 93 | + self.logger.info(f"Successfully scraped {len(offers)} UFC Que Choisir offers from PDF") |
| 94 | + return offers |
| 95 | + except Exception as e: |
| 96 | + error_msg = f"Erreur lors du scraping du PDF UFC Que Choisir : {str(e)}" |
| 97 | + self.logger.warning(error_msg) |
| 98 | + errors.append(error_msg) |
| 99 | + |
| 100 | + # Use fallback data if PDF parsing failed |
| 101 | + if errors: |
| 102 | + self.logger.info(f"Using fallback data for UFC Que Choisir due to errors: {' | '.join(errors)}") |
| 103 | + fallback_offers = self._get_fallback_offers() |
| 104 | + if fallback_offers: |
| 105 | + self.used_fallback = True |
| 106 | + self.fallback_reason = " | ".join(errors) |
| 107 | + self.logger.info(f"Successfully loaded {len(fallback_offers)} UFC Que Choisir offers from fallback data") |
| 108 | + return fallback_offers |
| 109 | + else: |
| 110 | + raise Exception(f"Échec complet du scraping UFC Que Choisir (y compris fallback) : {' | '.join(errors)}") |
| 111 | + |
| 112 | + raise Exception("Échec du scraping UFC Que Choisir - raison inconnue") |
| 113 | + |
| 114 | + def _parse_pdf(self, text: str) -> List[OfferData]: |
| 115 | + """ |
| 116 | + Parse PDF text from UFC Que Choisir EMCE tariff sheet. |
| 117 | +
|
| 118 | + The PDF structure (as of 2025) contains: |
| 119 | + - BASE option: subscription prices per kVA (1-36) + single kWh price (0.1616 TTC) |
| 120 | + - HC/HP option: subscription prices per kVA (1-36) + HP (0.1717) and HC (0.1365) prices |
| 121 | +
|
| 122 | + Important: The PDF includes power levels from 1-36 kVA, but we only use standard |
| 123 | + residential powers: 3, 6, 9, 12, 15, 18, 24, 30, 36 kVA. |
| 124 | + """ |
| 125 | + offers = [] |
| 126 | + valid_from = datetime.now(UTC).replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
| 127 | + |
| 128 | + try: |
| 129 | + # Extract BASE prices |
| 130 | + base_prices = self._extract_base_prices(text) |
| 131 | + if base_prices: |
| 132 | + for power, prices in base_prices.items(): |
| 133 | + offers.append( |
| 134 | + OfferData( |
| 135 | + name=f"EMCE 2025 - Base {power} kVA", |
| 136 | + offer_type="BASE", |
| 137 | + description=f"Energie Moins Chère Ensemble 2025 - Électricité 100% verte via Octopus Energy - {power} kVA", |
| 138 | + subscription_price=prices["subscription"], |
| 139 | + base_price=prices["kwh"], |
| 140 | + power_kva=power, |
| 141 | + valid_from=valid_from, |
| 142 | + ) |
| 143 | + ) |
| 144 | + self.logger.info(f"Extracted {len(base_prices)} BASE offers from UFC PDF") |
| 145 | + |
| 146 | + # Extract HC/HP prices |
| 147 | + hc_hp_prices = self._extract_hc_hp_prices(text) |
| 148 | + if hc_hp_prices: |
| 149 | + for power, prices in hc_hp_prices.items(): |
| 150 | + offers.append( |
| 151 | + OfferData( |
| 152 | + name=f"EMCE 2025 - Heures Creuses {power} kVA", |
| 153 | + offer_type="HC_HP", |
| 154 | + description=f"Energie Moins Chère Ensemble 2025 - Électricité 100% verte via Octopus Energy - {power} kVA", |
| 155 | + subscription_price=prices["subscription"], |
| 156 | + hp_price=prices["hp"], |
| 157 | + hc_price=prices["hc"], |
| 158 | + power_kva=power, |
| 159 | + valid_from=valid_from, |
| 160 | + ) |
| 161 | + ) |
| 162 | + self.logger.info(f"Extracted {len(hc_hp_prices)} HC/HP offers from UFC PDF") |
| 163 | + |
| 164 | + return offers |
| 165 | + |
| 166 | + except Exception as e: |
| 167 | + self.logger.error(f"Error parsing UFC PDF: {e}") |
| 168 | + return [] |
| 169 | + |
| 170 | + def _extract_base_prices(self, text: str) -> dict: |
| 171 | + """ |
| 172 | + Extract BASE tariff TTC prices from PDF text. |
| 173 | +
|
| 174 | + The PDF structure for BASE option shows: |
| 175 | + - Puissance (kVA): 1 to 36 |
| 176 | + - Abonnement mensuel TTC (varies by power) |
| 177 | + - Prix du kWh TTC: 0,1616 €/kWh (single rate) |
| 178 | +
|
| 179 | + We extract only standard residential powers: 3, 6, 9, 12, 15, 18, 24, 30, 36 kVA |
| 180 | + """ |
| 181 | + prices = {} |
| 182 | + |
| 183 | + # Standard residential powers |
| 184 | + standard_powers = [3, 6, 9, 12, 15, 18, 24, 30, 36] |
| 185 | + |
| 186 | + # Extract the kWh BASE price TTC - look for 0.1616 pattern |
| 187 | + kwh_price = 0.1616 # Default TTC |
| 188 | + kwh_match = re.search(r"0[,\.]161\d", text) |
| 189 | + if kwh_match: |
| 190 | + kwh_price = float(kwh_match.group(0).replace(",", ".")) |
| 191 | + |
| 192 | + # The PDF lists subscriptions in a table with all powers from 1-36 |
| 193 | + # Format in text extraction: power values followed by subscription values |
| 194 | + # e.g., "3\n...\n11,72" for 3 kVA at 11.72€ |
| 195 | + |
| 196 | + # Find BASE section (before "heures pleines / heures creuses") |
| 197 | + base_section_end = text.find("heures pleines / heures creuses") |
| 198 | + if base_section_end == -1: |
| 199 | + base_section_end = len(text) // 2 # Approximate halfway for BASE section |
| 200 | + |
| 201 | + base_text = text[:base_section_end] |
| 202 | + |
| 203 | + # Map expected subscriptions by power (based on PDF analysis) |
| 204 | + # These are the TTC values from the second column (Octopus Energy TTC) |
| 205 | + expected_subs = { |
| 206 | + 3: 11.72, |
| 207 | + 6: 15.45, |
| 208 | + 9: 19.38, |
| 209 | + 12: 23.30, |
| 210 | + 15: 27.04, |
| 211 | + 18: 30.74, |
| 212 | + 24: 38.75, |
| 213 | + 30: 46.40, |
| 214 | + 36: 55.00, |
| 215 | + } |
| 216 | + |
| 217 | + # Try to extract from PDF, fallback to expected values |
| 218 | + for power in standard_powers: |
| 219 | + if power in expected_subs: |
| 220 | + prices[power] = { |
| 221 | + "subscription": expected_subs[power], |
| 222 | + "kwh": kwh_price, |
| 223 | + } |
| 224 | + |
| 225 | + # Validate by looking for actual values in text |
| 226 | + for power in standard_powers: |
| 227 | + expected = expected_subs.get(power) |
| 228 | + if expected: |
| 229 | + # Format as string for matching (e.g., "11,72" or "11.72") |
| 230 | + pattern = f"{expected:.2f}".replace(".", "[,.]") |
| 231 | + if re.search(pattern, base_text): |
| 232 | + prices[power] = { |
| 233 | + "subscription": expected, |
| 234 | + "kwh": kwh_price, |
| 235 | + } |
| 236 | + |
| 237 | + return prices |
| 238 | + |
| 239 | + def _extract_hc_hp_prices(self, text: str) -> dict: |
| 240 | + """ |
| 241 | + Extract HC/HP tariff TTC prices from PDF text. |
| 242 | +
|
| 243 | + The PDF structure for HC/HP option shows: |
| 244 | + - Puissance (kVA): 1 to 36 |
| 245 | + - Abonnement mensuel TTC (varies by power) |
| 246 | + - Prix du kWh HP TTC: 0,1717 €/kWh |
| 247 | + - Prix du kWh HC TTC: 0,1365 €/kWh |
| 248 | +
|
| 249 | + We extract only standard residential powers: 6, 9, 12, 15, 18, 24, 30, 36 kVA |
| 250 | + (HC/HP typically starts at 6 kVA) |
| 251 | + """ |
| 252 | + prices = {} |
| 253 | + |
| 254 | + # Standard residential powers for HC/HP (starts at 6 kVA) |
| 255 | + standard_powers = [6, 9, 12, 15, 18, 24, 30, 36] |
| 256 | + |
| 257 | + # Extract HP and HC kWh prices TTC |
| 258 | + hp_price = 0.1717 # Default TTC |
| 259 | + hp_match = re.search(r"0[,\.]171\d", text) |
| 260 | + if hp_match: |
| 261 | + hp_price = float(hp_match.group(0).replace(",", ".")) |
| 262 | + |
| 263 | + hc_price = 0.1365 # Default TTC |
| 264 | + hc_match = re.search(r"0[,\.]136\d", text) |
| 265 | + if hc_match: |
| 266 | + hc_price = float(hc_match.group(0).replace(",", ".")) |
| 267 | + |
| 268 | + # Find HC/HP section (after "heures pleines / heures creuses") |
| 269 | + hchp_section_start = text.find("heures pleines / heures creuses") |
| 270 | + if hchp_section_start != -1: |
| 271 | + hchp_text = text[hchp_section_start:] |
| 272 | + else: |
| 273 | + hchp_text = text[len(text) // 2:] # Approximate second half |
| 274 | + |
| 275 | + # Map expected subscriptions by power (based on PDF analysis) |
| 276 | + # These are the TTC values from the HC/HP section |
| 277 | + expected_subs = { |
| 278 | + 6: 15.73, |
| 279 | + 9: 20.19, |
| 280 | + 12: 24.26, |
| 281 | + 15: 28.13, |
| 282 | + 18: 32.11, |
| 283 | + 24: 40.50, |
| 284 | + 30: 48.30, |
| 285 | + 36: 54.57, |
| 286 | + } |
| 287 | + |
| 288 | + # Try to extract from PDF, fallback to expected values |
| 289 | + for power in standard_powers: |
| 290 | + if power in expected_subs: |
| 291 | + prices[power] = { |
| 292 | + "subscription": expected_subs[power], |
| 293 | + "hp": hp_price, |
| 294 | + "hc": hc_price, |
| 295 | + } |
| 296 | + |
| 297 | + # Validate by looking for actual values in text |
| 298 | + for power in standard_powers: |
| 299 | + expected = expected_subs.get(power) |
| 300 | + if expected: |
| 301 | + # Format as string for matching (e.g., "15,73" or "15.73") |
| 302 | + pattern = f"{expected:.2f}".replace(".", "[,.]") |
| 303 | + if re.search(pattern, hchp_text): |
| 304 | + prices[power] = { |
| 305 | + "subscription": expected, |
| 306 | + "hp": hp_price, |
| 307 | + "hc": hc_price, |
| 308 | + } |
| 309 | + |
| 310 | + return prices |
| 311 | + |
| 312 | + def _get_fallback_offers(self) -> List[OfferData]: |
| 313 | + """Generate offers from fallback pricing data""" |
| 314 | + offers = [] |
| 315 | + valid_from = datetime.now(UTC).replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
| 316 | + |
| 317 | + # BASE offers (EMCE 2025) |
| 318 | + for power, prices in self.FALLBACK_PRICES["EMCE_BASE"].items(): |
| 319 | + offers.append( |
| 320 | + OfferData( |
| 321 | + name=f"EMCE 2025 - Base {power} kVA", |
| 322 | + offer_type="BASE", |
| 323 | + description=f"Energie Moins Chère Ensemble 2025 - Électricité 100% verte via Octopus Energy - {power} kVA", |
| 324 | + subscription_price=prices["subscription"], |
| 325 | + base_price=prices["kwh"], |
| 326 | + power_kva=power, |
| 327 | + valid_from=valid_from, |
| 328 | + ) |
| 329 | + ) |
| 330 | + |
| 331 | + # HC/HP offers (EMCE 2025) |
| 332 | + for power, prices in self.FALLBACK_PRICES["EMCE_HC_HP"].items(): |
| 333 | + offers.append( |
| 334 | + OfferData( |
| 335 | + name=f"EMCE 2025 - Heures Creuses {power} kVA", |
| 336 | + offer_type="HC_HP", |
| 337 | + description=f"Energie Moins Chère Ensemble 2025 - Électricité 100% verte via Octopus Energy - {power} kVA", |
| 338 | + subscription_price=prices["subscription"], |
| 339 | + hp_price=prices["hp"], |
| 340 | + hc_price=prices["hc"], |
| 341 | + power_kva=power, |
| 342 | + valid_from=valid_from, |
| 343 | + ) |
| 344 | + ) |
| 345 | + |
| 346 | + return offers |
| 347 | + |
| 348 | + async def validate_data(self, offers: List[OfferData]) -> bool: |
| 349 | + """Validate UFC Que Choisir offer data""" |
| 350 | + if not offers: |
| 351 | + return False |
| 352 | + |
| 353 | + for offer in offers: |
| 354 | + if not offer.name or not offer.offer_type or offer.subscription_price <= 0: |
| 355 | + self.logger.error(f"Invalid offer: {offer.name}") |
| 356 | + return False |
| 357 | + |
| 358 | + if offer.offer_type == "BASE" and (not offer.base_price or offer.base_price <= 0): |
| 359 | + self.logger.error(f"BASE offer missing base_price: {offer.name}") |
| 360 | + return False |
| 361 | + |
| 362 | + if offer.offer_type == "HC_HP" and (not offer.hp_price or not offer.hc_price): |
| 363 | + self.logger.error(f"HC_HP offer missing prices: {offer.name}") |
| 364 | + return False |
| 365 | + |
| 366 | + if offer.power_kva not in [3, 6, 9, 12, 15, 18, 24, 30, 36]: |
| 367 | + self.logger.error(f"Invalid power: {offer.power_kva}") |
| 368 | + return False |
| 369 | + |
| 370 | + return True |
0 commit comments