Planning applications tracker for InLinkUK from BT kiosks. https://kiosks.adrianshort.org/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build 3.3 KiB

6 년 전
6 년 전
6 년 전
6 년 전
6 년 전
6 년 전
6 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. csv_string = CSV.generate do |csv|
  32. csv << data.first.keys # header row
  33. data.each { |row| csv << row.values }
  34. end
  35. File.write(fn, csv_string)
  36. @log.info fn
  37. end
  38. def create_output_dir
  39. # Recursively delete working directory to ensure no redundant files are left behind from previous builds.
  40. # FileUtils.rm_rf(@working_dir)
  41. Dir.mkdir(@working_dir) unless File.directory?(@working_dir)
  42. # Dir.chdir(@working_dir)
  43. # Copy `public` dir to output dir
  44. FileUtils.copy_entry('public', @working_dir)
  45. end
  46. def gen_homepage
  47. summary = ScraperWiki.select("
  48. authority_name, status, decision, appeal_status, appeal_decision, count(*) as applications
  49. from applications
  50. group by authority_name, status, decision, appeal_status, appeal_decision
  51. ")
  52. write_page('.', 'index', { summary: summary })
  53. # Summary CSV file
  54. write_csv('.', 'inlink-summary', summary)
  55. # Full CSV file
  56. apps = ScraperWiki.select("* from applications")
  57. write_csv('.', 'inlink-full', apps)
  58. end
  59. def gen_new
  60. apps = ScraperWiki.select("* from `applications` order by date_received desc limit 30")
  61. write_page('new', 'new', { apps: apps })
  62. end
  63. def gen_decisions
  64. apps = ScraperWiki.select("* from `applications` order by date_decision desc limit 30")
  65. write_page('decisions', 'decisions', { apps: apps })
  66. end
  67. def gen_authorities
  68. auths = ScraperWiki.select("distinct(authority_name) as authority_name from applications order by authority_name")
  69. write_page('authorities', 'authorities', { auths: auths })
  70. auths.each do |auth|
  71. summary = ScraperWiki.select("
  72. status, decision, appeal_status, appeal_decision, count(*) as qty
  73. from applications
  74. where authority_name = '#{auth['authority_name']}'
  75. group by status, decision, appeal_status, appeal_decision
  76. ")
  77. apps = ScraperWiki.select("* from applications where authority_name='#{auth['authority_name']}' order by date_received")
  78. write_page(['authorities', slug(auth['authority_name'])], 'authority', { apps: apps, auth: auth, summary: summary })
  79. write_csv(['authorities', slug(auth['authority_name'])], slug(auth['authority_name']), apps)
  80. end
  81. end
  82. @working_dir = File.join(Dir.pwd, OUTPUT_DIR)
  83. puts @working_dir
  84. # exit
  85. @log = Logger.new($stdout)
  86. @log.level = Logger::INFO
  87. @pages = 0
  88. create_output_dir
  89. gen_homepage
  90. gen_new
  91. gen_decisions
  92. gen_authorities