-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathmailchimp_replicate_newsletter.rb
More file actions
executable file
·268 lines (219 loc) · 8.62 KB
/
Copy pathmailchimp_replicate_newsletter.rb
File metadata and controls
executable file
·268 lines (219 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'base64'
require 'date'
require 'json'
require 'net/http'
require 'optparse'
require 'uri'
require 'yaml'
DATA_FILE = File.expand_path('../_data/current.yml', __dir__)
ENV_FILE = File.expand_path('../.env', __dir__)
DEFAULT_LIMIT = 12
DEFAULT_DAYS_AHEAD = 60
DEFAULT_PLACEHOLDER = '{{TESTING_CONFERENCES_CONTENT}}'
MONTH_PATTERN = '(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Sept|Oct|Nov|Dec)'
def normalize_month(name)
case name.to_s.downcase
when 'jan' then 'January'
when 'feb' then 'February'
when 'mar' then 'March'
when 'apr' then 'April'
when 'may' then 'May'
when 'jun' then 'June'
when 'jul' then 'July'
when 'aug' then 'August'
when 'sep', 'sept' then 'September'
when 'oct' then 'October'
when 'nov' then 'November'
when 'dec' then 'December'
else
name.to_s.capitalize
end
end
def build_date(month_name, day, year)
month_index = Date::MONTHNAMES.index(month_name)
return nil unless month_index
Date.new(year.to_i, month_index, day.to_i)
rescue ArgumentError
nil
end
def parse_end_date(value)
return nil if value.nil?
s = value.to_s.strip.gsub(/[–—]/, '-')
if (m = s.match(/(#{MONTH_PATTERN})\s+(\d{1,2})\s*-\s*(#{MONTH_PATTERN})\s*(\d{1,2}),?\s*(\d{4})$/i))
return build_date(normalize_month(m[3]), m[4], m[5])
end
if (m = s.match(/(#{MONTH_PATTERN})\s+\d{1,2}\s*-\s*(\d{1,2}),?\s*(\d{4})$/i))
return build_date(normalize_month(m[1]), m[2], m[3])
end
if (m = s.match(/(#{MONTH_PATTERN})\s+\d{1,2}\s*-\s*(\d{1,2})\s+(#{MONTH_PATTERN}),?\s*(\d{4})$/i))
return build_date(normalize_month(m[3]), m[2], m[4])
end
if (m = s.match(/(#{MONTH_PATTERN})\s+(\d{1,2}),?\s*(\d{4})$/i))
return build_date(normalize_month(m[1]), m[2], m[3])
end
Date.parse(s)
rescue ArgumentError
nil
end
def strip_html(text)
text.to_s.gsub(%r{<[^>]+>}, '').gsub(/\s+/, ' ').strip
end
def html_escape(text)
text.to_s
.gsub('&', '&')
.gsub('<', '<')
.gsub('>', '>')
.gsub('"', '"')
.gsub("'", ''')
end
def unquote_env_value(value)
return value[1..-2].gsub('\"', '"').gsub("\\'", "'") if value.start_with?('"') && value.end_with?('"')
return value[1..-2].gsub("\\'", "'").gsub('\\"', '"') if value.start_with?("'") && value.end_with?("'")
value
end
def load_dotenv(path)
return unless File.exist?(path)
File.foreach(path) do |line|
stripped = line.strip
next if stripped.empty? || stripped.start_with?('#')
stripped = stripped.sub(/^export\s+/, '')
key, value = stripped.split('=', 2)
next if key.nil? || value.nil?
key = key.strip
value = unquote_env_value(value.strip)
next if key.empty? || value.empty?
next unless ENV[key].to_s.strip.empty?
ENV[key] = value
end
end
class MailchimpClient
def initialize(api_key)
@api_key = api_key
dc = api_key.to_s.split('-').last
raise ArgumentError, 'MAILCHIMP_API_KEY must end with data center suffix (for example: -us6)' if dc.nil? || dc.empty?
@base_uri = URI("https://#{dc}.api.mailchimp.com/3.0")
end
def request(method, path, body: nil)
uri = URI.join(@base_uri.to_s + '/', path.sub(%r{^/}, ''))
req_class = case method.upcase
when 'GET' then Net::HTTP::Get
when 'POST' then Net::HTTP::Post
when 'PATCH' then Net::HTTP::Patch
when 'PUT' then Net::HTTP::Put
else
raise ArgumentError, "Unsupported method: #{method}"
end
req = req_class.new(uri)
req['Authorization'] = "Basic #{Base64.strict_encode64("anystring:#{@api_key}")}"
req['Content-Type'] = 'application/json'
req.body = JSON.generate(body) if body
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(req)
parsed = response.body.to_s.empty? ? {} : JSON.parse(response.body)
return parsed if response.code.to_i.between?(200, 299)
detail = parsed.is_a?(Hash) ? parsed['detail'] : response.body
raise "Mailchimp API error (#{response.code} #{response.message}) on #{method} #{path}: #{detail}"
end
end
def build_events_html(events)
items = events.map do |event|
name = html_escape(event['name'])
url = html_escape(event['url'])
dates = html_escape(event['dates'])
location = html_escape(event['location'])
status = strip_html(event['status'])
status_html = status.empty? ? '' : "<br><span style=\"color:#666;\">#{html_escape(status)}</span>"
[
'<li style="margin-bottom:14px;">',
"<a href=\"#{url}\" target=\"_blank\" rel=\"noopener\"><strong>#{name}</strong></a>",
"<br>#{dates}#{location.empty? ? '' : " - #{location}"}",
status_html,
'</li>'
].join
end
[
'<h2 style="margin:0 0 12px;">Upcoming Software Testing Conferences</h2>',
'<p style="margin:0 0 16px;">Curated from testingconferences.org.</p>',
'<ul style="padding-left:20px; margin:0;">',
items.join,
'</ul>'
].join
end
def insert_newsletter_content(original_html, generated_html, placeholder)
return generated_html if original_html.to_s.strip.empty?
return original_html.sub(placeholder, generated_html) if original_html.include?(placeholder)
if original_html.include?('</body>')
return original_html.sub('</body>', "<hr style=\"border:none;border-top:1px solid #ddd;margin:24px 0;\">#{generated_html}</body>")
end
original_html + "\n#{generated_html}"
end
load_dotenv(ENV_FILE)
options = {
limit: DEFAULT_LIMIT,
days_ahead: DEFAULT_DAYS_AHEAD,
placeholder: DEFAULT_PLACEHOLDER,
dry_run: false,
source_campaign_id: ENV['MAILCHIMP_SOURCE_CAMPAIGN_ID'],
subject: nil,
title_prefix: 'TestingConferences Newsletter'
}
OptionParser.new do |opts|
opts.banner = 'Usage: ruby tools/mailchimp_replicate_newsletter.rb [options]'
opts.on('--source-campaign ID', 'Mailchimp campaign ID to replicate (or set MAILCHIMP_SOURCE_CAMPAIGN_ID)') { |v| options[:source_campaign_id] = v }
opts.on('--limit N', Integer, "Max events to include (default: #{DEFAULT_LIMIT})") { |v| options[:limit] = v }
opts.on('--days-ahead N', Integer, "Only include events ending within N days from today (default: #{DEFAULT_DAYS_AHEAD})") { |v| options[:days_ahead] = v }
opts.on('--placeholder TEXT', "Placeholder in the source campaign HTML (default: #{DEFAULT_PLACEHOLDER})") { |v| options[:placeholder] = v }
opts.on('--subject TEXT', 'Optional subject line override for the new draft') { |v| options[:subject] = v }
opts.on('--title-prefix TEXT', 'Title prefix for the new draft') { |v| options[:title_prefix] = v }
opts.on('--dry-run', 'Preview generated event HTML without Mailchimp API calls') { options[:dry_run] = true }
end.parse!
unless File.exist?(DATA_FILE)
warn "Data file not found: #{DATA_FILE}"
exit 1
end
raw_events = YAML.safe_load(File.read(DATA_FILE))
unless raw_events.is_a?(Array)
warn "Expected an array in #{DATA_FILE}"
exit 1
end
today = Date.today
cutoff = today + options[:days_ahead]
selected_events = raw_events.select { |event| (d = parse_end_date(event['dates'])) && d >= today && d <= cutoff }
.first(options[:limit])
if selected_events.empty?
warn "No upcoming events found between #{today} and #{cutoff}."
exit 1
end
generated_html = build_events_html(selected_events)
if options[:dry_run]
puts generated_html
puts "\nIncluded #{selected_events.size} events."
exit 0
end
api_key = ENV['MAILCHIMP_API_KEY'].to_s.strip
if api_key.empty?
warn 'MAILCHIMP_API_KEY is required unless using --dry-run.'
exit 1
end
source_id = options[:source_campaign_id].to_s.strip
if source_id.empty?
warn 'MAILCHIMP_SOURCE_CAMPAIGN_ID is required (or pass --source-campaign).'
exit 1
end
client = MailchimpClient.new(api_key)
replicated = client.request('POST', "/campaigns/#{source_id}/actions/replicate")
new_campaign_id = replicated['id']
content = client.request('GET', "/campaigns/#{new_campaign_id}/content")
base_html = content['html'] || ''
updated_html = insert_newsletter_content(base_html, generated_html, options[:placeholder])
client.request('PUT', "/campaigns/#{new_campaign_id}/content", body: { html: updated_html })
title = "#{options[:title_prefix]} #{today}"
settings_payload = { title: title }
settings_payload[:subject_line] = options[:subject] if options[:subject]
client.request('PATCH', "/campaigns/#{new_campaign_id}", body: { settings: settings_payload })
puts "Created draft campaign: #{new_campaign_id}"
puts "Included #{selected_events.size} events (#{today} to #{cutoff})."
puts "Content insertion mode: #{base_html.include?(options[:placeholder]) ? 'placeholder replaced' : 'appended'}"