Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/bin/google-form-news.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
9
# Fetch data from a google sheet
10
url = 'https://docs.google.com/spreadsheets/d/1KJsQpZsUfaZh-a8jLx0gNymaQxvSNForrekEgIzF3Eo/export?format=tsv'
11
data = `curl -sL "#{url}"`
12
13
contributions_keys = %w[Authorship Editing Testing Infrastructure Translation Funding]
14
15
data = CSV.parse(data, col_sep: "\t", headers: true, quote_char: '|')
16
count = 0
17
18
data.each do |row|
19
# Parse
20
# 29/01/2024 14:04:47
21
post_date = DateTime.strptime(row['Timestamp'], '%d/%m/%Y %H:%M:%S')
22
filename = "news/_posts/#{post_date.strftime('%Y-%m-%d')}-#{row['Title'].downcase.gsub(/[^a-z0-9\s]/i, '').gsub(
23
/\s+/, '-'
24
)}.md"
25
26
# Skip some testing posts
27
if (row['Title'] == 'TESTING') || (row['Title'] == "Wendi's Dog is the Best")
28
STDERR.puts "Skipping #{filename} as it is a test post"
29
next
30
end
31
32
# Don't overwrite existing posts
33
if File.exist?(filename)
34
STDERR.puts "Skipping #{filename} as it already exists"
35
next
36
end
37
38
STDERR.puts "Creating #{filename}"
39
count += 1
40
41
post_metadata = {
42
'title' => row['Title'],
43
'layout' => 'news',
44
'tags' => row['Tags'].split(',').map(&:strip),
45
'from_google_form' => true,
46
'contributions' => {}
47
}
48
49
contributions_keys.each do |key|
50
post_metadata['contributions'][key.downcase] = row[key].split(',').map(&:strip) if row[key]
51
end
52
53
if row['Optional Cover Image']
54
post_metadata['cover'] = row['Optional Cover Image']
55
post_metadata['coveralt'] = row['Cover Image Alternative Text']
56
end
57
58
if row['Link to a specific tutorial you wish to push people to view']
59
post_metadata['tutorial'] = row['Link to a specific tutorial you wish to push people to view']
60
end
61
62
if row['Link for a non-tutorial thing you want to push people to view']
63
post_metadata['link'] = row['Link for a non-tutorial thing you want to push people to view']
64
end
65
66
# Serialise to a file
67
68
File.open(filename, 'w') do |file|
69
file.puts YAML.dump(post_metadata)
70
file.puts "---\n"
71
file.puts row['Blog Post'].gsub(' ', "\n\n")
72
end
73
end
74
75
puts count
76
77