Skip to content

Instantly share code, notes, and snippets.

@bensie
Created December 17, 2012 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bensie/4314506 to your computer and use it in GitHub Desktop.
Save bensie/4314506 to your computer and use it in GitHub Desktop.
Scrape Postmark outbound for all emails sent - capture address, subject, and timestamp and write to JSON
require "mechanize"
require "nokogiri"
require "json"
emails = []
a = Mechanize.new
a.get("https://postmarkapp.com/login") do |login_page|
servers_page = login_page.form_with(:id => 'new_user_session') do |form|
form.send "user_session[login]", ARGV[0]
form.send "user_session[password]", ARGV[1]
end.submit
overview_page = a.click(servers_page.link_with(text: ARGV[2]))
outbound_page = a.click(overview_page.link_with(text: "Outbound"))
parsed = Nokogiri::HTML(outbound_page.body)
pages = []
parsed.xpath('//div[@class="pagination"]').css("a").each do |link|
pages << link.content.to_i
end
total_pages_for_server = pages.max
(1..total_pages_for_server).each do |page_number|
outbound_listing = a.get("https://postmarkapp.com/servers/41039/delivery_events/outbound?page=#{page_number}")
parsed_listing = Nokogiri::HTML(outbound_listing.body)
parsed_listing.xpath('//div[@id="activity"]/div').each do |activity_item|
address = activity_item.css("h3").css("a").text
subject = activity_item.css("p")[0].text.strip
timestamp = activity_item.css("p")[1].css("span").text
emails << { address: address, subject: subject, timestamp: timestamp }
end
end
File.open("out.json", 'w') {|f| f.write(emails.to_json) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment