From: Colin McCabe Date: Thu, 14 Jan 2010 01:15:33 +0000 (-0800) Subject: Add snarf_gmail.rb script X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d1bd5567451a40eb8ed2382f21d1bc4a148137b;p=cmccabe-bin Add snarf_gmail.rb script --- diff --git a/snarf_gmail.rb b/snarf_gmail.rb new file mode 100755 index 0000000..52aed0e --- /dev/null +++ b/snarf_gmail.rb @@ -0,0 +1,86 @@ +#!/usr/bin/env ruby + +# +# snarf_gmail.rb +# +# Copies mail from a gmail account +# You need ruby 1.9 for this +# You need the password gem for this +# +# Problem: this appears to only download some mails (usually around 383 or +# so). One workaround is to run this multiple times. Still not sure if there +# is a better workaround. +# +# Colin McCabe +# + +require 'net/pop' +require 'optparse' +require 'ostruct' +require 'password' + +class MyOptions + def self.parse(args) + opts = OpenStruct.new + + # Fill in $opts values + parser = OptionParser.new do |myparser| + myparser.banner = "Usage: #{ File.basename($0) } [opts]" + myparser.separator("Specific options:") + myparser.on("--username USERNAME", "-u", + "Email account to fetch. (example: \ +RareCactus@gmail.com)") do |u| + opts.username = u + end + myparser.on("--dry-run", "-d", + "Dry run. State what would be done without actually \ +doing it.") do |a| + opts.dry_run = true + end + end + + parser.parse!(args) + raise "must give a username" unless opts.username + return opts + end +end + +# MAIN +begin + $opts = MyOptions.parse(ARGV) +rescue Exception => msg + $stderr.print("#{msg}.\nType --help to see usage information.\n") + exit 1 +end + +puts "type password for #{$opts.username}" +password = gets +password.chomp! + +Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) +Net::POP3.start("pop.gmail.com", 995, $opts.username, password) do |pop| + #pop.reset() + mails = pop.mails + n_mails = pop.n_mails + puts "found #{n_mails} mails." + if ($opts.dry_run) + puts "successfully connected." + exit 0 + end + count = 0 + mails.each do |mail| + fname = mail.unique_id + #fname = sprintf("%08d", count) + File.open(fname, 'w+') do|f| + f.write mail.pop + end + count = count + 1 + if ((count % 100) == 0) + count = 0 + print "." + STDOUT.flush + end + end +end +puts "done." +exit 0