clarence.rb 6.8 KB

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