Quellcode durchsuchen

Further Lton Updates

Benjamin Harris vor 2 Monaten
Ursprung
Commit
64b09021af
4 geänderte Dateien mit 47 neuen und 27 gelöschten Zeilen
  1. 1 0
      lib/db.rb
  2. 14 0
      lib/migrate.rb
  3. 8 26
      scrapers/launcestoncity.rb
  4. 24 1
      web/index.php

+ 1 - 0
lib/db.rb

@@ -54,6 +54,7 @@ module DB
             postcode VARCHAR(10) NULL,
             document_url TEXT NULL,
             local_document_url TEXT NULL,
+            documents_json MEDIUMTEXT NULL,
             lat DECIMAL(10,7) NULL,
             lng DECIMAL(10,7) NULL,
             PRIMARY KEY (id),

+ 14 - 0
lib/migrate.rb

@@ -82,6 +82,20 @@ module Migrate
         SQL
       }
     }
+    {
+      version: 3,
+      description: "Add documents_json column to all existing da_* tables",
+      up: -> {
+        Migrate.da_tables.each do |table|
+          esc = DB.client.escape(table)
+          DB.client.query(
+            "ALTER TABLE `#{esc}` ADD COLUMN IF NOT EXISTS `documents_json` MEDIUMTEXT NULL"
+          )
+        rescue Mysql2::Error => e
+          Log.warn "migrate", "skipped #{table}.documents_json: #{e.message}"
+        end
+      }
+    }
   ].freeze
 
   # -------------------------------------------------------------------------

+ 8 - 26
scrapers/launcestoncity.rb

@@ -438,32 +438,14 @@ tables.each do |t|
 		puts "Found #{documents.size} docs for #{council_reference}" if doc_list_url
 
 		DB.upsert(TABLE, {
-		  # --- always include your base fields again ---
-		  council_reference:          council_reference,
-		  description:                description,
-		  address:                    address,
-		  info_url:                   info_url,
-		  on_notice_to:               closing_date,
-		  on_notice_to_raw:           closing_raw,
-
-		  # --- enrich fields from details page ---
-		  applicant:                  applicant_name,
-		  status:                     status_text,
-		  assigned_officer:           assigned_off,
-		  group:                      group_text,
-		  category:                   category_text,
-		  date_received:              received_date,
-		  date_received_raw:          received_raw,
-		  application_valid:          valid_date,
-		  application_valid_raw:      valid_raw,
-		  advertised_on:              advertised_date,
-		  advertised_on_raw:          advertised_raw,
-		  property_legal_description: legal_desc,
-
-		  # --- documents ---
-		  pdf_url:                    first_doc_url,           # <-- was document_url
-		  local_document_url:         first_local,             # keep
-		  documents_json:             JSON.generate(documents) # full set
+		  council_reference:  council_reference,
+		  address:            address,
+		  applicant:          applicant_name,
+		  date_received:      received_date,
+		  date_received_raw:  received_raw,
+		  document_url:       first_doc_url,
+		  local_document_url: first_local,
+		  documents_json:     documents.empty? ? nil : JSON.generate(documents)
 		})
 
     rescue StandardError => e

+ 24 - 1
web/index.php

@@ -192,6 +192,7 @@ foreach ($tables as $t) {
     $cols[] = tableHasColumn($pdo, $t, 'title_reference')     ? "title_reference"                               : "'' AS title_reference";
     $cols[] = tableHasColumn($pdo, $t, 'document_url')        ? "COALESCE(document_url,'') AS document_url"     : "'' AS document_url";
     $cols[] = tableHasColumn($pdo, $t, 'local_document_url')  ? "COALESCE(local_document_url,'') AS local_document_url" : "'' AS local_document_url";
+    $cols[] = tableHasColumn($pdo, $t, 'documents_json')      ? "documents_json"                                : "NULL AS documents_json";
 
     validate_table_name($t);
     $selects[] = "SELECT ".implode(", ", $cols)." FROM `{$t}`";
@@ -327,6 +328,22 @@ $rows = $st->fetchAll();
                            $docLocal = trim((string)($r['local_document_url'] ?? ''));
                            $docWeb   = trim((string)($r['document_url'] ?? ''));
                            $docHref  = $docLocal !== '' ? $docLocal : $docWeb;
+
+                           // multi-document list (e.g. Launceston)
+                           $docList = [];
+                           $rawJson = $r['documents_json'] ?? '';
+                           if ($rawJson !== '' && $rawJson !== null) {
+                               $decoded = json_decode($rawJson, true);
+                               if (is_array($decoded)) {
+                                   foreach ($decoded as $d) {
+                                       $href = trim((string)($d['local_url'] ?? ''));
+                                       if ($href === '') $href = trim((string)($d['url'] ?? ''));
+                                       $name = trim((string)($d['name'] ?? ''));
+                                       if ($name === '') $name = 'Document';
+                                       if ($href !== '') $docList[] = ['href' => $href, 'name' => $name];
+                                   }
+                               }
+                           }
                 ?>
                 <div class="accordion-item">
                     <h2 class="accordion-header" id="<?= $uid ?>-head">
@@ -394,7 +411,13 @@ $rows = $st->fetchAll();
                                     <input type="text" class="form-control form-control-sm" value="<?= is_null($days) ? '' : $days ?>" disabled readonly>
                                 </div>
                                 <div class="col-12">
-                                    <?php if ($docHref !== ''): ?>
+                                    <?php if (!empty($docList)): ?>
+                                    <div class="d-flex flex-wrap gap-2">
+                                        <?php foreach ($docList as $doc): ?>
+                                        <a class="btn btn-sm btn-outline-secondary" href="<?= h($doc['href']) ?>" target="_blank" rel="noopener"><?= h($doc['name']) ?></a>
+                                        <?php endforeach; ?>
+                                    </div>
+                                    <?php elseif ($docHref !== ''): ?>
                                     <a class="btn btn-outline-secondary" href="<?= h($docHref) ?>" target="_blank" rel="noopener">Open document</a>
                                     <?php else: ?>
                                     <span class="text-muted">No document link</span>