break_oday.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Break O'Day Council — Advertised Development Applications
  2. # List page: https://www.bodc.tas.gov.au/council/advertised-development-applications/
  3. require "nokogiri"
  4. require "cgi"
  5. require "uri"
  6. require_relative "../lib/http"
  7. require_relative "../lib/db"
  8. require_relative "../lib/util"
  9. require_relative "../lib/geocode"
  10. require_relative "../lib/enrich"
  11. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_break_oday
  12. URL = "https://www.bodc.tas.gov.au/council/advertised-development-applications/"
  13. DOWNLOAD_ATTACHMENTS = ENV["DOWNLOAD_ATTACHMENTS"] == "1"
  14. DOWNLOAD_DIR = ENV["DOWNLOAD_DIR"] || "/app/downloads"
  15. DB.ensure_table!(TABLE)
  16. def abs_url(base, href)
  17. return "" if href.to_s.strip.empty?
  18. URI.join(base, href).to_s rescue href.to_s
  19. end
  20. # Accepts "DA 2025/123", "DA2025/0123", "DA 054-2026", etc
  21. REF_RXES = [
  22. %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-_.]+)}i, # DA 2025/123
  23. %r{\bDA(20\d{2})\s*[-/]?\s*([0-9]{3,})\b}i, # DA2025-0123
  24. %r{\bDA\s*([0-9]{1,4})\s*-\s*(20\d{2})\b}i # DA 054-2026
  25. ]
  26. def extract_ref(str)
  27. s = CGI.unescape(str.to_s)
  28. REF_RXES.each do |rx|
  29. if (m = s.match(rx))
  30. # normalize to "DA YYYY / NNN"
  31. if rx.source.include?("\\s*-\\s*") # hyphen form "054-2026"
  32. return "DA #{m[2]} / #{m[1]}"
  33. else
  34. return "DA #{m[1]} / #{m[2]}"
  35. end
  36. end
  37. end
  38. nil
  39. end
  40. def find_list_table(doc)
  41. doc.css("table").find do |t|
  42. heads = t.css("thead th").map { |th| th.text.strip.downcase }
  43. heads.any? && (
  44. heads.any? { |h| h.include?("closing") } ||
  45. heads.any? { |h| h.include?("pdf") } ||
  46. heads.any? { |h| h.include?("name") }
  47. )
  48. end
  49. end
  50. def safe_name(s) = s.to_s.gsub(/[^\w\-.]+/, "_")
  51. def download_pdf(url, council_reference)
  52. return nil unless DOWNLOAD_ATTACHMENTS && !url.to_s.strip.empty?
  53. folder = File.join(DOWNLOAD_DIR, 'breakoday', safe_name(council_reference))
  54. FileUtils.mkdir_p(folder)
  55. begin
  56. res = Http.get_response(url) rescue Http.get(url)
  57. # If Http.get already gives us the body, use it directly
  58. body = res.respond_to?(:body) ? res.body : res.to_s
  59. fname = safe_name(File.basename(URI.parse(url).path))
  60. fname += ".pdf" unless fname.downcase.end_with?(".pdf")
  61. path = File.join(folder, fname)
  62. File.binwrite(path, body)
  63. puts "Saved PDF #{path}"
  64. # return web-accessible relative path if needed
  65. "/downloads/breakoday/#{safe_name(council_reference)}/#{fname}"
  66. rescue StandardError => e
  67. warn "PDF download failed for #{url}: #{e.class} #{e.message}"
  68. nil
  69. end
  70. end
  71. html = Http.get(URL)
  72. doc = Nokogiri::HTML(html)
  73. table = find_list_table(doc) || doc.at_css("table")
  74. unless table
  75. puts "No table found on #{URL}"
  76. exit 0
  77. end
  78. # Work out the column indexes by header text if possible
  79. headers = table.css("thead th").map { |th| th.text.strip.downcase }
  80. idx_name = headers.index { |h| h.include?("name") } || 0
  81. idx_addr = headers.index { |h| h.include?("address") } || 1
  82. idx_close = headers.index { |h| h.include?("closing") || h.include?("notice") } || 2
  83. idx_pdf = headers.index { |h| h.include?("pdf") } || 3
  84. rows = table.css("tbody tr")
  85. puts "Found #{rows.length} row(s) for #{TABLE}"
  86. saved = 0
  87. rows.each do |tr|
  88. tds = tr.css("td")
  89. next if tds.empty?
  90. name_text = tds[idx_name]&.text&.strip.to_s
  91. address = tds[idx_addr]&.text&.strip.to_s
  92. close_raw = tds[idx_close]&.text&.strip.to_s
  93. pdf_cell = tds[idx_pdf]
  94. pdf_a = pdf_cell&.at_css("a[href]")
  95. document_url = pdf_a ? abs_url(URL, pdf_a["href"].to_s) : ""
  96. row_text = tr.text.to_s.gsub(/\s+/, " ")
  97. raw_ref = extract_ref(pdf_cell&.text) || extract_ref(File.basename(document_url)) || extract_ref(row_text)
  98. council_reference = raw_ref&.gsub(/\s*\/\s*/, "_")&.gsub(/\s+/, "_")
  99. next if address.empty? || council_reference.nil?
  100. on_notice = Util.parse_aus_date(close_raw)
  101. description = name_text.empty? ? "Development Application" : name_text
  102. local_doc_url = download_pdf(document_url, council_reference)
  103. DB.upsert(TABLE, {
  104. description: description,
  105. address: address,
  106. council_reference: council_reference,
  107. applicant: "",
  108. owner: "",
  109. document_url: document_url,
  110. local_document_url: local_doc_url,
  111. on_notice_to: on_notice,
  112. on_notice_to_raw: close_raw,
  113. })
  114. enrich_after_upsert!(table: TABLE, council_reference: council_reference, address: address)
  115. tn = DB.client.escape(TABLE)
  116. sql = %Q{
  117. SELECT address_std, lat, lng
  118. FROM `#{tn}`
  119. WHERE council_reference = ? AND address = ?
  120. LIMIT 1
  121. }
  122. begin
  123. row = DB.client.prepare(sql).execute(council_reference, address).first
  124. puts " enriched -> #{row ? row.inspect : 'nil'}"
  125. rescue StandardError => e
  126. warn " enriched probe failed: #{e.class} #{e.message}"
  127. end
  128. puts "Upserted #{council_reference} -> #{address}"
  129. saved += 1
  130. end
  131. puts "Done #{TABLE}. Saved #{saved} item(s)."