Created
October 11, 2017 12:52
-
-
Save daxadax/865bd2212bb9bf09ced511e1192b3aa6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'gmail' | |
require 'csv' | |
require 'io/console' | |
class EmailExtractor | |
def self.run | |
new.run | |
end | |
def initialize | |
printf "Enter your gmail username: " | |
username = gets.chomp | |
printf "Enter you password: " | |
password = STDIN.noecho(&:gets).chomp | |
@gmail = Gmail.new(username, password) | |
end | |
def run | |
printf "\nConnecting..." | |
printf "\nCollecting emails..." | |
all_messages | |
printf "\nWriting to CSV..." | |
CSV.open("emails_test.csv", "w+") do |csv| | |
csv << %w[emails] | |
all_messages.uniq.each_with_index do |email, i| | |
csv << [email] | |
printf '.' if (i%15).zero? | |
end | |
end | |
printf "\nWriting complete." | |
printf "\nLogging you out..." | |
gmail.logout | |
printf "\nGoodbye.\n" | |
end | |
private | |
attr_reader :gmail | |
def all_messages | |
sent + all_mail | |
end | |
def all_mail | |
@all_mail ||= gmail.mailbox("[Gmail]/All Mail").emails.map(&:from).uniq | |
end | |
def sent | |
@sent ||= gmail.mailbox("[Gmail]/Sent Mail").emails.map(&:to).uniq | |
end | |
end | |
EmailExtractor.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment