georgetown.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # George Town Council — Development Applications (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 from filename: da_georgetown
  8. URL = "https://georgetown.tas.gov.au/development-applications/"
  9. DB.ensure_table!(TABLE)
  10. # Optional: keep the document link with each row
  11. begin
  12. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url VARCHAR(1024) NULL")
  13. rescue => e
  14. warn "document_url add skipped: #{e.class} #{e.message}"
  15. end
  16. def text_or(node, fallback = "")
  17. node ? node.text.strip : fallback
  18. end
  19. def abs_url(base, href)
  20. return "" if href.to_s.strip.empty?
  21. URI.join(base, href).to_s rescue href.to_s
  22. end
  23. # Try to pull a council reference from common places
  24. REF_RX = %r{(DA|APP|APPLICATION|PLA)\s*([0-9]{4})\s*[/\-]?\s*([A-Za-z0-9\-_.]{2,})}i
  25. def extract_reference(str)
  26. s = str.to_s
  27. if (m = s.match(REF_RX))
  28. return "DA #{m[2]} / #{m[3]}"
  29. end
  30. # Compact like DA202500123
  31. if (m = s.match(/\bDA(20\d{2})(\d{3,})\b/i))
  32. return "DA #{m[1]} / #{m[2]}"
  33. end
  34. nil
  35. end
  36. html = Http.get(URL)
  37. doc = Nokogiri::HTML(html)
  38. # Most items on this page are shown as “cards” with a small details table inside
  39. cards = doc.css(".card, .entry-content .wp-block-group, .entry-content .content-block, .entry-content .notice, .entry-content")
  40. items = []
  41. cards.each do |card|
  42. table = card.at_css("table")
  43. next unless table
  44. rows = table.css("tr")
  45. kv = {}
  46. rows.each do |tr|
  47. cells = tr.css("th, td")
  48. next if cells.empty?
  49. # Expect two-column label/value rows; be defensive about order
  50. key = cells[0]&.text&.strip.to_s
  51. val = cells[1]&.text&.strip.to_s
  52. if cells.size >= 2 && !key.empty?
  53. kv[key] = val
  54. end
  55. end
  56. next if kv.empty?
  57. # Pull useful fields by fuzzy key match
  58. find = ->(needle_regex) {
  59. pair = kv.find { |k, _| k =~ needle_regex }
  60. pair ? pair[1] : ""
  61. }
  62. application_id = find.call(/^(Application\s*(ID|No|Number)|Ref)/i)
  63. address = find.call(/(Address|Property)/i)
  64. proposal = find.call(/(Proposal|Description)/i)
  65. app_date_raw = find.call(/(Application\s*Date|Date\s*Lodged|Date\s*Received)/i)
  66. closing_date_raw = find.call(/(On\s*Notice\s*(to|until)|Closing\s*Date|Closes)/i)
  67. # Document link if present in the table or surrounding block
  68. link = table.at_css("a[href$='.pdf'], a[href*='.pdf?']") || card.at_css("a[href$='.pdf'], a[href*='.pdf?']")
  69. document_url = link ? abs_url(URL, link["href"]) : ""
  70. # Council reference priority: Application ID, then text refs, then file name
  71. council_reference =
  72. application_id.to_s.strip
  73. council_reference = extract_reference(proposal) if council_reference.to_s.empty?
  74. council_reference ||= extract_reference(File.basename(document_url)) || extract_reference(address) || ""
  75. # Pick a date to store: prefer application date, else closing/on-notice
  76. date_received = Util.parse_aus_date(app_date_raw)
  77. date_received_raw = app_date_raw.to_s.strip
  78. if date_received.nil? && !closing_date_raw.to_s.strip.empty?
  79. date_received = Util.parse_aus_date(closing_date_raw)
  80. date_received_raw = closing_date_raw
  81. end
  82. # Minimal required fields
  83. address = address.to_s.strip
  84. next if address.empty? || council_reference.empty?
  85. items << {
  86. description: proposal.to_s.strip,
  87. date_received: date_received,
  88. date_received_raw: date_received_raw,
  89. address: address,
  90. council_reference: council_reference,
  91. document_url: document_url
  92. }
  93. end
  94. puts "Found #{items.length} items for #{TABLE}"
  95. items.each do |row|
  96. DB.upsert(TABLE, {
  97. description: row[:description],
  98. date_received: row[:date_received],
  99. date_received_raw: row[:date_received_raw],
  100. address: row[:address],
  101. council_reference: row[:council_reference],
  102. applicant: "",
  103. owner: ""
  104. })
  105. enrich_after_upsert!(
  106. table: TABLE,
  107. council_reference: council_reference,
  108. address: address
  109. )
  110. # keep the PDF link if the column exists
  111. begin
  112. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ? WHERE council_reference = ? AND address = ?")
  113. upd.execute(row[:document_url], row[:council_reference], row[:address])
  114. rescue Mysql2::Error => e
  115. warn "[georgetown] db update skipped for #{row[:council_reference]}: #{e.message}"
  116. end
  117. puts "Upserted #{row[:council_reference]} -> #{row[:address]}"
  118. end
  119. puts "Done #{TABLE}."