centralcoast.rb 5.2 KB

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