northernmidlands.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Northern Midlands Council — Advertised / Planning Applications (site page)
  2. require "nokogiri"
  3. require "uri"
  4. require "cgi"
  5. require_relative "../lib/scraper_helpers"
  6. require_relative "../lib/log"
  7. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_northernmidlands
  8. URL = "https://northernmidlands.tas.gov.au/planning/development-in-the-northern-midlands/development-applications-2"
  9. DB.ensure_table!(TABLE)
  10. # “DA 2025/00123”, “DA2025/00123”, “Application No. DA 2025/123”
  11. REF_RX1 = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-_.]+)}i
  12. REF_RX2 = %r{\bDA(20\d{2})\s*[-\/]?\s*([0-9]{3,})\b}i
  13. def extract_ref(str)
  14. s = CGI.unescape(str.to_s)
  15. if (m = s.match(REF_RX1))
  16. return "DA #{m[1]} / #{m[2]}"
  17. end
  18. if (m = s.match(REF_RX2))
  19. return "DA #{m[1]} / #{m[2]}"
  20. end
  21. nil
  22. end
  23. DATE_RX = /
  24. (\b\d{1,2}\/\d{1,2}\/\d{2,4}\b|
  25. \b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b|
  26. \b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)
  27. /x
  28. def extract_on_notice_raw(text)
  29. s = text.to_s.gsub(/\s+/, " ")
  30. if (m = s.match(/\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i))
  31. if (d = m[2].match(DATE_RX))
  32. return d[1]
  33. end
  34. end
  35. if (m = s.match(/clos(?:e|ing|es)\s*(on)?\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i))
  36. if (d = m[2].match(DATE_RX))
  37. return d[1]
  38. end
  39. end
  40. if (d = s.match(DATE_RX))
  41. return d[1]
  42. end
  43. ""
  44. end
  45. def nearest_context_text(a)
  46. host = a.ancestors("li, p, div, tr, article").first || a.parent
  47. host ? host.text.to_s.strip.gsub(/\s+/, " ") : ""
  48. end
  49. def parse_items(doc, base_url)
  50. rows = []
  51. # 1) Obvious list items or rows with PDFs or application keywords
  52. anchors = doc.css("a").select { |a|
  53. href = a["href"].to_s
  54. a.text.to_s.strip.match?(/application|permit|planning|advertis/i) || href.downcase.end_with?(".pdf")
  55. }
  56. anchors.each do |a|
  57. href = a["href"].to_s
  58. link_text = a.text.to_s.strip
  59. document_url = abs_url(base_url, href)
  60. ctx = nearest_context_text(a)
  61. # Title to keep, if present
  62. title_reference = link_text.empty? ? ctx[0,200] : link_text
  63. text_for_parse = [link_text, ctx].reject(&:empty?).uniq.join(" — ")
  64. # Address: prefer the link text, else the surrounding text slice
  65. address = if link_text.length >= 6
  66. link_text
  67. else
  68. ctx[0, 140]
  69. end
  70. # Reference from text or file name
  71. ref = extract_ref(text_for_parse) || extract_ref(File.basename(document_url))
  72. # On-notice
  73. on_raw = extract_on_notice_raw(text_for_parse)
  74. on_dt = Util.parse_aus_date(on_raw)
  75. # Description
  76. description = if text_for_parse =~ /proposal\s*[:\-]\s*([^—\-]+)\b/i
  77. $1.strip
  78. else
  79. "Development Application"
  80. end
  81. next if ref.nil? || address.to_s.strip.empty?
  82. rows << {
  83. council_reference: ref,
  84. address: address.to_s.strip,
  85. description: description,
  86. date_received: on_dt,
  87. date_received_raw: on_raw,
  88. document_url: document_url,
  89. title_reference: title_reference
  90. }
  91. end
  92. # 2) If the page uses a two-column details table, pick that up too
  93. doc.css("table").each do |t|
  94. heads = t.css("th").map { |th| th.text.strip.downcase }
  95. next unless heads.any? { |h| h.match?(/application|reference|proposal|address|notice|closing/) }
  96. t.css("tr").each do |tr|
  97. cells = tr.css("td")
  98. next unless cells.length >= 2
  99. row_text = tr.text.to_s.strip.gsub(/\s+/, " ")
  100. ref = extract_ref(row_text)
  101. addr = row_text[/address[:\s]+(.+?)(?:\s{2,}|$)/i, 1] || row_text[0, 140]
  102. on_raw = extract_on_notice_raw(row_text)
  103. on_dt = Util.parse_aus_date(on_raw)
  104. next if ref.nil? || addr.to_s.strip.empty?
  105. rows << {
  106. council_reference: ref,
  107. address: addr.to_s.strip,
  108. description: "Development Application",
  109. date_received: on_dt,
  110. date_received_raw: on_raw,
  111. document_url: "",
  112. title_reference: row_text[0,200]
  113. }
  114. end
  115. end
  116. rows.uniq { |r| [r[:council_reference], r[:address]] }
  117. end
  118. if URL.empty?
  119. Log.warn "scraper", "NORTHERN_MIDLANDS_URL is not set. Example:\n ONLY=northernmidlands NORTHERN_MIDLANDS_URL='https://.../advertised-applications' docker compose run --rm scraper /app/run_all.sh"
  120. exit 0
  121. end
  122. begin
  123. html = if URL.include?("/eservice/")
  124. # Some councils use ePathway, which needs a cookie-warmed session
  125. Http.dorset_session_get(URL)
  126. else
  127. Http.get(URL)
  128. end
  129. rescue StandardError => e
  130. Log.warn "scraper", "Failed to fetch #{URL}: #{e.class} #{e.message}"
  131. exit 1
  132. end
  133. doc = Nokogiri::HTML(html)
  134. items = parse_items(doc, URL)
  135. puts "Found #{items.length} item(s) for #{TABLE}"
  136. items.each do |r|
  137. upsert_and_enrich!(
  138. table: TABLE,
  139. row: {
  140. description: r[:description],
  141. date_received: r[:date_received],
  142. date_received_raw: r[:date_received_raw],
  143. address: r[:address],
  144. council_reference: r[:council_reference],
  145. applicant: "",
  146. owner: ""
  147. },
  148. extras: {
  149. document_url: r[:document_url],
  150. on_notice_to: r[:date_received],
  151. on_notice_to_raw: r[:date_received_raw],
  152. title_reference: r[:title_reference]
  153. }
  154. )
  155. end
  156. puts "Done #{TABLE}."