glenorchy.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Glenorchy City Council – Planning Applications (site page, not PlanBuild)
  2. require "nokogiri"
  3. require "date"
  4. require_relative "../lib/http"
  5. require_relative "../lib/db"
  6. require_relative "../lib/util"
  7. require_relative "../lib/enrich"
  8. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets from filename: da_glenorchy
  9. URL = "https://www.gcc.tas.gov.au/services/planning-and-building/planning-and-development/planning-applications/"
  10. DOWNLOAD_ATTACHMENTS = ENV["DOWNLOAD_ATTACHMENTS"] == "1"
  11. DOWNLOAD_DIR = ENV["DOWNLOAD_DIR"] || "/app/downloads"
  12. DB.ensure_table!(TABLE)
  13. ensure_extra_columns!(TABLE)
  14. # Optional: keep the document link with each row (adds a column if missing)
  15. begin
  16. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url VARCHAR(1024) NULL")
  17. rescue => e
  18. warn "Could not add document_url column: #{e.class} #{e.message}"
  19. end
  20. def text_or(node, default = "")
  21. node ? node.text.strip : default
  22. end
  23. def abs_url(href)
  24. return "" if href.to_s.strip.empty?
  25. URI.join(URL, href).to_s
  26. rescue URI::InvalidURIError
  27. href.to_s
  28. end
  29. def safe_name(s) = s.to_s.gsub(/[^\w\-.]+/, "_")
  30. def filename_from_response(res, fallback_url)
  31. cd = res["content-disposition"].to_s
  32. name =
  33. if cd =~ /filename\*?=(?:UTF-8''|")?([^\";]+)/
  34. $1
  35. else
  36. File.basename(URI.parse(fallback_url).path)
  37. end
  38. name = "document.pdf" if name.to_s.strip.empty?
  39. name += ".pdf" unless name.downcase.end_with?(".pdf")
  40. safe_name(name)
  41. end
  42. def download_pdf(doc_url, council_reference)
  43. return "" unless DOWNLOAD_ATTACHMENTS && !doc_url.to_s.strip.empty?
  44. begin
  45. u = abs_url(doc_url)
  46. res = Http.request(URI.parse(u), headers: {}, jar: {}, referer: URL)
  47. raise "HTTP #{res.code}" unless res.is_a?(Net::HTTPSuccess)
  48. dir = File.join(DOWNLOAD_DIR, "glenorchy", safe_name(council_reference))
  49. FileUtils.mkdir_p(dir)
  50. fname = filename_from_response(res, u)
  51. path = File.join(dir, fname)
  52. File.binwrite(path, res.body.to_s)
  53. puts " saved #{path}"
  54. # web-facing relative path (match your nginx/apache mapping)
  55. "/downloads/glenorchy/#{safe_name(council_reference)}/#{fname}"
  56. rescue => e
  57. warn "Download failed for #{doc_url}: #{e.class} #{e.message}"
  58. ""
  59. end
  60. end
  61. html = Http.get(URL)
  62. doc = Nokogiri::HTML(html)
  63. # Cards on this page use “content-block” classes (WordPress pattern).
  64. cards = doc.css(".content-block, .content-block--featured")
  65. puts "Found #{cards.length} items for #{TABLE}"
  66. saved = 0
  67. cards.each do |card|
  68. # Title / address: try specific title element, then fall back to header text or first link text
  69. title_el = card.at_css(".content-block__title, .content-block__heading, h3, h2, a")
  70. address = text_or(title_el)
  71. # Closing date appears like: "Closes: 29 August 2025"
  72. date_el = card.at_css(".content-block__date")
  73. on_notice_to_raw = text_or(date_el).sub(/^Closes:\s*/i, "")
  74. on_notice_to = Util.parse_aus_date(on_notice_to_raw)
  75. date_received = Date.today
  76. # Short description paragraph
  77. desc_el = card.at_css(".content-block__description p, .content-block__description")
  78. description = text_or(desc_el)
  79. # Document link button (often a PDF)
  80. doc_link = card.at_css(".content-block__button a, a[href$='.pdf']")
  81. document_url = doc_link ? doc_link["href"].to_s.strip : ""
  82. # Council reference: prefer the document file name (e.g., ABC-123) if present
  83. council_reference =
  84. if document_url && !document_url.empty?
  85. File.basename(document_url).sub(/\.pdf\z/i, "").upcase
  86. else
  87. # fallback: compact title text
  88. address.gsub(/\s+/, " ")[0,120]
  89. end
  90. # Require key fields
  91. next if address.empty? || council_reference.empty?
  92. DB.upsert(TABLE, {
  93. description: description,
  94. date_received: date_received,
  95. on_notice_to: on_notice_to, # keep the closing date in the DATE column
  96. on_notice_to_raw: on_notice_to_raw, # raw text as seen on page
  97. address: address,
  98. council_reference: council_reference,
  99. applicant: "",
  100. owner: ""
  101. })
  102. # store remote doc URL
  103. begin
  104. DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ? WHERE council_reference = ? AND address = ?")
  105. .execute(document_url, council_reference, address)
  106. rescue => e
  107. warn "document_url update failed: #{e.class} #{e.message}"
  108. end
  109. # download + store local_document_url
  110. local_rel = download_pdf(document_url, council_reference)
  111. if !local_rel.empty?
  112. begin
  113. DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET local_document_url = ? WHERE council_reference = ? AND address = ?")
  114. .execute(local_rel, council_reference, address)
  115. rescue => e
  116. warn "local_document_url update failed: #{e.class} #{e.message}"
  117. end
  118. end
  119. enrich_after_upsert!(
  120. table: TABLE,
  121. council_reference: council_reference,
  122. address: address
  123. )
  124. # If we added the optional column earlier, keep the link
  125. begin
  126. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ? WHERE council_reference = ? AND address = ?")
  127. upd.execute(document_url, council_reference, address)
  128. rescue => e
  129. # ignore if column not present
  130. end
  131. puts "Upserted #{council_reference} -> #{address} (doc: #{document_url.empty? ? 'none' : 'remote'}#{local_rel.empty? ? '' : ', saved'})"
  132. saved += 1
  133. end
  134. puts "Done #{TABLE}. Saved #{saved} item(s)."