Improve stability.
This commit is contained in:
parent
f127dfd276
commit
6e3dd9e38a
3 changed files with 74 additions and 51 deletions
2
Gemfile
2
Gemfile
|
@ -2,8 +2,10 @@ source "https://rubygems.org"
|
||||||
|
|
||||||
gem 'mail'
|
gem 'mail'
|
||||||
gem 'json'
|
gem 'json'
|
||||||
|
#gem 'bson'
|
||||||
gem 'thor'
|
gem 'thor'
|
||||||
gem 'pry'
|
gem 'pry'
|
||||||
|
gem 'pry-rescue'
|
||||||
|
|
||||||
# manage remote API/web page
|
# manage remote API/web page
|
||||||
gem 'mechanize'
|
gem 'mechanize'
|
||||||
|
|
12
Gemfile.lock
12
Gemfile.lock
|
@ -10,8 +10,9 @@ GEM
|
||||||
http-cookie (1.0.2)
|
http-cookie (1.0.2)
|
||||||
domain_name (~> 0.5)
|
domain_name (~> 0.5)
|
||||||
i18n (0.6.0)
|
i18n (0.6.0)
|
||||||
json (1.6.2)
|
interception (0.5)
|
||||||
json (1.6.2-java)
|
json (1.8.3)
|
||||||
|
json (1.8.3-java)
|
||||||
mail (2.3.0)
|
mail (2.3.0)
|
||||||
i18n (>= 0.4.0)
|
i18n (>= 0.4.0)
|
||||||
mime-types (~> 1.16)
|
mime-types (~> 1.16)
|
||||||
|
@ -44,6 +45,9 @@ GEM
|
||||||
method_source (~> 0.8.1)
|
method_source (~> 0.8.1)
|
||||||
slop (~> 3.4)
|
slop (~> 3.4)
|
||||||
spoon (~> 0.0)
|
spoon (~> 0.0)
|
||||||
|
pry-rescue (1.4.2)
|
||||||
|
interception (>= 0.5)
|
||||||
|
pry
|
||||||
slop (3.5.0)
|
slop (3.5.0)
|
||||||
spoon (0.0.4)
|
spoon (0.0.4)
|
||||||
ffi
|
ffi
|
||||||
|
@ -68,4 +72,8 @@ DEPENDENCIES
|
||||||
mail
|
mail
|
||||||
mechanize
|
mechanize
|
||||||
pry
|
pry
|
||||||
|
pry-rescue
|
||||||
thor
|
thor
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
1.10.6
|
||||||
|
|
111
bin/crawl.rb
111
bin/crawl.rb
|
@ -1,27 +1,32 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require 'bundler/setup'
|
#require 'bundler/setup'
|
||||||
Bundler.require
|
#Bundler.require
|
||||||
|
|
||||||
|
require 'pry'
|
||||||
require 'zlib'
|
require 'zlib'
|
||||||
require 'net/imap'
|
require 'net/imap'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
require 'pry'
|
|
||||||
require 'mechanize'
|
require 'mechanize'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'hash_validator'
|
require 'hash_validator'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
require 'thor'
|
||||||
|
require 'json'
|
||||||
|
require 'mail'
|
||||||
|
require 'colorize'
|
||||||
|
|
||||||
#Net::IMAP.debug = true
|
#Net::IMAP.debug = true
|
||||||
|
|
||||||
class Hash
|
class Hash
|
||||||
#take keys of hash and transform those to a symbols
|
#take keys of hash and transform those to a symbols
|
||||||
def self.transform_keys_to_symbols(value)
|
def self.transform_keys_to_symbols(value)
|
||||||
return value if not value.is_a?(Hash)
|
return value if not value.is_a?(Hash)
|
||||||
hash = value.inject({}) do |memo,(k,v)|
|
hash = value.inject({}) do |memo,(k,v)|
|
||||||
memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo
|
memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo
|
||||||
end
|
end
|
||||||
return hash
|
return hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Epafi
|
module Epafi
|
||||||
|
@ -64,13 +69,13 @@ module Epafi
|
||||||
|
|
||||||
def connect!
|
def connect!
|
||||||
@browser.get(@config[:crm][:baseurl] + CRM_LOGIN_URL) do |page|
|
@browser.get(@config[:crm][:baseurl] + CRM_LOGIN_URL) do |page|
|
||||||
my_page = page.form_with(:action => '/authentication') do |f|
|
page.form_with(action: '/authentication') do |f|
|
||||||
f['authentication[username]'] = @config[:crm][:login]
|
f['authentication[username]'] = @config[:crm][:login]
|
||||||
f['authentication[password]'] = @config[:crm][:password]
|
f['authentication[password]'] = @config[:crm][:password]
|
||||||
end.click_button
|
end.click_button
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue Mechanize::ResponseCodeError => e
|
rescue Mechanize::ResponseCodeError
|
||||||
raise "Authentication error. Verify your credentials."
|
raise "Authentication error. Verify your credentials."
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -142,8 +147,8 @@ module Epafi
|
||||||
TMPMAIL_FILE = '.tmpmail'
|
TMPMAIL_FILE = '.tmpmail'
|
||||||
|
|
||||||
def initialize config
|
def initialize config
|
||||||
@saved_key = 'RFC822'
|
@saved_key = 'RFC822'
|
||||||
@filter_headers = 'BODY[HEADER.FIELDS (FROM TO Subject)]'.upcase
|
@filter_headers = 'BODY[HEADER.FIELDS (FROM TO Subject)]'.upcase
|
||||||
@config = config
|
@config = config
|
||||||
@imap = nil
|
@imap = nil
|
||||||
@contact_manager = ContactManager.new config
|
@contact_manager = ContactManager.new config
|
||||||
|
@ -151,31 +156,36 @@ module Epafi
|
||||||
|
|
||||||
|
|
||||||
def connect!
|
def connect!
|
||||||
@imap = Net::IMAP.new(
|
@imap = Net::IMAP.new(
|
||||||
@config[:imap][:server],
|
@config[:imap][:server],
|
||||||
:ssl => {:verify_mode => OpenSSL::SSL::VERIFY_NONE},
|
ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE},
|
||||||
:port => 993
|
port: 993
|
||||||
)
|
)
|
||||||
@imap.login(@config[:imap][:login], @config[:imap][:password])
|
@imap.login(@config[:imap][:login], @config[:imap][:password])
|
||||||
|
#@imap.select(SOURCE_MAILBOX)
|
||||||
end
|
end
|
||||||
|
|
||||||
def disconnect!
|
def disconnect!
|
||||||
imap.logout
|
imap.logout
|
||||||
imap.disconnect
|
imap.disconnect
|
||||||
end
|
end
|
||||||
|
|
||||||
MAIL_REGEXP = /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
|
MAIL_REGEXP = /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
|
||||||
|
|
||||||
def examine_message message
|
def examine_message message
|
||||||
m = Mail.read_from_string message.attr[@saved_key]
|
m = Mail.read_from_string message.attr[@saved_key]
|
||||||
return if m.from.nil?
|
return if m.from.nil?
|
||||||
return if m.to.nil?
|
return if m.to.nil?
|
||||||
|
|
||||||
|
|
||||||
emails = Set.new
|
emails = Set.new
|
||||||
emails.merge m.from
|
begin
|
||||||
emails.merge m.to if m.to
|
emails.merge m.from
|
||||||
emails.merge m.cc if m.cc
|
emails.merge [m.to].flatten if m.to
|
||||||
|
emails.merge [m.cc].flatten if m.cc
|
||||||
|
rescue => e
|
||||||
|
binding.pry
|
||||||
|
end
|
||||||
|
|
||||||
body_emails = Set.new
|
body_emails = Set.new
|
||||||
m.body.parts.each do |part|
|
m.body.parts.each do |part|
|
||||||
|
@ -215,9 +225,9 @@ module Epafi
|
||||||
all_addr.each do |key, list|
|
all_addr.each do |key, list|
|
||||||
list.each do |addr|
|
list.each do |addr|
|
||||||
addr_str = if remaining_emails.map{|e,t| e}.include? addr then
|
addr_str = if remaining_emails.map{|e,t| e}.include? addr then
|
||||||
addr.yellow.on_black
|
addr.yellow.on_black
|
||||||
else addr
|
else addr
|
||||||
end
|
end
|
||||||
str = "%4s: %s" % [key.to_s.upcase, addr_str]
|
str = "%4s: %s" % [key.to_s.upcase, addr_str]
|
||||||
puts str
|
puts str
|
||||||
end
|
end
|
||||||
|
@ -252,7 +262,7 @@ module Epafi
|
||||||
system "formail < #{TMPMAIL_FILE}.2 > #{TMPMAIL_FILE}"
|
system "formail < #{TMPMAIL_FILE}.2 > #{TMPMAIL_FILE}"
|
||||||
system "mutt -R -f #{TMPMAIL_FILE}"
|
system "mutt -R -f #{TMPMAIL_FILE}"
|
||||||
end
|
end
|
||||||
rescue Encoding::ConverterNotFoundError => e
|
rescue Encoding::ConverterNotFoundError
|
||||||
STDERR.puts "ERROR: encoding problem in email. Unable to convert."
|
STDERR.puts "ERROR: encoding problem in email. Unable to convert."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -261,34 +271,36 @@ module Epafi
|
||||||
end
|
end
|
||||||
|
|
||||||
def examine_all
|
def examine_all
|
||||||
@imap.list('', '*').each do |mailbox|
|
@imap.list('', '*').each do |mailbox|
|
||||||
puts "\nMAILBOX #{mailbox.name}"
|
puts "\nMAILBOX #{mailbox.name}".yellow
|
||||||
next unless mailbox.name =~ /#{@config[:imap][:pattern]}/
|
next unless mailbox.name =~ /#{@config[:imap][:pattern]}/
|
||||||
@imap.examine mailbox.name
|
@imap.examine mailbox.name
|
||||||
|
|
||||||
puts "Searching #{mailbox.name}"
|
puts "Searching #{mailbox.name}"
|
||||||
messages_in_mailbox = @imap.responses['EXISTS'][0]
|
messages_in_mailbox = @imap.responses['EXISTS'][0]
|
||||||
unless messages_in_mailbox
|
if not messages_in_mailbox then
|
||||||
say "#{mailbox.name} does not have any messages"
|
say "#{mailbox.name} does not have any messages"
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
ids = @imap.search('SINCE 1-Jan-2001')
|
@imap.select mailbox.name #GYR: TEST
|
||||||
|
ids = @imap.search('SINCE 1-Jan-2001')
|
||||||
# NOT OR TO "@agilefant.org" CC "@agilefant.org"')
|
# NOT OR TO "@agilefant.org" CC "@agilefant.org"')
|
||||||
if ids.empty?
|
if ids.empty?
|
||||||
puts "\tFound no messages"
|
puts "\tFound no messages"
|
||||||
else
|
else
|
||||||
examine_message_list ids
|
examine_message_list mailbox.name, ids
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def examine_message_list ids
|
def examine_message_list mailbox_name, ids
|
||||||
ids.each do |id|
|
ids.each do |id|
|
||||||
|
@imap.select mailbox_name #GYR: TEST
|
||||||
message = imap.fetch(id, [@saved_key])[0]
|
message = imap.fetch(id, [@saved_key])[0]
|
||||||
examine_message message
|
examine_message message
|
||||||
end
|
end
|
||||||
rescue IOError => e
|
rescue IOError
|
||||||
# re-connect and try again
|
# re-connect and try again
|
||||||
connect!
|
connect!
|
||||||
retry
|
retry
|
||||||
|
@ -299,13 +311,13 @@ module Epafi
|
||||||
|
|
||||||
CONFIG_FILE = 'config/secrey.yml'
|
CONFIG_FILE = 'config/secrey.yml'
|
||||||
|
|
||||||
include Thor::Actions
|
include Thor::Actions
|
||||||
default_task :crawl
|
default_task :crawl
|
||||||
|
|
||||||
|
|
||||||
desc 'crawl', 'Crawls email to save mails'
|
desc 'crawl', 'Crawls email to save mails'
|
||||||
def crawl
|
def crawl
|
||||||
saved_info = []
|
#saved_info = []
|
||||||
parse_configuration
|
parse_configuration
|
||||||
|
|
||||||
## Run application
|
## Run application
|
||||||
|
@ -315,7 +327,7 @@ module Epafi
|
||||||
app.examine_all
|
app.examine_all
|
||||||
#pp saved_info
|
#pp saved_info
|
||||||
app.disconnect!
|
app.disconnect!
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize *args
|
def initialize *args
|
||||||
@config = {}
|
@config = {}
|
||||||
|
@ -351,3 +363,4 @@ module Epafi
|
||||||
end
|
end
|
||||||
|
|
||||||
Epafi::Crawler.start
|
Epafi::Crawler.start
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue