Created
September 23, 2021 23:54
-
-
Save devondragon/cbf920a9917281984891ec55bbf5d9a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will pick a random winner from your MailChimp audience. Useful for giveaways! | |
# Just set the Audience ID, API_KEY, and Datacenter with the correct information for your mailing list | |
require 'rubygems' | |
require 'net/http' | |
require 'json' | |
AUDIENCE_ID = "YYYYYYYY" | |
API_KEY = "XXXXXXX" | |
DATACENTER = "us4" | |
BASE_URL = "https://" + DATACENTER + ".api.mailchimp.com/export/1.0/list/" | |
def getAudience | |
emails = [] | |
# This is the REST URL that will be hit. Change the jql query if you want to adjust the query used here | |
uri = URI(BASE_URL + '?id=' + AUDIENCE_ID + "&apikey=" + API_KEY) | |
Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| | |
request = Net::HTTP::Get.new(uri) | |
response = http.request request | |
# If the response was good, then grab the data | |
if response.code =~ /20[0-9]{1}/ | |
body = response.body | |
body.each_line do |line| | |
#email = line.s | |
ind = line.index('"', 2) | |
email = line[2..ind-1] | |
# puts email | |
emails.append(email) | |
end | |
puts "Loaded #{emails.length()} emails." | |
else | |
raise StandardError, "Unsuccessful response code " + response.code + " for issue " + issue | |
end | |
end | |
return emails | |
end | |
def getRandomEntry(emailArray) | |
length = emailArray.length() | |
winnerPos = Random.rand(length) | |
winner = emailArray[winnerPos] | |
puts "Winner:" + winner | |
end | |
def main () | |
emails = getAudience | |
getRandomEntry(emails) | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment