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.
 
 
 
 
 

121 lines
3.6 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. summary = ScraperWiki.select("
  44. authority_name, status, decision, appeal_status, appeal_decision, count(*) as applications
  45. from applications
  46. group by authority_name, status, decision, appeal_status, appeal_decision
  47. ")
  48. write_page('.', 'index', { summary: summary })
  49. # Summary CSV file
  50. csv_string = CSV.generate do |csv|
  51. csv << summary.first.keys # header row
  52. summary.each { |row| csv << row.values }
  53. end
  54. write_csv('.', 'inlink-summary', csv_string)
  55. # Full CSV file
  56. apps = ScraperWiki.select("* from applications")
  57. csv_string = CSV.generate do |csv|
  58. csv << apps.first.keys # header row
  59. apps.each { |app| csv << app.values }
  60. end
  61. write_csv('.', 'inlink-full', csv_string)
  62. end
  63. def gen_new
  64. apps = ScraperWiki.select("* from `applications` order by date_received desc limit 30")
  65. write_page('new', 'new', { apps: apps })
  66. end
  67. def gen_decisions
  68. apps = ScraperWiki.select("* from `applications` order by date_decision desc limit 30")
  69. write_page('decisions', 'decisions', { apps: apps })
  70. end
  71. def gen_authorities
  72. auths = ScraperWiki.select("distinct(authority_name) as authority_name from applications order by authority_name")
  73. write_page('authorities', 'authorities', { auths: auths })
  74. auths.each do |auth|
  75. summary = ScraperWiki.select("
  76. status, decision, appeal_status, appeal_decision, count(*) as qty
  77. from applications
  78. where authority_name = '#{auth['authority_name']}'
  79. group by status, decision, appeal_status, appeal_decision
  80. ")
  81. apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received")
  82. write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary })
  83. csv_string = CSV.generate do |csv|
  84. csv << apps.first.keys # header row
  85. apps.each { |app| csv << app.values }
  86. end
  87. write_csv(['authorities', slug(auth['authority_name'])], slug(auth['authority_name']), csv_string)
  88. end
  89. end
  90. @working_dir = File.join(Dir.pwd, OUTPUT_DIR)
  91. puts @working_dir
  92. # exit
  93. @log = Logger.new($stdout)
  94. @pages = 0
  95. create_output_dir
  96. gen_homepage
  97. gen_new
  98. gen_decisions
  99. gen_authorities