derwentvalley.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Derwent Valley Council — Development Applications being advertised
  2. # Primary list: https://www.derwentvalley.tas.gov.au/home/card-listing/development-applications
  3. # Fallback list (Public Notice posts): https://www.derwentvalley.tas.gov.au/home/latest-news?f.News+category%7CnewsCategory=Public+Notice
  4. require "nokogiri"
  5. require_relative "../lib/http"
  6. require_relative "../lib/util"
  7. require_relative "../lib/scraper_helpers"
  8. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_derwentvalley
  9. LIST_URL = "https://www.derwentvalley.tas.gov.au/home/card-listing/development-applications"
  10. NEWS_URL = "https://www.derwentvalley.tas.gov.au/home/latest-news?f.News+category%7CnewsCategory=Public+Notice"
  11. DB.ensure_table!(TABLE)
  12. # Common reference forms: "DA 2025/097"
  13. REF_RX = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-_.]+)}i
  14. def extract_ref(s)
  15. t = s.to_s
  16. if (m = t.match(REF_RX))
  17. return "DA #{m[1]} / #{m[2]}"
  18. end
  19. nil
  20. end
  21. def extract_date_token(s)
  22. text = s.to_s
  23. return $1 if text =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  24. return $1 if text =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  25. return $1 if text =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  26. ""
  27. end
  28. def extract_on_notice_raw(text)
  29. s = text.to_s.gsub(/\s+/, " ")
  30. # Look for wording like "Submissions must be received by ...", "close on ...", "on notice until ..."
  31. if s =~ /(submissions?|representations?)\s+(must\s+be\s+)?(received|made|close|closing)\s+(by|on)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  32. d = extract_date_token($5)
  33. return d unless d.empty?
  34. end
  35. if s =~ /\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  36. d = extract_date_token($2)
  37. return d unless d.empty?
  38. end
  39. extract_date_token(s)
  40. end
  41. def parse_detail(url)
  42. html = Http.get(url)
  43. doc = Nokogiri::HTML(html)
  44. title = doc.at_css("h1, .entry-title")&.text&.strip.to_s
  45. body_text = doc.at_css("main")&.text.to_s
  46. body_text = doc.text.to_s if body_text.strip.empty?
  47. council_reference = extract_ref(title) || extract_ref(body_text)
  48. # Address often sits in the title after " - "
  49. address = if title.include?(" - ")
  50. title.split(" - ", 2)[1].to_s.strip
  51. else
  52. # Fallback: first line with a number and street
  53. line = body_text.split(/\n/).find { |l| l =~ /\d{1,4}\s+\S+/ }
  54. line.to_s.strip
  55. end
  56. address = title if address.to_s.strip.empty?
  57. pdf_a = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")
  58. pdf = pdf_a ? abs_url(url, pdf_a["href"].to_s) : ""
  59. on_raw = extract_on_notice_raw(body_text)
  60. on_dt = Util.parse_aus_date(on_raw)
  61. return nil if council_reference.to_s.strip.empty? || address.to_s.strip.empty?
  62. {
  63. council_reference: council_reference,
  64. address: address,
  65. description: "Development Application",
  66. date_received_raw: on_raw,
  67. date_received: on_dt,
  68. document_url: pdf,
  69. title_reference: title
  70. }
  71. end
  72. def detail_links_from_list(list_url)
  73. html = Http.get(list_url)
  74. doc = Nokogiri::HTML(html)
  75. # Cards or list items link to detail posts
  76. links = doc.css("a").map { |a|
  77. href = a["href"].to_s
  78. next if href.empty? || href.start_with?("#")
  79. abs_url(list_url, href)
  80. }.compact.uniq
  81. # Keep obvious news or notice items
  82. links.select { |u|
  83. u.include?("/home/latest-news/") || u.include?("/news/") || u =~ /application-for-planning-approval/i
  84. }
  85. end
  86. def detail_links_from_news(news_url)
  87. html = Http.get(news_url)
  88. doc = Nokogiri::HTML(html)
  89. doc.css("a").map { |a|
  90. href = a["href"].to_s
  91. next if href.empty? || href.start_with?("#")
  92. u = abs_url(news_url, href)
  93. u if u =~ /application-for-planning-approval/i
  94. }.compact.uniq
  95. end
  96. links = []
  97. begin
  98. links = detail_links_from_list(LIST_URL)
  99. rescue => e
  100. warn "List fetch failed, will try news listing: #{e.class} #{e.message}"
  101. end
  102. if links.empty?
  103. begin
  104. links = detail_links_from_news(NEWS_URL)
  105. rescue => e
  106. warn "News fetch failed: #{e.class} #{e.message}"
  107. end
  108. end
  109. links.uniq!
  110. puts "Found #{links.length} candidate link(s) for #{TABLE}"
  111. saved = 0
  112. links.each do |u|
  113. begin
  114. item = parse_detail(u)
  115. rescue => e
  116. warn "Skip #{u}: #{e.class} #{e.message}"
  117. next
  118. end
  119. next unless item
  120. upsert_and_enrich!(
  121. table: TABLE,
  122. row: {
  123. description: item[:description],
  124. date_received: item[:date_received],
  125. date_received_raw: item[:date_received_raw],
  126. address: item[:address],
  127. council_reference: item[:council_reference],
  128. applicant: "",
  129. owner: ""
  130. },
  131. extras: {
  132. document_url: item[:document_url],
  133. on_notice_to: item[:date_received],
  134. on_notice_to_raw: item[:date_received_raw],
  135. title_reference: item[:title_reference]
  136. }
  137. )
  138. saved += 1
  139. end
  140. puts "Done #{TABLE}. Saved #{saved} item(s)."