Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/bin/google-form-event.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/1LTShstcORXf_zB06naOfPzisAoOcik-xJvgK5LQCGP8/export?format=tsv'
12
data = `curl -sL "#{url}"`
13
14
data = CSV.parse(data, col_sep: "\t", headers: true, quote_char: '|')
15
count = 0
16
pr_message = ""
17
18
data.each do |row|
19
# Parse
20
# 29/01/2024 14:04:47
21
event_date = DateTime.strptime(row['Start date'], '%d/%m/%Y')
22
if row['End date']
23
event_date_end = DateTime.strptime(row['End date'], '%d/%m/%Y')
24
end
25
post_date = DateTime.strptime(row['Timestamp'], '%d/%m/%Y %H:%M:%S')
26
27
filename = "events/#{event_date.strftime('%Y-%m-%d')}-#{row['Title of your Event'].downcase.gsub(/[^a-z0-9\s-]/i, '').gsub(/\s+/, ' ').gsub(/ /, '-')}.md"
28
29
# Skip some testing posts
30
if (row['Title of your Event'] == 'TESTING')
31
STDERR.puts "Skipping #{filename} as it is a test post"
32
next
33
end
34
35
# Don't overwrite existing posts
36
if File.exist?(filename)
37
other_file = safe_load_yaml(filename)
38
if other_file['google_form_id'] == post_date.to_time.to_i
39
STDERR.puts "Skipping #{filename} as it already exists"
40
next
41
else
42
filename = filename.gsub('.md', "-#{post_date.to_time.to_i}.md")
43
end
44
end
45
46
STDERR.puts "Creating #{filename}"
47
count += 1
48
49
post_metadata = {
50
'layout' => 'event-external',
51
'google_form_id' => post_date.to_time.to_i,
52
'title' => row['Title of your Event'],
53
'description' => row['Description of your event'],
54
'external' => row['Link to event page'],
55
'contributions' => {
56
'organisers' => row['Organizers already in the GTN CONTRIBUTORS file']&.split(',')&.map(&:strip)
57
},
58
'location' => {
59
'name' => row['Location of the event']
60
},
61
'date_start' => event_date.to_date,
62
}
63
64
if row['End date']
65
post_metadata['date_end'] = event_date_end.to_date
66
end
67
68
# Serialise to a file
69
File.open(filename, 'w') do |file|
70
file.puts YAML.dump(post_metadata)
71
file.puts "---\n"
72
73
if row['Comments'] && row['Comments'].length.positive?
74
file.puts "<!-- #{row['Comments']} -->"
75
end
76
end
77
78
79
if row['Organizers not yet in the GTN CONTRIBUTORS file']
80
pr_message += "<br>TODO: add the following contributors to the GTN: <br> #{row['Organizers not yet in the GTN CONTRIBUTORS file']}"
81
end
82
if row['Anything else we should know?']
83
pr_message += "<br><br>Remarks from submitter:<br> #{row['Anything else we should know?']}"
84
end
85
end
86
87
88
puts "new_ids=#{count}"
89
puts "pr_message=#{pr_message}"
90
91