glamorgan.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Glamorgan Spring Bay Council — Public Notices scraper (site page, not PlanBuild)
  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 sets from filename: da_glamorgan
  7. URL = "https://gsbc.tas.gov.au/services-facilities/public-notices/"
  8. DB.ensure_table!(TABLE)
  9. # Try to extract a reference from visible text or file names
  10. REF_RX = %r{(DA|PLA|APP|APPLICATION)\s*([0-9]{4})\s*/\s*([A-Za-z0-9\-_.]+)}i
  11. def extract_reference(str)
  12. s = str.to_s
  13. if (m = s.match(REF_RX))
  14. return "#{m[1].upcase} #{m[2]} / #{m[3]}"
  15. end
  16. nil
  17. end
  18. html = Http.get(URL)
  19. doc = Nokogiri::HTML(html)
  20. # Find a table that looks like the notices list
  21. tables = doc.css("table")
  22. table = tables.find do |t|
  23. headers = t.css("thead th").map { |th| th.text.strip.downcase }
  24. headers.empty? ? t.css("tbody tr:first-child td").size >= 4 : headers.size >= 4
  25. end
  26. unless table
  27. puts "No notices table found on #{URL}"
  28. exit 0
  29. end
  30. rows = table.css("tbody tr")
  31. puts "Found #{rows.length} rows for #{TABLE}"
  32. saved = 0
  33. rows.each_with_index do |row, idx|
  34. tds = row.css("td")
  35. next if tds.empty?
  36. # Expected columns on this page:
  37. # 0: Description, 1: Address, 2: Application date, 3: On notice to, 4: Document link
  38. description = text_or(tds[0])
  39. address = text_or(tds[1])
  40. application_raw = text_or(tds[2])
  41. on_notice_to_raw = text_or(tds[3])
  42. link_el = tds[4]&.at_css("a")
  43. document_url = link_el ? abs_url(URL, link_el["href"]) : ""
  44. date_received = Util.parse_aus_date(application_raw)
  45. on_notice_to = Util.parse_aus_date(on_notice_to_raw)
  46. # Reference: try text, then file name, then a stable fallback
  47. council_reference = extract_reference(description) ||
  48. extract_reference(address) ||
  49. extract_reference(File.basename(document_url)) ||
  50. begin
  51. date_key = date_received || on_notice_to
  52. base = [date_key&.strftime("%Y%m%d"), address.gsub(/\s+/, " ")[0,40]].compact.join(" ")
  53. base.empty? ? "GSBC-ROW-#{idx+1}" : "GSBC #{base}"
  54. end
  55. next if address.empty? || council_reference.empty?
  56. upsert_and_enrich!(
  57. table: TABLE,
  58. row: {
  59. description: description,
  60. date_received: date_received,
  61. date_received_raw: application_raw,
  62. address: address,
  63. council_reference: council_reference,
  64. applicant: "",
  65. owner: ""
  66. },
  67. extras: { document_url: document_url }
  68. )
  69. saved += 1
  70. end
  71. puts "Done #{TABLE}. Saved #{saved} item(s)."