tasman.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Tasman Council — Advertised 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_tasman
  9. URL = "https://tasman.tas.gov.au/advertised-applications/"
  10. DB.ensure_table!(TABLE)
  11. # Optional: store the PDF link
  12. begin
  13. DB.client.query("ALTER TABLE `#{DB.client.escape(TABLE)}` ADD COLUMN IF NOT EXISTS document_url VARCHAR(1024) NULL")
  14. rescue => e
  15. warn "document_url add skipped: #{e.class} #{e.message}"
  16. end
  17. def abs_url(base, href)
  18. return "" if href.to_s.strip.empty?
  19. URI.join(base, href).to_s rescue href.to_s
  20. end
  21. # Nokogiri CSS :contains(...) is a bit special. Use a safer find for the Date row.
  22. def find_date_from_details(row)
  23. details = row.at_css(".details")
  24. return "" unless details
  25. details.css("tr").each do |tr|
  26. tds = tr.css("td")
  27. next unless tds.length >= 2
  28. key = tds[0].text.strip
  29. val = tds[1].text.strip
  30. return val if key =~ /\bDate\b/i
  31. end
  32. ""
  33. end
  34. html = Http.get(URL)
  35. doc = Nokogiri::HTML(html)
  36. items = doc.css(".wpfilebase-file-default")
  37. puts "Found #{items.length} items for #{TABLE}"
  38. saved = 0
  39. items.each_with_index do |row, idx|
  40. link = row.at_css(".filetitle a")
  41. next unless link
  42. title_text = link.text.strip
  43. document_url = abs_url(URL, link["href"])
  44. # Common pattern on this page is "REF - Address - On notice date"
  45. council_reference = title_text.split(" - ").first.to_s.strip
  46. council_reference = council_reference.empty? ? title_text[0,120] : council_reference
  47. # Use title as address if nothing cleaner is available
  48. address = title_text
  49. # On-notice date often appears inside the title
  50. on_notice_to_raw = if (m = title_text.match(/(\d{1,2}\s+[A-Za-z]+\s+\d{4})/))
  51. m[1]
  52. else
  53. ""
  54. end
  55. on_notice_to = Util.parse_aus_date(on_notice_to_raw)
  56. # Application date is shown inside the details table under "Date"
  57. date_received_raw = find_date_from_details(row)
  58. date_received =
  59. Util.parse_aus_date(date_received_raw) ||
  60. begin
  61. s = date_received_raw.to_s
  62. s.empty? ? nil : Date.strptime(s, "%A, %d %B, %Y") rescue nil
  63. end
  64. description = "Development Application"
  65. # Require core fields
  66. next if council_reference.empty? || address.empty?
  67. DB.upsert(TABLE, {
  68. description: description,
  69. date_received: date_received,
  70. date_received_raw: date_received_raw,
  71. address: address,
  72. council_reference: council_reference,
  73. applicant: "",
  74. owner: ""
  75. })
  76. enrich_after_upsert!(
  77. table: TABLE,
  78. council_reference: council_reference,
  79. address: address
  80. )
  81. # Save the PDF link if the column exists
  82. begin
  83. upd = DB.client.prepare("UPDATE `#{DB.client.escape(TABLE)}` SET document_url = ? WHERE council_reference = ? AND address = ?")
  84. upd.execute(document_url, council_reference, address)
  85. rescue Mysql2::Error => e
  86. warn "[tasman] db update skipped for #{council_reference}: #{e.message}"
  87. end
  88. puts "Upserted #{council_reference} -> #{address}"
  89. saved += 1
  90. end
  91. puts "Done #{TABLE}. Saved #{saved} item(s)."