Planning applications tracker for InLinkUK from BT kiosks. https://kiosks.adrianshort.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

93 lines
2.8 KiB

  1. #!/usr/bin/env ruby
  2. require 'scraperwiki'
  3. require 'haml'
  4. require 'pp'
  5. require 'logger'
  6. require 'csv'
  7. require_relative '../lib/helpers'
  8. OUTPUT_DIR = '_site'
  9. VIEWS_DIR = File.join('views')
  10. LAYOUT_FN = File.join(VIEWS_DIR, 'layout.haml')
  11. def write_page(path_items, template, locals = {})
  12. dir = File.join(OUTPUT_DIR, path_items)
  13. FileUtils.mkdir_p(dir)
  14. @log.debug dir
  15. fn = File.join(dir, 'index.html')
  16. # https://stackoverflow.com/questions/6125265/using-layouts-in-haml-files-independently-of-rails
  17. html = Haml::Engine.new(File.read(LAYOUT_FN)).render do
  18. Haml::Engine.new(File.read(File.join(VIEWS_DIR, "#{template}.haml"))).render(Object.new, locals)
  19. end
  20. File.write(fn, html)
  21. @log.info fn
  22. @pages += 1
  23. # TODO - add page to sitemap.xml or sitemap.txt
  24. # https://support.google.com/webmasters/answer/183668?hl=en&ref_topic=4581190
  25. end
  26. def write_csv(path_items, filename, data)
  27. dir = File.join(OUTPUT_DIR, path_items)
  28. FileUtils.mkdir_p(dir)
  29. @log.debug dir
  30. fn = File.join(dir, filename + '.csv')
  31. File.write(fn, data)
  32. @log.info fn
  33. end
  34. def create_output_dir
  35. # Recursively delete working directory to ensure no redundant files are left behind from previous builds.
  36. # FileUtils.rm_rf(@working_dir)
  37. Dir.mkdir(@working_dir) unless File.directory?(@working_dir)
  38. # Dir.chdir(@working_dir)
  39. # Copy `public` dir to output dir
  40. FileUtils.copy_entry('public', @working_dir)
  41. end
  42. def gen_homepage
  43. decisions = ScraperWiki.select("* from `applications` order by date_decision desc limit 20")
  44. write_page('.', 'index', { decisions: decisions })
  45. def gen_new
  46. apps = ScraperWiki.select("* from `applications` order by date_received desc limit 30")
  47. write_page('new', 'new', { apps: apps })
  48. end
  49. end
  50. def gen_authorities
  51. auths = ScraperWiki.select("distinct(authority_name) as authority_name from applications order by authority_name")
  52. write_page('authorities', 'authorities', { auths: auths })
  53. auths.each do |auth|
  54. summary = ScraperWiki.select("
  55. status, decision, appeal_status, appeal_decision, count(*) as qty
  56. from applications
  57. where authority_name = '#{auth['authority_name']}'
  58. group by status, decision, appeal_status, appeal_decision
  59. ")
  60. apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received")
  61. write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary })
  62. csv_string = CSV.generate do |csv|
  63. csv << apps.first.keys # header row
  64. apps.each { |app| csv << app.values }
  65. end
  66. write_csv(['authorities', slug(auth['authority_name'])], slug(auth['authority_name']), csv_string)
  67. end
  68. end
  69. @working_dir = File.join(Dir.pwd, OUTPUT_DIR)
  70. puts @working_dir
  71. # exit
  72. @log = Logger.new($stdout)
  73. @pages = 0
  74. create_output_dir
  75. gen_homepage
  76. gen_new
  77. gen_authorities