Path: blob/main/bin/google-form-news.rb
1677 views
#!/usr/bin/env ruby1# frozen_string_literal: true23require 'yaml'4require 'net/http'5require 'csv'6require 'date'78# Fetch data from a google sheet9url = 'https://docs.google.com/spreadsheets/d/1KJsQpZsUfaZh-a8jLx0gNymaQxvSNForrekEgIzF3Eo/export?format=tsv'10data = `curl -sL "#{url}"`1112contributions_keys = %w[Authorship Editing Testing Infrastructure Translation Funding]1314data = CSV.parse(data, col_sep: "\t", headers: true, quote_char: '|')15count = 01617data.each do |row|18# Parse19# 29/01/2024 14:04:4720post_date = DateTime.strptime(row['Timestamp'], '%d/%m/%Y %H:%M:%S')21filename = "news/_posts/#{post_date.strftime('%Y-%m-%d')}-#{row['Title'].downcase.gsub(/[^a-z0-9\s]/i, '').gsub(22/\s+/, '-'23)}.md"2425# Skip some testing posts26if (row['Title'] == 'TESTING') || (row['Title'] == "Wendi's Dog is the Best")27STDERR.puts "Skipping #{filename} as it is a test post"28next29end3031# Don't overwrite existing posts32if File.exist?(filename)33STDERR.puts "Skipping #{filename} as it already exists"34next35end3637STDERR.puts "Creating #{filename}"38count += 13940post_metadata = {41'title' => row['Title'],42'layout' => 'news',43'tags' => row['Tags'].split(',').map(&:strip),44'from_google_form' => true,45'contributions' => {}46}4748contributions_keys.each do |key|49post_metadata['contributions'][key.downcase] = row[key].split(',').map(&:strip) if row[key]50end5152if row['Optional Cover Image']53post_metadata['cover'] = row['Optional Cover Image']54post_metadata['coveralt'] = row['Cover Image Alternative Text']55end5657if row['Link to a specific tutorial you wish to push people to view']58post_metadata['tutorial'] = row['Link to a specific tutorial you wish to push people to view']59end6061if row['Link for a non-tutorial thing you want to push people to view']62post_metadata['link'] = row['Link for a non-tutorial thing you want to push people to view']63end6465# Serialise to a file6667File.open(filename, 'w') do |file|68file.puts YAML.dump(post_metadata)69file.puts "---\n"70file.puts row['Blog Post'].gsub(' ', "\n\n")71end72end7374puts count757677