westcoast.rb 5.4 KB

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