break_oday.rb 5.3 KB

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