kentish.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Kentish Council — Advertised / Planning Applications (site page, not PlanBuild)
  2. require "nokogiri"
  3. require "uri"
  4. require "cgi"
  5. require_relative "../lib/enrich"
  6. require_relative "../lib/log"
  7. require_relative "../lib/util"
  8. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_kentish
  9. # Set this to the exact page you use for Kentish (from your original file)
  10. URL = "https://www.kentish.tas.gov.au/services/building-and-planning-services/planningapp"
  11. DB.ensure_table!(TABLE)
  12. def abs_url(base, href)
  13. return "" if href.to_s.strip.empty?
  14. URI.join(base, href).to_s rescue href.to_s
  15. end
  16. # Reference formats like:
  17. # DA 2025/00123
  18. # DA2025/00123
  19. # Application No. DA 2025/123
  20. REF_RX1 = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-\._]+)}i # DA 2025/0123
  21. REF_RX2 = %r{\bDA(20\d{2})\s*[-\/]?\s*([0-9]{3,})\b}i # DA2025-0123 or DA2025/0123
  22. REF_RX3 = %r{\bDA\s*([0-9]{1,4})\s*-\s*(20\d{2})\b}i # DA 114-2025
  23. def extract_ref(str)
  24. s = CGI.unescape(str.to_s)
  25. if (m = s.match(REF_RX1))
  26. return "DA #{m[1]} / #{m[2]}"
  27. end
  28. if (m = s.match(REF_RX2))
  29. return "DA #{m[1]} / #{m[2]}"
  30. end
  31. if (m = s.match(REF_RX3))
  32. return "DA #{m[2]} / #{m[1]}"
  33. end
  34. nil
  35. end
  36. DATE_RX = /
  37. (\b\d{1,2}\/\d{1,2}\/\d{2,4}\b|
  38. \b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b|
  39. \b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)
  40. /x
  41. def extract_on_notice_raw(text)
  42. s = text.to_s.gsub(/\s+/, " ")
  43. if (m = s.match(/\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i))
  44. if (d = m[2].match(DATE_RX))
  45. return d[1]
  46. end
  47. end
  48. if (m = s.match(/clos(?:e|ing|es)\s*(on)?\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i))
  49. if (d = m[2].match(DATE_RX))
  50. return d[1]
  51. end
  52. end
  53. if (d = s.match(DATE_RX))
  54. return d[1]
  55. end
  56. ""
  57. end
  58. def first_meaningful_text(node)
  59. return "" unless node
  60. t = node.text.to_s.strip.gsub(/\s+/, " ")
  61. t
  62. end
  63. def nearest_context_text(a)
  64. host = a.ancestors("li, p, div, tr").first || a.parent
  65. first_meaningful_text(host)
  66. end
  67. def parse_document_list(doc, base_url)
  68. # Look for clear “items”: pdf links, or list/table rows containing one
  69. anchors = doc.css("a").select { |a|
  70. href = a["href"].to_s
  71. a.text.to_s.strip.match?(/application|permit|advertis/i) || href.downcase.end_with?(".pdf")
  72. }
  73. rows = []
  74. anchors.each do |a|
  75. href = a["href"].to_s
  76. pdf = abs_url(base_url, href)
  77. ctx = nearest_context_text(a)
  78. link_text = a.text.to_s.strip
  79. text_for_parse = [link_text, ctx].uniq.join(" — ")
  80. # Try to pull fields
  81. ref = extract_ref(text_for_parse)
  82. addr = if link_text.length > 6
  83. link_text
  84. else
  85. ctx[0, 140]
  86. end
  87. on_raw = extract_on_notice_raw(text_for_parse)
  88. on_dt = Util.parse_aus_date(on_raw)
  89. desc = if text_for_parse =~ /proposal\s*[:\-]\s*([^—\-]+)\b/i
  90. $1.strip
  91. else
  92. "Development Application"
  93. end
  94. next if ref.nil? || addr.to_s.strip.empty?
  95. rows << {
  96. council_reference: ref,
  97. address: addr.to_s.strip,
  98. description: desc,
  99. date_received: on_dt,
  100. date_received_raw: on_raw,
  101. document_url: pdf
  102. }
  103. end
  104. rows
  105. end
  106. begin
  107. html = Http.get(URL)
  108. rescue StandardError => e
  109. Log.warn "scraper", "Failed to fetch #{URL}: #{e.class} #{e.message}"
  110. exit 1
  111. end
  112. doc = Nokogiri::HTML(html)
  113. items = parse_document_list(doc, URL)
  114. puts "Found #{items.length} item(s) for #{TABLE}"
  115. items.each do |r|
  116. DB.upsert(TABLE, {
  117. description: r[:description],
  118. date_received: r[:date_received],
  119. date_received_raw: r[:date_received_raw],
  120. on_notice_to: r[:date_received],
  121. on_notice_to_raw: r[:date_received_raw],
  122. address: r[:address],
  123. council_reference: r[:council_reference],
  124. document_url: r[:document_url],
  125. applicant: "",
  126. owner: ""
  127. })
  128. enrich_after_upsert!(
  129. table: TABLE,
  130. council_reference: r[:council_reference],
  131. address: r[:address]
  132. )
  133. puts "Upserted #{r[:council_reference]} -> #{r[:address]}"
  134. end
  135. puts "Done #{TABLE}."