Last active
October 24, 2015 13:13
-
-
Save Cohen-Carlisle/2e008d18a7ce5ddbaa40 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
#!/usr/bin/env ruby | |
require 'httparty' | |
require 'sendgrid-ruby' | |
if (ARGV.length > 2 || ARGV.length == 0) | |
abort 'Usage: wikispam SEND_TO_EMAIL_ADDRESS [NUMBER_OF_EMAILS]' | |
end | |
send_to = ARGV[0] | |
n = (ARGV.length == 1) ? 1 : ARGV[1].to_i | |
file_lines = File.readlines(File.expand_path('~/.wikispam')) | |
user = file_lines[0].chomp | |
key = file_lines[1].chomp | |
from_email = file_lines[2].chomp | |
author = file_lines[3] ? file_lines[3].chomp : nil | |
client = SendGrid::Client.new(api_user: user, api_key: key) | |
url = 'http://en.wikipedia.org/w/api.php?action=query&generator=random&'\ | |
'grnnamespace=0&prop=extracts&format=json' | |
(1..n).each do |i| | |
parsed_response = HTTParty.get(url).parsed_response | |
raise "#{parsed_response['error']['info']}" if parsed_response['error'] | |
title = parsed_response['query']['pages'].values[0]['title'] | |
article = parsed_response['query']['pages'].values[0]['extract'] | |
mail = SendGrid::Mail.new do |m| | |
m.to = send_to | |
m.from = from_email | |
m.from_name = author | |
m.subject = title | |
m.html = article | |
end | |
mail_response = client.send(mail) | |
puts "Email #{i}: #{mail_response.body['message']} (#{title})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment