break_oday.rb 5.3 KB

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