flinders_council.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Flinders Council — Current Advertising scraper (site page, not PlanBuild)
  2. require "nokogiri"
  3. require "cgi"
  4. require_relative "../lib/http"
  5. require_relative "../lib/db"
  6. require_relative "../lib/util"
  7. require_relative "../lib/enrich"
  8. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets this from filename: da_flinders
  9. URL = "https://www.flinders.tas.gov.au/current-advertising"
  10. DB.ensure_table!(TABLE)
  11. # Optional column to keep the PDF link
  12. begin
  13. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url VARCHAR(1024) NULL")
  14. rescue StandardError => e
  15. warn "document_url add skipped: #{e.class} #{e.message}"
  16. end
  17. def abs_url(base, href)
  18. return "" if href.to_s.strip.empty?
  19. URI.join(base, href).to_s rescue href.to_s
  20. end
  21. def extract_ref_from(text)
  22. s = CGI.unescape(text.to_s)
  23. # Pattern 1: "DA 2025 / 00017" or "DA 2025/00017"
  24. if (m = s.match(/DA\s*20\d{2}\s*\/\s*\d{3,}/i))
  25. return m[0].gsub(/\s+/, " ").sub(" / ", " / ").strip
  26. end
  27. # Pattern 2: "DA202500017" -> "DA 2025 / 00017"
  28. if (m = s.match(/DA20\d{2}\d{5}/i))
  29. raw = m[0]
  30. year = raw[2,4]
  31. num = raw[6,5]
  32. return "DA #{year} / #{num}"
  33. end
  34. nil
  35. end
  36. html = Http.get(URL)
  37. doc = Nokogiri::HTML(html)
  38. # Pick all advertised PDFs listed on the page
  39. links = doc.css("a").select { |a|
  40. href = a["href"].to_s
  41. href.match?(%r{/client-assets/images/Development/Downloads/Advertising/}i) && href.downcase.end_with?(".pdf")
  42. }
  43. puts "Found #{links.length} items for #{TABLE}"
  44. saved = 0
  45. links.each do |a|
  46. text = a.text.strip.gsub(/\s+/, " ")
  47. pdf = abs_url(URL, a["href"])
  48. # Use link text first, fall back to file name as address
  49. address = text.empty? ? File.basename(pdf).sub(/\.pdf\z/i, "") : text
  50. # Council reference from link text or filename
  51. ref = extract_ref_from(text) || extract_ref_from(File.basename(pdf))
  52. # Description and dates are usually inside the PDF, so keep minimal fields here
  53. description = "Development Application"
  54. date_received_raw = ""
  55. date_received = nil
  56. next if address.empty? || ref.nil?
  57. DB.upsert(TABLE, {
  58. description: description,
  59. date_received: date_received,
  60. date_received_raw: date_received_raw,
  61. address: address,
  62. council_reference: ref,
  63. applicant: "",
  64. owner: ""
  65. })
  66. enrich_after_upsert!(
  67. table: TABLE,
  68. council_reference: council_reference,
  69. address: address
  70. )
  71. begin
  72. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ? WHERE council_reference = ? AND address = ?")
  73. upd.execute(pdf, ref, address)
  74. rescue Mysql2::Error => e
  75. warn "[flinders] db update skipped for #{ref}: #{e.message}"
  76. end
  77. puts "Upserted #{ref} -> #{address}"
  78. saved += 1
  79. end
  80. puts "Done #{TABLE}. Saved #{saved} item(s)."