flinders_council.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Flinders Council — Current Advertising scraper (site page, not PlanBuild)
  2. require "nokogiri"
  3. require "cgi"
  4. require_relative "../lib/enrich"
  5. require_relative "../lib/log"
  6. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets this from filename: da_flinders
  7. URL = "https://www.flinders.tas.gov.au/current-advertising"
  8. DB.ensure_table!(TABLE)
  9. def abs_url(base, href)
  10. return "" if href.to_s.strip.empty?
  11. URI.join(base, href).to_s rescue href.to_s
  12. end
  13. def extract_ref_from(text)
  14. s = CGI.unescape(text.to_s)
  15. # Pattern 1: "DA 2025 / 00017" or "DA 2025/00017"
  16. if (m = s.match(/DA\s*20\d{2}\s*\/\s*\d{3,}/i))
  17. return m[0].gsub(/\s+/, " ").sub(" / ", " / ").strip
  18. end
  19. # Pattern 2: "DA202500017" -> "DA 2025 / 00017"
  20. if (m = s.match(/DA20\d{2}\d{5}/i))
  21. raw = m[0]
  22. year = raw[2,4]
  23. num = raw[6,5]
  24. return "DA #{year} / #{num}"
  25. end
  26. nil
  27. end
  28. html = Http.get(URL)
  29. doc = Nokogiri::HTML(html)
  30. # Pick all advertised PDFs listed on the page
  31. links = doc.css("a").select { |a|
  32. href = a["href"].to_s
  33. href.match?(%r{/client-assets/images/Development/Downloads/Advertising/}i) && href.downcase.end_with?(".pdf")
  34. }
  35. puts "Found #{links.length} items for #{TABLE}"
  36. saved = 0
  37. links.each do |a|
  38. text = a.text.strip.gsub(/\s+/, " ")
  39. pdf = abs_url(URL, a["href"])
  40. # Use link text first, fall back to file name as address
  41. address = text.empty? ? File.basename(pdf).sub(/\.pdf\z/i, "") : text
  42. # Council reference from link text or filename
  43. ref = extract_ref_from(text) || extract_ref_from(File.basename(pdf))
  44. # Description and dates are usually inside the PDF, so keep minimal fields here
  45. description = "Development Application"
  46. date_received_raw = ""
  47. date_received = nil
  48. next if address.empty? || ref.nil?
  49. DB.upsert(TABLE, {
  50. description: description,
  51. date_received: date_received,
  52. date_received_raw: date_received_raw,
  53. address: address,
  54. council_reference: ref,
  55. document_url: pdf,
  56. applicant: "",
  57. owner: ""
  58. })
  59. enrich_after_upsert!(
  60. table: TABLE,
  61. council_reference: ref,
  62. address: address
  63. )
  64. puts "Upserted #{ref} -> #{address}"
  65. saved += 1
  66. end
  67. puts "Done #{TABLE}. Saved #{saved} item(s)."