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.
 
 
 
 
 

98 regels
3.0 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. def gen_decisions
  50. apps = ScraperWiki.select("* from `applications` order by date_decision desc limit 30")
  51. write_page('decisions', 'decisions', { apps: apps })
  52. end
  53. def gen_authorities
  54. auths = ScraperWiki.select("distinct(authority_name) as authority_name from applications order by authority_name")
  55. write_page('authorities', 'authorities', { auths: auths })
  56. auths.each do |auth|
  57. summary = ScraperWiki.select("
  58. status, decision, appeal_status, appeal_decision, count(*) as qty
  59. from applications
  60. where authority_name = '#{auth['authority_name']}'
  61. group by status, decision, appeal_status, appeal_decision
  62. ")
  63. apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received")
  64. write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary })
  65. csv_string = CSV.generate do |csv|
  66. csv << apps.first.keys # header row
  67. apps.each { |app| csv << app.values }
  68. end
  69. write_csv(['authorities', slug(auth['authority_name'])], slug(auth['authority_name']), csv_string)
  70. end
  71. end
  72. @working_dir = File.join(Dir.pwd, OUTPUT_DIR)
  73. puts @working_dir
  74. # exit
  75. @log = Logger.new($stdout)
  76. @pages = 0
  77. create_output_dir
  78. gen_homepage
  79. gen_new
  80. gen_decisions
  81. gen_authorities