Created
December 17, 2012 00:15
-
-
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
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
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