kingborough.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Kingborough Council — Planning Notices (site page, not PlanBuild)
  2. require "nokogiri"
  3. require_relative "../lib/http"
  4. require_relative "../lib/db"
  5. require_relative "../lib/util"
  6. require_relative "../lib/enrich"
  7. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets this from filename: da_kingborough
  8. URL = "https://www.kingborough.tas.gov.au/development/planning-notices/"
  9. DB.ensure_table!(TABLE)
  10. # Optional columns for extras used on this site
  11. begin
  12. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url TEXT NULL")
  13. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to DATE NULL")
  14. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS on_notice_to_raw VARCHAR(50) NULL")
  15. rescue => e
  16. warn "Optional column add skipped: #{e.class} #{e.message}"
  17. end
  18. def abs_url(base, href)
  19. return "" if href.to_s.strip.empty?
  20. URI.join(base, href).to_s rescue href.to_s
  21. end
  22. html = Http.get(URL)
  23. doc = Nokogiri::HTML(html)
  24. rows = doc.css("#list tbody tr")
  25. puts "Found #{rows.length} rows for #{TABLE}"
  26. saved = 0
  27. rows.each_with_index do |row, idx|
  28. tds = row.css("td")
  29. next if tds.length < 5
  30. council_reference = tds[0].text.strip
  31. address = tds[1].text.strip
  32. date_received_raw = tds[2].text.strip
  33. on_notice_to_raw = tds[3].text.strip
  34. description = tds[4].text.strip
  35. # All links in the row (often PDFs). Join if there are multiple.
  36. links = row.css("td a").map { |a| abs_url(URL, a["href"]) }.reject(&:empty?)
  37. document_url = links.join(", ")
  38. date_received = Util.parse_aus_date(date_received_raw)
  39. on_notice_to = Util.parse_aus_date(on_notice_to_raw)
  40. # Need a reference and an address to store a row
  41. next if council_reference.empty? || address.empty?
  42. DB.upsert(TABLE, {
  43. description: description,
  44. date_received: date_received,
  45. date_received_raw: date_received_raw,
  46. address: address,
  47. council_reference: council_reference,
  48. applicant: "",
  49. owner: ""
  50. })
  51. enrich_after_upsert!(
  52. table: TABLE,
  53. council_reference: council_reference,
  54. address: address
  55. )
  56. # Save the extras
  57. begin
  58. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ?, on_notice_to = ?, on_notice_to_raw = ? WHERE council_reference = ? AND address = ?")
  59. upd.execute(document_url, on_notice_to, on_notice_to_raw, council_reference, address)
  60. rescue => e
  61. warn "Extra field update skipped for #{council_reference}: #{e.class} #{e.message}"
  62. end
  63. puts "Upserted #{council_reference} -> #{address}"
  64. saved += 1
  65. end
  66. puts "Done #{TABLE}. Saved #{saved} item(s)."