glenorchy.rb 6.0 KB

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