kentish.rb 3.9 KB

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