clarence.rb 6.8 KB

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