From 3d1bd5567451a40eb8ed2382f21d1bc4a148137b Mon Sep 17 00:00:00 2001 From: Colin McCabe Date: Wed, 13 Jan 2010 17:15:33 -0800 Subject: [PATCH] Add snarf_gmail.rb script --- snarf_gmail.rb | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 86 insertions(+), 0 deletions(-) create mode 100755 snarf_gmail.rb 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 -- 1.6.6.rc1.39.g9a42