#!/usr/bin/env ruby

#======
# Copyright (c) 2007 ActBlue

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#====




#**********
# IMPORTANT
#**********
# Before running the script the first time, you need to seed the
# directory where the script resides with a version of foiacn.dta
# called foiacn_old.dta  This filed can be found at: 
# ftp.fec.gov/FEC/foiacn.dta




require 'net/ftp'
require 'gmailer'  #located at http://rubyforge.org/projects/gmailutils
require 'zip/zipfilesystem'  #located at http://rubyzip.sourceforge.net/


# Enter your gmail login info here (without the @gmail.com):
gmailLogin =
gmailPass =

class Candidate
	def initialize(id, name, address, city, state, zip, url)
		@id = id
		@name = name
		@address = address
		@city = city
		@state = state
		@zip = zip
		@url = url
	end
	
	def to_s
		"#{@id} #{@name}\n#{@url}\n#{@address}\n#{@city}, #{@state} #{@zip}\n\n"
	end

	attr_reader :id, :name, :address, :city, :state, :zip, :url
end

oldHash = Hash.new
currentHash = Hash.new
holdArray = Array.new
lineCount = 0
holdMsg = ""

ftp = Net::FTP.new('ftp.fec.gov')
ftp.debug_mode=true
ftp.login(user = "anonymous", passwd = "anonymous")
ftp.chdir("FEC/")
ftp.getbinaryfile("cn08.zip")
ftp.close

Zip::ZipFile.open("cn08.zip") {
  |zipfile|
  zipfile.extract("foiacn.dta","foiacn.dta")
}

puts "Zip file extracted"

currentFile = File.open("foiacn.dta", "r")
oldFile = File.open("foiacn_old.dta", "r")

#Only import lines from file that are DEMs and have a committee number
oldFile.each_line do |line|
	if line.to_s[47..49] == "DEM"
		if line.to_s[152..160] != "         "
			oldHash[line.to_s[152..160]] = Candidate.new(line.to_s[152..160],line.to_s[9..46].strip.split(" ").each{|word| word.capitalize!}.join(" "),"#{line.to_s[59..92].strip.split(" ").each{|word| word.capitalize!}.join(" ")} #{line[93..126].strip.split(" ").each{|word| word.capitalize!}.join(" ")}",line.to_s[127..144].strip.split(" ").each{|word| word.capitalize!}.join(" "),line.to_s[145..146],line.to_s[147..151],"http://query.nictusa.com/cgi-bin/fecimg/?#{line.to_s[152..160]}")
		end
	end
end

currentFile.each_line do |line|
	if line.to_s[47..49] == "DEM"
		if line.to_s[152..160] != "         "
			currentHash[line.to_s[152..160]] = Candidate.new(line.to_s[152..160],line.to_s[9..46].strip.split(" ").each{|word| word.capitalize!}.join(" "),"#{line.to_s[59..92].strip.split(" ").each{|word| word.capitalize!}.join(" ")} #{line[93..126].strip.split(" ").each{|word| word.capitalize!}.join(" ")}",line.to_s[127..144].strip.split(" ").each{|word| word.capitalize!}.join(" "),line.to_s[145..146],line.to_s[147..151],"http://query.nictusa.com/cgi-bin/fecimg/?#{line.to_s[152..160]}")
		end
	end
end

puts "Data in Hash Table"

oldFile.close
currentFile.close

# Clean directory for next run of script
File.delete("cn08.zip")
File.delete("foiacn_old.dta")
File.rename("foiacn.dta", "foiacn_old.dta")

#Compare the old data to the new data
currentHash.each do |curKey, curValue|
	if oldHash.has_key?(curKey) == false
		holdArray.push(curValue)
	end
end

puts "Committee's Compared"

#Local logging for testing without emailing
msgFile = File.new("textlog.txt", File::CREAT)
msgFile.close
msgFile = File.new("textlog.txt", "w")
holdArray.each {|item| msgFile.puts item}
msgFile.close

holdArray.each do |item|
	holdMsg = holdMsg + item.to_s
end

GMailer.connect(gmailLogin, gmailPass) do |g|
    g.send(
      :from => "#{gmailLogin}@gmail.com",
      :to => "#{gmailLogin}@gmail.com",
      :subject => "Newly filed Democratic FEC candidate committees",
      :body => holdMsg)
end
 
puts "Email Sent!"