westtamar.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # West Tamar Council — Advertised Planning Applications
  2. require "nokogiri"
  3. require_relative "../lib/http"
  4. require_relative "../lib/util"
  5. require_relative "../lib/scraper_helpers"
  6. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh -> da_westtamar
  7. URL = "https://www.wtc.tas.gov.au/advertised-planning-applications/"
  8. DB.ensure_table!(TABLE)
  9. REF_RX_SLASH = %r{\bDA\s*(20\d{2})\s*/\s*([A-Za-z0-9\-_.]+)}i
  10. REF_RX_HYPHEN = %r{\bDA\s*(\d{1,4})\s*-\s*(20\d{2})\b}i
  11. def extract_ref(text)
  12. s = text.to_s
  13. if (m = s.match(REF_RX_SLASH))
  14. return "DA #{m[1]} / #{m[2]}"
  15. end
  16. if (m = s.match(REF_RX_HYPHEN))
  17. return "DA #{m[2]} / #{m[1]}"
  18. end
  19. if (m = s.match(/\bDA(20\d{2})(\d{3,})\b/i))
  20. return "DA #{m[1]} / #{m[2]}"
  21. end
  22. nil
  23. end
  24. def extract_date_like(str)
  25. s = str.to_s
  26. return $1 if s =~ /(\b\d{1,2}\/\d{1,2}\/\d{2,4}\b)/
  27. return $1 if s =~ /(\b\d{1,2}\s+[A-Za-z]{3,}\s+\d{4}\b)/
  28. return $1 if s =~ /(\b[A-Za-z]{3,}\s+\d{1,2},?\s+\d{4}\b)/
  29. ""
  30. end
  31. def extract_on_notice_raw(text)
  32. s = text.to_s.gsub(/\s+/, " ")
  33. if s =~ /\bon\s*notice\s*(until|to)\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  34. d = extract_date_like($2)
  35. return d unless d.empty?
  36. end
  37. if s =~ /clos(?:e|ing|es)\s*(on)?\s*[:\-]?\s*([A-Za-z0-9\/ ,]+)/i
  38. d = extract_date_like($2)
  39. return d unless d.empty?
  40. end
  41. extract_date_like(s)
  42. end
  43. def parse_detail(url)
  44. html = Http.get(url)
  45. doc = Nokogiri::HTML(html)
  46. # Try two-column detail table first
  47. kv = {}
  48. doc.css("table tr").each do |tr|
  49. cells = tr.css("th, td")
  50. next unless cells.length >= 2
  51. key = cells[0].text.strip
  52. val = cells[1].text.strip
  53. kv[key] = val unless key.empty?
  54. end
  55. find = ->(rx) { kv.find { |k,_| k =~ rx }&.last.to_s.strip }
  56. council_reference = find.call(/(Application\s*(No|Number|ID)|Reference)/i)
  57. address = find.call(/(Address|Location|Property)/i)
  58. description = find.call(/(Proposal|Description)/i)
  59. on_notice_raw = find.call(/(On\s*Notice\s*(until|to)|Closing\s*Date|Closes)/i)
  60. on_notice = Util.parse_aus_date(on_notice_raw)
  61. title_reference = doc.at_css("h1, .entry-title")&.text&.strip.to_s
  62. # Fallbacks from page text if labels are missing
  63. if council_reference.empty?
  64. council_reference = extract_ref(title_reference) || extract_ref(doc.text)
  65. end
  66. address = title_reference if address.empty?
  67. description = "Development Application" if description.to_s.strip.empty?
  68. if on_notice.nil?
  69. guess = extract_on_notice_raw(doc.text)
  70. on_notice = Util.parse_aus_date(guess)
  71. on_notice_raw = guess if on_notice
  72. end
  73. pdf = doc.at_css("a[href$='.pdf'], a[href*='.pdf?']")&.[]("href")
  74. document_url = pdf ? abs_url(url, pdf) : ""
  75. return nil if council_reference.empty? || address.empty?
  76. {
  77. council_reference: council_reference,
  78. address: address,
  79. description: description,
  80. date_received: on_notice,
  81. date_received_raw: on_notice_raw.to_s,
  82. document_url: document_url,
  83. title_reference: title_reference
  84. }
  85. end
  86. list_html = Http.get(URL)
  87. list_doc = Nokogiri::HTML(list_html)
  88. detail_links = list_doc.css("article h2 a, .entry-content a").map { |a|
  89. href = a["href"].to_s
  90. next if href.strip.empty? || href.start_with?("#")
  91. abs_url(URL, href)
  92. }.compact.uniq
  93. puts "Found #{detail_links.size} candidate link(s) for #{TABLE}"
  94. saved = 0
  95. detail_links.each do |u|
  96. begin
  97. item = parse_detail(u)
  98. rescue => e
  99. warn "Skip #{u}: #{e.class} #{e.message}"
  100. next
  101. end
  102. next unless item
  103. upsert_and_enrich!(
  104. table: TABLE,
  105. row: {
  106. description: item[:description],
  107. date_received: item[:date_received],
  108. date_received_raw: item[:date_received_raw],
  109. address: item[:address],
  110. council_reference: item[:council_reference],
  111. applicant: "",
  112. owner: ""
  113. },
  114. extras: {
  115. document_url: item[:document_url],
  116. on_notice_to: item[:date_received],
  117. on_notice_to_raw: item[:date_received_raw],
  118. title_reference: item[:title_reference]
  119. }
  120. )
  121. saved += 1
  122. end
  123. puts "Done #{TABLE}. Saved #{saved} item(s)."