derwentvalley.rb 4.7 KB

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