westcoast.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # West Coast Council — Advertised Development Applications
  2. # Source list: https://www.westcoast.tas.gov.au/planning-and-development/planning/advertised-development-applications/
  3. require "date"
  4. require "nokogiri"
  5. require "cgi"
  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_westcoast
  10. URL = "https://www.westcoast.tas.gov.au/planning-and-development/planning/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 DA2025-26, DA 2025/26, DA2025/026, DA 26-2025
  19. REF_RXES = [
  20. %r{\bDA\s*(20\d{2})\s*[/\-]\s*([A-Za-z0-9\-_.]{1,})\b}i, # DA 2025/26 or DA2025-26
  21. %r{\bDA\s*(20\d{2})([A-Za-z0-9]{3,})\b}i, # DA2025012
  22. %r{\bDA\s*([0-9]{1,4})\s*-\s*(20\d{2})\b}i # DA 26-2025
  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*(20") && m[2] # hyphen reversed
  30. return "DA #{m[2]} / #{m[1]}"
  31. elsif rx.source.include?("(20\\d{2})([A-Za-z0-9]{3,})")
  32. return "DA #{m[1]} / #{m[2]}"
  33. else
  34. return "DA #{m[1]} / #{m[2]}"
  35. end
  36. end
  37. end
  38. nil
  39. end
  40. def extract_date_token(str)
  41. s = str.to_s
  42. return $1 if s =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  43. return $1 if s =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  44. return $1 if s =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  45. ""
  46. end
  47. def extract_on_notice_raw(text)
  48. s = text.to_s.gsub(/\s+/, " ")
  49. # West Coast detail pages usually say: "Representations must be received by 5pm on Monday 1 September 2025"
  50. if s =~ /representations? .*? (received|made) .*? by .*?([A-Za-z0-9\/ ,]+)/i
  51. d = extract_date_token($2)
  52. return d unless d.empty?
  53. end
  54. if s =~ /\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  55. d = extract_date_token($2)
  56. return d unless d.empty?
  57. end
  58. extract_date_token(s)
  59. end
  60. def nearest_ctx(a)
  61. host = a.ancestors("article, li, p, div").first || a.parent
  62. host ? host.text.to_s.strip.gsub(/\s+/, " ") : ""
  63. end
  64. def parse_detail(url)
  65. html = Http.get(url)
  66. doc = Nokogiri::HTML(html)
  67. title_reference = doc.at_css("h1, .entry-title")&.text&.strip.to_s
  68. page_text = doc.text.to_s.gsub(/\s+/, " ")
  69. # Reference from title or page body
  70. council_reference = extract_ref(title_reference) || extract_ref(page_text)
  71. # Address from title after ":" if present, else from first " at <address>" phrase
  72. address = if title_reference.include?(":")
  73. title_reference.split(":", 2)[1].to_s.strip
  74. elsif (m = page_text.match(/\bat\s+([A-Z0-9].+?)(?:\.\s|, Representations| In accordance|$)/i))
  75. m[1].strip
  76. else
  77. title_reference
  78. end
  79. address = address[0, 140] if address.length > 140
  80. # Description often appears near the top: "Residential – Outbuilding", etc
  81. description = if (m = page_text.match(/(Residential|Commercial|Industrial|Subdivision|Outbuilding|Dwelling|Multiple Dwelling|Alterations|Additions|Use|Development)\s*[–-]\s*([A-Za-z ]+)/i))
  82. [m[1], m[2]].join(" - ").strip
  83. else
  84. "Development Application"
  85. end
  86. # On-notice
  87. on_notice_raw = extract_on_notice_raw(page_text)
  88. on_notice = Util.parse_aus_date(on_notice_raw)
  89. # First PDF on the page
  90. pdf = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")&.[]("href")
  91. document_url = pdf ? abs_url(url, pdf) : ""
  92. return nil if council_reference.to_s.strip.empty? || address.to_s.strip.empty?
  93. {
  94. council_reference: council_reference,
  95. address: address,
  96. description: description,
  97. on_notice: on_notice,
  98. on_notice_raw: on_notice_raw,
  99. document_url: document_url,
  100. title_reference: title_reference
  101. }
  102. end
  103. # 1) Open the list page and find links to individual Development Application posts
  104. list_html = Http.get(URL)
  105. list_doc = Nokogiri::HTML(list_html)
  106. detail_links = list_doc.css("a").map { |a|
  107. href = a["href"].to_s
  108. u = abs_url(URL, href)
  109. u if u.include?("/development-application/")
  110. }.compact.uniq
  111. puts "Found #{detail_links.size} candidate link(s) for #{TABLE}"
  112. saved = 0
  113. date_received = Date.today
  114. detail_links.each do |u|
  115. begin
  116. item = parse_detail(u)
  117. rescue StandardError => e
  118. Log.warn "scraper", "Skip #{u}: #{e.class} #{e.message}"
  119. next
  120. end
  121. next unless item
  122. DB.upsert(TABLE, {
  123. description: item[:description],
  124. date_received: date_received,
  125. date_received_raw: date_received.to_s,
  126. on_notice_to: item[:on_notice],
  127. on_notice_to_raw: item[:on_notice_raw],
  128. address: item[:address],
  129. council_reference: item[:council_reference],
  130. document_url: item[:document_url],
  131. title_reference: item[:title_reference],
  132. applicant: "",
  133. owner: ""
  134. })
  135. enrich_after_upsert!(
  136. table: TABLE,
  137. council_reference: item[:council_reference],
  138. address: item[:address]
  139. )
  140. puts "Upserted #{item[:council_reference]} -> #{item[:address]}"
  141. saved += 1
  142. end
  143. puts "Done #{TABLE}. Saved #{saved} item(s)."