westtamar.rb 4.0 KB

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