georgetown.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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|Location)/i)
  50. proposal = find.call(/(Proposal|Description)/i)
  51. applicant = find.call(/(Applicant)/i)
  52. title_ref = find.call(/(Title\s*[Rr]ef)/i)
  53. app_date_raw = find.call(/(Application\s*Date|Date\s*Lodged|Date\s*Received|Opening\s*Date)/i)
  54. closing_date_raw = find.call(/(On\s*Notice\s*(to|until)|Closing\s*Date|Closes)/i)
  55. # Document link if present in the table or surrounding block
  56. link = table.at_css("a[href$='.pdf'], a[href*='.pdf?']") || card.at_css("a[href$='.pdf'], a[href*='.pdf?']")
  57. document_url = link ? abs_url(URL, link["href"]) : ""
  58. # Council reference priority: Application ID, then text refs, then file name
  59. council_reference =
  60. application_id.to_s.strip
  61. council_reference = extract_reference(proposal) if council_reference.to_s.empty?
  62. council_reference ||= extract_reference(File.basename(document_url)) || extract_reference(address) || ""
  63. # Pick a date to store: prefer application date, else closing/on-notice
  64. date_received = Util.parse_aus_date(app_date_raw)
  65. date_received_raw = app_date_raw.to_s.strip
  66. if date_received.nil? && !closing_date_raw.to_s.strip.empty?
  67. date_received = Util.parse_aus_date(closing_date_raw)
  68. date_received_raw = closing_date_raw
  69. end
  70. # Minimal required fields
  71. address = address.to_s.strip
  72. next if address.empty? || council_reference.empty?
  73. on_notice_to = Util.parse_aus_date(closing_date_raw)
  74. on_notice_to_raw = closing_date_raw.to_s.strip
  75. items << {
  76. description: proposal.to_s.strip,
  77. date_received: date_received,
  78. date_received_raw: date_received_raw,
  79. on_notice_to: on_notice_to,
  80. on_notice_to_raw: on_notice_to_raw,
  81. address: address,
  82. council_reference: council_reference,
  83. applicant: applicant.to_s.strip,
  84. title_reference: title_ref.to_s.strip,
  85. document_url: document_url
  86. }
  87. end
  88. puts "Found #{items.length} items for #{TABLE}"
  89. items.each do |row|
  90. upsert_and_enrich!(
  91. table: TABLE,
  92. row: {
  93. description: row[:description],
  94. date_received: row[:date_received],
  95. date_received_raw: row[:date_received_raw],
  96. on_notice_to: row[:on_notice_to],
  97. on_notice_to_raw: row[:on_notice_to_raw],
  98. address: row[:address],
  99. council_reference: row[:council_reference],
  100. applicant: row[:applicant],
  101. owner: "",
  102. title_reference: row[:title_reference],
  103. document_url: row[:document_url]
  104. }
  105. )
  106. end
  107. puts "Done #{TABLE}."