Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/bin/google-form-faq.rb
1677 views
1
#!/usr/bin/env ruby
2
# frozen_string_literal: true
3
4
require 'yaml'
5
require 'net/http'
6
require 'csv'
7
require 'date'
8
require './_plugins/util'
9
10
# Fetch data from a google sheet
11
url = 'https://docs.google.com/spreadsheets/d/1RFF3G9_bP8EpfACBk8lnF-Ib43ZGGAMm3ewPwW7eFT0/export?format=tsv'
12
data = `curl -sL "#{url}"`
13
14
# We have the problem of renamed FAQs, these will be stored somewhere other
15
# than the auto-generated name.
16
#
17
# We need to eliminate that duplication and ensure we don't overwrite an existing one.
18
#
19
# So we need to discover all existing FAQs and their google form IDs.
20
21
def self.discover_faqs
22
paths = []
23
paths += Dir.glob('faqs/**/*.md')
24
paths += Dir.glob('topics/**/faqs/*.md')
25
paths += Dir.glob('topics/**/faqs/*.md')
26
27
# Reject symlinks
28
paths.reject { |path| File.symlink?(path) }
29
end
30
31
faqs = discover_faqs.map do |path|
32
metadata = safe_load_yaml(path)
33
if metadata.is_a?(String)
34
next
35
end
36
37
[metadata['google_form_id'], path]
38
end.reject{|x, y| x.nil?}.to_h
39
40
# The google form data.
41
data = CSV.parse(data, col_sep: "\t", headers: true)
42
count = 0
43
44
data.each do |row|
45
# Parse
46
# 29/01/2024 14:04:47
47
post_date = DateTime.strptime(row['Timestamp'], '%d/%m/%Y %H:%M:%S')
48
filename = "faqs/#{row['This FAQ Concerns'].downcase}/#{row['Title'].downcase.gsub(/[^a-z0-9\s-]/i, '').gsub(/\s+/, ' ').gsub(/ /, '-')}.md"
49
50
google_form_id = post_date.to_time.to_i
51
if faqs.include?(google_form_id)
52
STDERR.puts "Already imported as #{faqs[google_form_id]}"
53
next
54
end
55
56
# Skip some testing posts
57
if (row['Title'] == 'TESTING')
58
STDERR.puts "Skipping #{filename} as it is a test post"
59
next
60
end
61
62
# Don't overwrite existing posts
63
if File.exist?(filename)
64
other_file = safe_load_yaml(filename)
65
if other_file['google_form_id'] == google_form_id
66
STDERR.puts "Skipping #{filename} as it already exists"
67
next
68
else
69
filename = filename.gsub('.md', "-#{google_form_id}.md")
70
end
71
end
72
73
STDERR.puts "Creating #{filename}"
74
count += 1
75
76
post_metadata = {
77
'title' => row['Title'],
78
'layout' => 'faq',
79
'area' => row['Area'],
80
'box_type' => 'tip',
81
'google_form_id' => post_date.to_time.to_i,
82
'contributors' => [row['Your GitHub username']]
83
}
84
85
# Serialise to a file
86
File.open(filename, 'w') do |file|
87
file.puts YAML.dump(post_metadata)
88
file.puts "---\n"
89
file.puts row['FAQ Text'].gsub(' ', "\n\n")
90
91
if row['Comments'] && row['Comments'].length.positive?
92
file.puts "<!-- #{row['Comments']} -->"
93
end
94
end
95
end
96
97
puts count
98
99