westcoast.rb 5.8 KB

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