centralcoast.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Central Coast Council — Current Planning Applications (site page)
  2. # Source: https://www.centralcoast.tas.gov.au/current-planning-applications/
  3. require "date"
  4. require "nokogiri"
  5. require "cgi"
  6. require "fileutils"
  7. require_relative "../lib/enrich"
  8. require_relative "../lib/log"
  9. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets from filename: da_centralcoast
  10. URL = "https://www.centralcoast.tas.gov.au/current-planning-applications/"
  11. DOWNLOAD_ATTACHMENTS = ENV["DOWNLOAD_ATTACHMENTS"] == "1"
  12. DOWNLOAD_DIR = ENV["DOWNLOAD_DIR"] || "/app/downloads"
  13. DB.ensure_table!(TABLE)
  14. def abs_url(base, href)
  15. return "" if href.to_s.strip.empty?
  16. URI.join(base, href).to_s
  17. rescue URI::InvalidURIError
  18. href.to_s
  19. end
  20. def safe_name(s) = s.to_s.gsub(/[^\w\-.]+/, "_")
  21. def filename_from_response(res, fallback)
  22. cd = res.respond_to?(:[]) ? res["content-disposition"].to_s : ""
  23. if cd =~ /filename\*?=(?:UTF-8''|")?([^\";]+)/
  24. return safe_name($1)
  25. end
  26. base = safe_name(fallback || "document")
  27. ct = res.respond_to?(:[]) ? res["content-type"].to_s.downcase : ""
  28. ext = if base =~ /\.[A-Za-z0-9]{2,4}\z/
  29. ""
  30. elsif ct.include?("pdf")
  31. ".pdf"
  32. else
  33. ".bin"
  34. end
  35. "#{base}#{ext}"
  36. end
  37. def download_pdf(url, council_reference)
  38. return nil if url.to_s.strip.empty?
  39. dir = File.join(DOWNLOAD_DIR, "centralcoast", safe_name(council_reference))
  40. FileUtils.mkdir_p(dir)
  41. res = (Http.get_response(url) rescue nil)
  42. body = if res && res.respond_to?(:body)
  43. res.body
  44. else
  45. Http.get(url)
  46. end
  47. fname = filename_from_response(res, File.basename(URI.parse(url).path))
  48. path = File.join(dir, fname)
  49. File.binwrite(path, body.to_s)
  50. puts " saved #{File.basename(path)} (#{body.to_s.bytesize} bytes)"
  51. # adjust if your web container mounts differently
  52. "/downloads/centralcoast/#{safe_name(council_reference)}/#{fname}"
  53. rescue StandardError => e
  54. Log.warn "scraper", "Download failed for #{url}: #{e.class} #{e.message}"
  55. nil
  56. end
  57. # Normalize refs like "DA2025123" or "DA 2025/123" -> "DA 2025 / 123"
  58. def normalize_ref(s)
  59. t = s.to_s
  60. if (m = t.match(/\bDA\s*([12]\d{3})\s*[-\/]?\s*0*([A-Za-z0-9]{2,})\b/i))
  61. return "DA #{m[1]} / #{m[2]}"
  62. elsif (m = t.match(/\bDA(20\d{2})\s*0*([A-Za-z0-9]{2,})\b/i))
  63. return "DA #{m[1]} / #{m[2]}"
  64. end
  65. t.split(/\s*-\s*/, 2).first # fallback
  66. end
  67. # Parse dd-mm-yyyy shown in "Date modified"
  68. def parse_dd_mm_yyyy(s)
  69. s = s.to_s.strip
  70. if s =~ /\A\d{2}-\d{2}-\d{4}\z/
  71. Date.strptime(s, "%d-%m-%Y") rescue nil
  72. else
  73. # fall back to your common AUS parser
  74. Util.parse_aus_date(s)
  75. end
  76. end
  77. html = Http.get(URL)
  78. doc = Nokogiri::HTML(html)
  79. rows = doc.css("table.wpfd-search-result tbody tr.file")
  80. puts "Found #{rows.length} items for #{TABLE}"
  81. saved = 0
  82. rows.each do |tr|
  83. # Primary link + title from the first column
  84. a = tr.at_css("td.file_title a.wpfd_downloadlink")
  85. next unless a
  86. document_url = abs_url(URL, a["href"].to_s)
  87. title_reference = a["title"].to_s.strip
  88. title_text = tr.at_css("input.wpfd_file_preview_link_download")&.[]("data-filetitle").to_s.strip
  89. title_reference = title_reference.empty? ? title_text : title_reference
  90. # Title looks like: "DA2025123 - 148 Main Street, Ulverstone"
  91. council_reference_raw = title_reference.split(" - ").first.to_s.strip
  92. council_reference = normalize_ref(council_reference_raw)
  93. address = if title_reference.include?(" - ")
  94. title_reference.split(" - ", 2)[1].to_s.strip
  95. else
  96. # crude fallback to a street-like token
  97. title_reference[/\b\d+[A-Za-z]*\s[\w\s,]+/, 0].to_s.strip
  98. end
  99. address = title_reference[0, 140] if address.empty?
  100. # Optional description column (often blank)
  101. description = tr.at_css("td.file_desc")&.text.to_s.strip
  102. description = "Development Application" if description.empty?
  103. # Date modified -> date_received
  104. date_received_raw = tr.at_css("td.file_modified")&.text.to_s.strip
  105. date_received = parse_dd_mm_yyyy(date_received_raw)
  106. # Minimal required fields
  107. next if council_reference.to_s.strip.empty? || address.to_s.strip.empty?
  108. DB.upsert(TABLE, {
  109. description: description,
  110. date_received: date_received,
  111. date_received_raw: date_received_raw,
  112. address: address,
  113. council_reference: council_reference,
  114. applicant: "",
  115. owner: ""
  116. })
  117. enrich_after_upsert!(
  118. table: TABLE,
  119. council_reference: council_reference,
  120. address: address
  121. )
  122. local_doc_url = nil
  123. if DOWNLOAD_ATTACHMENTS && !document_url.empty?
  124. local_doc_url = download_pdf(document_url, council_reference)
  125. end
  126. begin
  127. upd = DB.client.prepare(
  128. "UPDATE `#{DB.client.escape(TABLE)}` " \
  129. "SET document_url = ?, local_document_url = ?, title_reference = ? " \
  130. "WHERE council_reference = ? AND address = ?"
  131. )
  132. upd.execute(document_url, local_doc_url, title_reference, council_reference, address)
  133. rescue StandardError => e
  134. Log.warn "scraper", "Extras update skipped for #{council_reference}: #{e.class} #{e.message}"
  135. end
  136. puts "Upserted #{council_reference} -> #{address} #{local_doc_url ? '(downloaded)' : ''}"
  137. saved += 1
  138. end
  139. puts "Done #{TABLE}. Saved #{saved} item(s)."