clarence.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Clarence City Council — Advertised Plans
  2. # Source list: https://www.ccc.tas.gov.au/development/advertised-plans/
  3. require "nokogiri"
  4. require "cgi"
  5. require "uri"
  6. require "date"
  7. require "fileutils"
  8. require_relative "../lib/http"
  9. require_relative "../lib/db"
  10. require_relative "../lib/util"
  11. require_relative "../lib/geocode"
  12. require_relative "../lib/enrich"
  13. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_clarence
  14. URL = "https://www.ccc.tas.gov.au/development/advertised-plans/"
  15. DOWNLOAD_ATTACHMENTS = ENV["DOWNLOAD_ATTACHMENTS"] == "1"
  16. DOWNLOAD_DIR = ENV["DOWNLOAD_DIR"] || "/app/downloads"
  17. DB.ensure_table!(TABLE)
  18. ensure_extra_columns!(TABLE)
  19. def abs_url(base, href)
  20. return "" if href.to_s.strip.empty?
  21. URI.join(base, href).to_s
  22. rescue URI::InvalidURIError
  23. href.to_s
  24. end
  25. def extract_app_number(text)
  26. text.to_s[/Application\s*Number:\s*([A-Za-z0-9\/\-\._]+)/i, 1].to_s.strip
  27. end
  28. def extract_close_raw(text)
  29. text.to_s[/Closes:\s*([^\n\r<]+)/i, 1].to_s.strip
  30. end
  31. def parse_date_token(s)
  32. s = s.to_s
  33. return $1 if s =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  34. return $1 if s =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  35. return $1 if s =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  36. ""
  37. end
  38. def looks_like_address(s)
  39. s =~ /\d{1,4}\s+\S+/ && s =~ /,\s*[A-Z][A-Z]+/
  40. end
  41. def split_title(title)
  42. parts = title.split(/\s+–\s+/) # en dash
  43. parts = title.split(/\s+-\s+/) if parts.length < 2
  44. parts.map!(&:strip)
  45. parts
  46. end
  47. def pick_address_from_title(parts)
  48. parts.find { |p| looks_like_address(p) } || parts.find { |p| p =~ /\d/ } || parts[1].to_s
  49. end
  50. def pick_description_from_title(parts, code, address)
  51. parts.find { |p| p != code && p != address && p.length > 3 }.to_s
  52. end
  53. def safe_name(s) = s.to_s.gsub(/[^\w\-.]+/, "_")
  54. # Download the PDF (if enabled) and return a web path like:
  55. # /downloads/clarence/<council_reference>/<filename.pdf>
  56. def download_pdf(url, council_reference)
  57. return nil unless DOWNLOAD_ATTACHMENTS && !url.to_s.strip.empty?
  58. folder = File.join(DOWNLOAD_DIR, "clarence", safe_name(council_reference))
  59. FileUtils.mkdir_p(folder)
  60. begin
  61. res = Http.get_response(url) rescue Http.get(url)
  62. body = res.respond_to?(:body) ? res.body : res.to_s
  63. fname = safe_name(File.basename(URI.parse(url).path))
  64. fname += ".pdf" unless fname.downcase.end_with?(".pdf")
  65. path = File.join(folder, fname)
  66. File.binwrite(path, body)
  67. puts "Saved PDF #{path}"
  68. # Web-accessible path (served by your web container)
  69. "/downloads/clarence/#{safe_name(council_reference)}/#{fname}"
  70. rescue => e
  71. warn "PDF download failed for #{url}: #{e.class} #{e.message}"
  72. nil
  73. end
  74. end
  75. list_html = Http.get(URL)
  76. doc = Nokogiri::HTML(list_html)
  77. items = []
  78. # Headings tend to be h2/h3, followed by blocks that contain
  79. # “Closes:” and “Application Number:” and a PDF link.
  80. doc.css("h2, h3").each do |h|
  81. title = h.text.to_s.strip
  82. next if title.empty?
  83. texts = []
  84. pdf_url = ""
  85. node = h
  86. 12.times do
  87. node = node.next_element
  88. break if node.nil? || node.name =~ /^h[23]$/i
  89. texts << node.text.to_s.strip
  90. if (a = node.at_css("a[href]"))
  91. href = a["href"].to_s
  92. if href =~ /\.pdf($|\?)/i || href.include?("assets.ccc.tas.gov.au")
  93. pdf_url = abs_url(URL, href)
  94. end
  95. end
  96. end
  97. detail_text = texts.join("\n")
  98. app_no_raw = extract_app_number(detail_text)
  99. closes_raw = extract_close_raw(detail_text)
  100. closes_tok = parse_date_token(closes_raw)
  101. on_notice = Util.parse_aus_date(closes_tok)
  102. parts = split_title(title)
  103. code = parts.first.to_s
  104. address = pick_address_from_title(parts).to_s
  105. desc = pick_description_from_title(parts, code, address)
  106. desc = "Development Application" if desc.strip.empty?
  107. council_reference = app_no_raw.empty? ? code : app_no_raw
  108. next if council_reference.strip.empty? || address.strip.empty?
  109. items << {
  110. council_reference: council_reference,
  111. address: address,
  112. description: desc,
  113. on_notice_raw: closes_tok,
  114. on_notice: on_notice,
  115. pdf: pdf_url,
  116. title_reference: title
  117. }
  118. end
  119. items.uniq! { |r| [r[:council_reference], r[:address]] }
  120. puts "Found #{items.length} item(s) for #{TABLE}"
  121. date_received = Date.today
  122. items.each do |r|
  123. cr = r[:council_reference].to_s
  124. addr = r[:address].to_s
  125. # Skip site promo / competitions that occasionally appear as a “heading”
  126. next if cr =~ /turn your two cents/i || r[:title_reference].to_s =~ /two cents/i
  127. # Skip if we didn’t get a sensible address
  128. next if addr.strip.empty? || addr == cr
  129. # Clarence app numbers look like PDPLANPMTD-2025/054004 etc
  130. next unless cr =~ /\APDPLAN[A-Z]*-\d{4}\/\d+\z/
  131. DB.upsert(TABLE, {
  132. description: r[:description],
  133. date_received: date_received,
  134. on_notice_to: r[:on_notice],
  135. on_notice_to_raw: r[:on_notice_raw],
  136. address: addr,
  137. council_reference: cr,
  138. applicant: "",
  139. owner: ""
  140. })
  141. enrich_after_upsert!(
  142. table: TABLE,
  143. council_reference: cr,
  144. address: addr
  145. )
  146. # Try to download and set local_document_url
  147. local_doc_url = download_pdf(r[:pdf], cr)
  148. begin
  149. upd = DB.client.prepare(
  150. "UPDATE `#{DB.client.escape(TABLE)}` " \
  151. "SET document_url = ?, " \
  152. " local_document_url = COALESCE(?, local_document_url), " \
  153. " on_notice_to = ?, on_notice_to_raw = ?, title_reference = ? " \
  154. "WHERE council_reference = ? AND address = ?"
  155. )
  156. upd.execute(r[:pdf], local_doc_url, r[:on_notice], r[:on_notice_raw], r[:title_reference], cr, addr)
  157. rescue => e
  158. warn "Extras update skipped for #{cr}: #{e.class} #{e.message}"
  159. end
  160. puts "Upserted #{cr} -> #{addr} saved: #{local_doc_url ? 1 : 0}"
  161. end
  162. puts "Done #{TABLE}."