georgetown.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # George Town Council — Development Applications (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_georgetown
  7. URL = "https://georgetown.tas.gov.au/development-applications/"
  8. DB.ensure_table!(TABLE)
  9. # Try to pull a council reference from common places
  10. REF_RX = %r{(DA|APP|APPLICATION|PLA)\s*([0-9]{4})\s*[/\-]?\s*([A-Za-z0-9\-_.]{2,})}i
  11. def extract_reference(str)
  12. s = str.to_s
  13. if (m = s.match(REF_RX))
  14. return "DA #{m[2]} / #{m[3]}"
  15. end
  16. # Compact like DA202500123
  17. if (m = s.match(/\bDA(20\d{2})(\d{3,})\b/i))
  18. return "DA #{m[1]} / #{m[2]}"
  19. end
  20. nil
  21. end
  22. html = Http.get(URL)
  23. doc = Nokogiri::HTML(html)
  24. # Most items on this page are shown as "cards" with a small details table inside
  25. cards = doc.css(".card, .entry-content .wp-block-group, .entry-content .content-block, .entry-content .notice, .entry-content")
  26. items = []
  27. cards.each do |card|
  28. table = card.at_css("table")
  29. next unless table
  30. rows = table.css("tr")
  31. kv = {}
  32. rows.each do |tr|
  33. cells = tr.css("th, td")
  34. next if cells.empty?
  35. # Expect two-column label/value rows; be defensive about order
  36. key = cells[0]&.text&.strip.to_s
  37. val = cells[1]&.text&.strip.to_s
  38. if cells.size >= 2 && !key.empty?
  39. kv[key] = val
  40. end
  41. end
  42. next if kv.empty?
  43. # Pull useful fields by fuzzy key match
  44. find = ->(needle_regex) {
  45. pair = kv.find { |k, _| k =~ needle_regex }
  46. pair ? pair[1] : ""
  47. }
  48. application_id = find.call(/^(Application\s*(ID|No|Number)|Ref)/i)
  49. address = find.call(/(Address|Property)/i)
  50. proposal = find.call(/(Proposal|Description)/i)
  51. app_date_raw = find.call(/(Application\s*Date|Date\s*Lodged|Date\s*Received)/i)
  52. closing_date_raw = find.call(/(On\s*Notice\s*(to|until)|Closing\s*Date|Closes)/i)
  53. # Document link if present in the table or surrounding block
  54. link = table.at_css("a[href$='.pdf'], a[href*='.pdf?']") || card.at_css("a[href$='.pdf'], a[href*='.pdf?']")
  55. document_url = link ? abs_url(URL, link["href"]) : ""
  56. # Council reference priority: Application ID, then text refs, then file name
  57. council_reference =
  58. application_id.to_s.strip
  59. council_reference = extract_reference(proposal) if council_reference.to_s.empty?
  60. council_reference ||= extract_reference(File.basename(document_url)) || extract_reference(address) || ""
  61. # Pick a date to store: prefer application date, else closing/on-notice
  62. date_received = Util.parse_aus_date(app_date_raw)
  63. date_received_raw = app_date_raw.to_s.strip
  64. if date_received.nil? && !closing_date_raw.to_s.strip.empty?
  65. date_received = Util.parse_aus_date(closing_date_raw)
  66. date_received_raw = closing_date_raw
  67. end
  68. # Minimal required fields
  69. address = address.to_s.strip
  70. next if address.empty? || council_reference.empty?
  71. items << {
  72. description: proposal.to_s.strip,
  73. date_received: date_received,
  74. date_received_raw: date_received_raw,
  75. address: address,
  76. council_reference: council_reference,
  77. document_url: document_url
  78. }
  79. end
  80. puts "Found #{items.length} items for #{TABLE}"
  81. items.each do |row|
  82. upsert_and_enrich!(
  83. table: TABLE,
  84. row: {
  85. description: row[:description],
  86. date_received: row[:date_received],
  87. date_received_raw: row[:date_received_raw],
  88. address: row[:address],
  89. council_reference: row[:council_reference],
  90. applicant: "",
  91. owner: ""
  92. },
  93. extras: { document_url: row[:document_url] }
  94. )
  95. end
  96. puts "Done #{TABLE}."