waratah_wynyard.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # Waratah–Wynyard Council — Advertised / Planning Applications
  2. require "nokogiri"
  3. require "uri"
  4. require "cgi"
  5. require_relative "../lib/http"
  6. require_relative "../lib/db"
  7. require_relative "../lib/util"
  8. require_relative "../lib/enrich"
  9. TABLE = ENV.fetch("TABLE_NAME") # da_waratah_wynyard
  10. URL = "https://www.warwyn.tas.gov.au/planning-and-development/advertised-permits/"
  11. DB.ensure_table!(TABLE)
  12. # Optional extras
  13. begin
  14. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url TEXT NULL")
  15. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to DATE NULL")
  16. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to_raw VARCHAR(80) NULL")
  17. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS title_reference TEXT NULL")
  18. rescue StandardError => e
  19. warn "Optional column add skipped: #{e.class} #{e.message}"
  20. end
  21. def abs_url(base, href)
  22. return "" if href.to_s.strip.empty?
  23. URI.join(base, href).to_s
  24. rescue URI::InvalidURIError
  25. href.to_s
  26. end
  27. # DA 2025/0123, DA2025-0123, DA 114-2025 etc.
  28. REF_RX1 = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-\._]+)}i
  29. REF_RX2 = %r{\bDA(20\d{2})\s*[-\/]?\s*([0-9]{3,})\b}i
  30. REF_RX3 = %r{\bDA\s*([0-9]{1,4})\s*-\s*(20\d{2})\b}i
  31. def extract_ref(str)
  32. s = CGI.unescape(str.to_s)
  33. if (m = s.match(REF_RX1)) then return "DA #{m[1]} / #{m[2]}" end
  34. if (m = s.match(REF_RX2)) then return "DA #{m[1]} / #{m[2]}" end
  35. if (m = s.match(REF_RX3)) then return "DA #{m[2]} / #{m[1]}" end
  36. nil
  37. end
  38. def extract_date_like(str)
  39. s = str.to_s
  40. return $1 if s =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  41. return $1 if s =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  42. return $1 if s =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  43. ""
  44. end
  45. def extract_on_notice_raw(text)
  46. s = text.to_s.gsub(/\s+/, " ")
  47. if s =~ /\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  48. d = extract_date_like($2); return d unless d.empty?
  49. end
  50. if s =~ /clos(?:e|ing|es)\s*(on)?\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  51. d = extract_date_like($2); return d unless d.empty?
  52. end
  53. if s =~ /submissions?\s*close\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  54. d = extract_date_like($1); return d unless d.empty?
  55. end
  56. extract_date_like(s)
  57. end
  58. def nearest_context_text(a)
  59. host = a.ancestors("li, p, div, tr, article").first || a.parent
  60. host ? host.text.to_s.strip.gsub(/\s+/, " ") : ""
  61. end
  62. def parse_list_items(doc, base_url)
  63. rows = []
  64. anchors = doc.css("a").select { |a|
  65. href = a["href"].to_s
  66. a.text.to_s.strip.match?(/application|permit|planning|advertis/i) || href.downcase.end_with?(".pdf")
  67. }
  68. anchors.each do |a|
  69. href = a["href"].to_s
  70. link_text = a.text.to_s.strip
  71. document_url = abs_url(base_url, href)
  72. ctx = nearest_context_text(a)
  73. title_reference = link_text.empty? ? ctx[0,200] : link_text
  74. text_for_parse = [link_text, ctx].reject(&:empty?).uniq.join(" — ")
  75. # Address guess
  76. address = link_text.length >= 6 ? link_text : ctx[0, 140]
  77. ref = extract_ref(text_for_parse) || extract_ref(File.basename(document_url))
  78. on_raw = extract_on_notice_raw(text_for_parse)
  79. on_dt = Util.parse_aus_date(on_raw)
  80. description = if text_for_parse =~ /proposal\s*[:\-]\s*([^—\-]+)\b/i
  81. $1.strip
  82. else
  83. "Development Application"
  84. end
  85. next if ref.nil? || address.to_s.strip.empty?
  86. rows << {
  87. council_reference: ref,
  88. address: address.to_s.strip,
  89. description: description,
  90. date_received: on_dt,
  91. date_received_raw: on_raw,
  92. document_url: document_url,
  93. title_reference: title_reference
  94. }
  95. end
  96. rows.uniq { |r| [r[:council_reference], r[:address]] }
  97. end
  98. def parse_detail_page(url)
  99. html = Http.get(url)
  100. doc = Nokogiri::HTML(html)
  101. # Try simple two-column tables first
  102. kv = {}
  103. doc.css("table tr").each do |tr|
  104. cells = tr.css("th, td")
  105. next unless cells.length >= 2
  106. key = cells[0].text.strip
  107. val = cells[1].text.strip
  108. kv[key] = val unless key.empty?
  109. end
  110. if kv.any?
  111. find = ->(rx) { kv.find { |k, _| k =~ rx }&.last.to_s.strip }
  112. council_reference = find.call(/(Application\s*(No|Number|ID)|Reference)/i)
  113. address = find.call(/(Address|Location|Property)/i)
  114. description = find.call(/(Proposal|Description)/i)
  115. on_notice_raw = find.call(/(On\s*Notice\s*(until|to)|Closing\s*Date|Closes|Submissions)/i)
  116. pdf = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")&.[]("href")
  117. document_url = pdf ? abs_url(url, pdf) : ""
  118. unless council_reference.empty? || address.empty?
  119. return {
  120. council_reference: council_reference,
  121. address: address,
  122. description: description.empty? ? "Development Application" : description,
  123. date_received_raw: on_notice_raw,
  124. date_received: Util.parse_aus_date(on_notice_raw),
  125. document_url: document_url,
  126. title_reference: doc.at_css("h1, .entry-title")&.text&.strip.to_s
  127. }
  128. end
  129. end
  130. # Fallback: parse from page text
  131. page_text = doc.text.to_s.strip.gsub(/\s+/, " ")
  132. ref = extract_ref(page_text)
  133. on_raw = extract_on_notice_raw(page_text)
  134. on_dt = Util.parse_aus_date(on_raw)
  135. h1 = doc.at_css("h1, .entry-title")&.text&.strip.to_s
  136. address = h1.empty? ? page_text[0, 140] : h1
  137. pdf = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")&.[]("href")
  138. document_url = pdf ? abs_url(url, pdf) : ""
  139. return nil if ref.nil? || address.empty?
  140. {
  141. council_reference: ref,
  142. address: address,
  143. description: "Development Application",
  144. date_received_raw: on_raw,
  145. date_received: on_dt,
  146. document_url: document_url,
  147. title_reference: h1
  148. }
  149. end
  150. begin
  151. html = URL.include?("/eservice/") ? Http.dorset_session_get(URL) : Http.get(URL)
  152. rescue StandardError => e
  153. warn "Failed to fetch #{URL}: #{e.class} #{e.message}"
  154. exit 1
  155. end
  156. doc = Nokogiri::HTML(html)
  157. host = begin
  158. URI.parse(URL).host
  159. rescue URI::InvalidURIError
  160. nil
  161. end
  162. anchors = doc.css("a").map { |a| abs_url(URL, a["href"].to_s) }.select { |u|
  163. next false if u.empty? || u.start_with?("#")
  164. u.downcase.end_with?(".pdf") || begin
  165. uh = URI.parse(u).host rescue nil
  166. host && uh == host
  167. end
  168. }.uniq
  169. rows = []
  170. anchors.each do |u|
  171. if u.downcase.end_with?(".pdf")
  172. if (a = doc.at_css(%Q{a[href="#{u}"]}))
  173. ctx_text = nearest_context_text(a)
  174. title = a.text.to_s.strip
  175. ref = extract_ref([title, ctx_text].join(" — "))
  176. addr = title.length >= 6 ? title : ctx_text[0, 140]
  177. on_raw = extract_on_notice_raw([title, ctx_text].join(" — "))
  178. on_dt = Util.parse_aus_date(on_raw)
  179. next if ref.nil? || addr.to_s.strip.empty?
  180. rows << {
  181. council_reference: ref,
  182. address: addr,
  183. description: "Development Application",
  184. date_received: on_dt,
  185. date_received_raw: on_raw,
  186. document_url: u,
  187. title_reference: title.empty? ? ctx_text[0,200] : title
  188. }
  189. end
  190. else
  191. begin
  192. item = parse_detail_page(u)
  193. rows << item if item
  194. rescue StandardError => e
  195. warn "Skip detail #{u}: #{e.class} #{e.message}"
  196. end
  197. end
  198. end
  199. # Safety net: scrape items from the main page content too
  200. rows += parse_list_items(doc, URL)
  201. rows.uniq! { |r| [r[:council_reference], r[:address]] }
  202. puts "Found #{rows.length} item(s) for #{TABLE}"
  203. rows.each do |r|
  204. cr = r[:council_reference].to_s
  205. addr = r[:address].to_s
  206. next if addr.strip.empty?
  207. next if addr =~ /\A(?:download|advertised planning applications)\z/i
  208. next if cr.strip.empty?
  209. next if addr == cr
  210. DB.upsert(TABLE, {
  211. description: r[:description],
  212. date_received: r[:date_received],
  213. date_received_raw: r[:date_received_raw],
  214. address: addr,
  215. council_reference: cr,
  216. applicant: "",
  217. owner: ""
  218. })
  219. enrich_after_upsert!(
  220. table: TABLE,
  221. council_reference: cr,
  222. address: addr
  223. )
  224. begin
  225. upd = DB.client.prepare(
  226. "UPDATE `#{DB.client.escape(TABLE)}` " \
  227. "SET document_url = ?, on_notice_to = ?, on_notice_to_raw = ?, title_reference = ? " \
  228. "WHERE council_reference = ? AND address = ?"
  229. )
  230. upd.execute(r[:document_url], r[:date_received], r[:date_received_raw], r[:title_reference], cr, addr)
  231. rescue StandardError => e
  232. warn "Extras update skipped for #{cr}: #{e.class} #{e.message}"
  233. end
  234. puts "Upserted #{cr} -> #{addr}"
  235. end
  236. puts "Done #{TABLE}."