tasman.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Tasman Council — Advertised Applications (site page, not PlanBuild)
  2. require "nokogiri"
  3. require "date"
  4. require_relative "../lib/http"
  5. require_relative "../lib/util"
  6. require_relative "../lib/scraper_helpers"
  7. TABLE = ENV.fetch("TABLE_NAME") # run_all.sh sets from filename: da_tasman
  8. URL = "https://tasman.tas.gov.au/advertised-applications/"
  9. DB.ensure_table!(TABLE)
  10. # Nokogiri CSS :contains(...) is a bit special. Use a safer find for the Date row.
  11. def find_date_from_details(row)
  12. details = row.at_css(".details")
  13. return "" unless details
  14. details.css("tr").each do |tr|
  15. tds = tr.css("td")
  16. next unless tds.length >= 2
  17. key = tds[0].text.strip
  18. val = tds[1].text.strip
  19. return val if key =~ /\bDate\b/i
  20. end
  21. ""
  22. end
  23. html = Http.get(URL)
  24. doc = Nokogiri::HTML(html)
  25. items = doc.css(".wpfilebase-file-default")
  26. puts "Found #{items.length} items for #{TABLE}"
  27. saved = 0
  28. items.each_with_index do |row, idx|
  29. link = row.at_css(".filetitle a")
  30. next unless link
  31. title_text = link.text.strip
  32. document_url = abs_url(URL, link["href"])
  33. # Common pattern on this page is "REF - Address - On notice date"
  34. council_reference = title_text.split(" - ").first.to_s.strip
  35. council_reference = council_reference.empty? ? title_text[0,120] : council_reference
  36. # Use title as address if nothing cleaner is available
  37. address = title_text
  38. # On-notice date often appears inside the title
  39. on_notice_to_raw = if (m = title_text.match(/(\d{1,2}\s+[A-Za-z]+\s+\d{4})/))
  40. m[1]
  41. else
  42. ""
  43. end
  44. on_notice_to = Util.parse_aus_date(on_notice_to_raw)
  45. # Application date is shown inside the details table under "Date"
  46. date_received_raw = find_date_from_details(row)
  47. date_received =
  48. Util.parse_aus_date(date_received_raw) ||
  49. begin
  50. s = date_received_raw.to_s
  51. s.empty? ? nil : Date.strptime(s, "%A, %d %B, %Y") rescue nil
  52. end
  53. description = "Development Application"
  54. # Require core fields
  55. next if council_reference.empty? || address.empty?
  56. upsert_and_enrich!(
  57. table: TABLE,
  58. row: {
  59. description: description,
  60. date_received: date_received,
  61. date_received_raw: date_received_raw,
  62. address: address,
  63. council_reference: council_reference,
  64. applicant: "",
  65. owner: ""
  66. },
  67. extras: { document_url: document_url }
  68. )
  69. saved += 1
  70. end
  71. puts "Done #{TABLE}. Saved #{saved} item(s)."