southernmidlands.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Southern Midlands Council — Advertised Development Applications
  2. require "nokogiri"
  3. require "uri"
  4. require "cgi"
  5. require_relative "../lib/http"
  6. require_relative "../lib/db"
  7. require_relative "../lib/util"
  8. require_relative "../lib/enrich"
  9. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_southernmidlands
  10. LIST_URL = "https://www.southernmidlands.tas.gov.au/advertised-development-applications/"
  11. DB.ensure_table!(TABLE)
  12. # Optional extras used on this site
  13. begin
  14. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url TEXT NULL")
  15. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to DATE NULL")
  16. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to_raw VARCHAR(80) NULL")
  17. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS title_reference TEXT NULL")
  18. rescue => e
  19. warn "Optional column add skipped: #{e.class} #{e.message}"
  20. end
  21. def abs_url(base, href)
  22. return "" if href.to_s.strip.empty?
  23. URI.join(base, href).to_s rescue href.to_s
  24. end
  25. # Reference forms like "DA 2025/00123", "DA2025/00123"
  26. REF_RX1 = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-\._]+)}i
  27. REF_RX2 = %r{\bDA(20\d{2})\s*[-\/]?\s*([0-9]{3,})\b}i
  28. def extract_ref(text)
  29. s = text.to_s
  30. if (m = s.match(REF_RX1))
  31. return "DA #{m[1]} / #{m[2]}"
  32. end
  33. if (m = s.match(REF_RX2))
  34. return "DA #{m[1]} / #{m[2]}"
  35. end
  36. nil
  37. end
  38. def extract_date_like(str)
  39. s = str.to_s
  40. return $1 if s =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  41. return $1 if s =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  42. return $1 if s =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  43. ""
  44. end
  45. def extract_on_notice_raw(text)
  46. s = text.to_s.gsub(/\s+/, " ")
  47. if s =~ /\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  48. t = $2
  49. d = extract_date_like(t)
  50. return d unless d.empty?
  51. end
  52. if s =~ /clos(?:e|ing|es)\s*(on)?\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  53. t = $2
  54. d = extract_date_like(t)
  55. return d unless d.empty?
  56. end
  57. extract_date_like(s)
  58. end
  59. def first_nonempty_text_after(node, max_hops: 12)
  60. sib = node
  61. max_hops.times do
  62. sib = sib.next_element
  63. break if sib.nil?
  64. t = sib.text.to_s.strip.gsub(/\s+/, " ")
  65. return t unless t.empty?
  66. end
  67. ""
  68. end
  69. # Get all application detail links from the list page
  70. list_html = Http.get(LIST_URL)
  71. list_doc = Nokogiri::HTML(list_html)
  72. # Southern Midlands lists items as articles or grouped blocks. Collect obvious links.
  73. detail_links = list_doc.css("article .content h2 a, article h2 a, .entry-content a").map { |a|
  74. href = a["href"].to_s
  75. next if href.strip.empty?
  76. next if href.start_with?("#")
  77. abs_url(LIST_URL, href)
  78. }.compact.uniq
  79. puts "Found #{detail_links.size} candidate link(s) for #{TABLE}"
  80. saved = 0
  81. detail_links.each do |url|
  82. begin
  83. html = Http.get(url)
  84. rescue => e
  85. warn "Skip #{url}: #{e.class} #{e.message}"
  86. next
  87. end
  88. doc = Nokogiri::HTML(html)
  89. # Title often contains address or reference
  90. title_reference = doc.at_css("h1, .entry-title")&.text&.strip.to_s
  91. # Try to find a details table or labeled rows
  92. kv = {}
  93. doc.css("table tr").each do |tr|
  94. cells = tr.css("th, td")
  95. next unless cells.length >= 2
  96. key = cells[0].text.strip
  97. val = cells[1].text.strip
  98. kv[key] = val unless key.empty?
  99. end
  100. find = ->(rx) {
  101. pair = kv.find { |k, _| k =~ rx }
  102. pair ? pair[1] : ""
  103. }
  104. # Fields by label when present
  105. council_reference = find.call(/(Application\s*(No|Number|ID)|Reference)/i)
  106. address = find.call(/(Address|Location|Property)/i)
  107. description = find.call(/(Proposal|Description)/i)
  108. on_notice_raw = find.call(/(On\s*Notice\s*(until|to)|Closing\s*Date|Closes)/i)
  109. # Fallbacks from free text around the title
  110. if council_reference.to_s.strip.empty?
  111. council_reference = extract_ref(title_reference) || extract_ref(doc.text)
  112. end
  113. address = title_reference if address.to_s.strip.empty?
  114. if description.to_s.strip.empty?
  115. # Take the first non-empty paragraph after the title
  116. h = doc.at_css("h1, .entry-title")
  117. description = if h then first_nonempty_text_after(h) else "" end
  118. description = "Development Application" if description.empty?
  119. end
  120. if on_notice_raw.to_s.strip.empty?
  121. on_notice_raw = extract_on_notice_raw(doc.text)
  122. end
  123. on_notice = Util.parse_aus_date(on_notice_raw)
  124. # Grab a PDF link if present
  125. pdf = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")&.[]("href")
  126. document_url = pdf ? abs_url(url, pdf) : ""
  127. # Minimal required fields
  128. council_reference = council_reference.to_s.strip
  129. address = address.to_s.strip
  130. next if council_reference.empty? || address.empty?
  131. # Store on_notice in the DATE column for consistency with your other site scrapers
  132. DB.upsert(TABLE, {
  133. description: description,
  134. date_received: on_notice,
  135. date_received_raw: on_notice_raw.to_s,
  136. address: address,
  137. council_reference: council_reference,
  138. applicant: "",
  139. owner: ""
  140. })
  141. enrich_after_upsert!(
  142. table: TABLE,
  143. council_reference: council_reference,
  144. address: address
  145. )
  146. begin
  147. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ?, on_notice_to = ?, on_notice_to_raw = ?, title_reference = ? WHERE council_reference = ? AND address = ?")
  148. upd.execute(document_url, on_notice, on_notice_raw.to_s, title_reference, council_reference, address)
  149. rescue => e
  150. warn "Extras update skipped for #{council_reference}: #{e.class} #{e.message}"
  151. end
  152. puts "Upserted #{council_reference} -> #{address}"
  153. saved += 1
  154. end
  155. puts "Done #{TABLE}. Saved #{saved} item(s)."