glenorchy.rb 6.1 KB

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