Skip to content

Instantly share code, notes, and snippets.

@supernullset
Created April 1, 2013 05:48
Show Gist options
  • Save supernullset/5283425 to your computer and use it in GitHub Desktop.
Save supernullset/5283425 to your computer and use it in GitHub Desktop.
mail-grouper one off script
require "tempfile"
require "fileutils"
require "csv"
require "time"
# -*- coding: utf-8 -*-
rents = "/Users/seanjw/Library/Thunderbird/Profiles/gdg1f8hw.default/ImapMail/imap.googlemail.com/Parents"
output = "/Users/seanjw/Dropbox/Programming-Languages/Ruby/rents-email-dates.txt"
odd_output = "/Users/seanjw/Dropbox/Programming-Languages/Ruby/odd-rents-dates.txt"
input = File.open(rents, "r")
dirty_output= File.open(output, "r+")
odd = File.open(odd_output, "w")
count = 0
prefix = /^Date: /
input.each do |line|
if line =~ prefix
dirty_output.puts line.split(prefix).last
count += 1
end
end
input.close
dirty_output.close
puts "TOTAL: #{count}"
dirty_output= File.open(output, "r")
temp = Tempfile.new("good_data")
odd_count = 0
standard = /\d{1,2} [a-zA-z]{3} \d{4}/
dirty_output.each do |line|
if line !~ standard
odd.puts line
odd_count += 1
else
temp.puts line
end
end
dirty_output.close
odd.close
FileUtils.mv(temp.path, output)
puts "ODD: #{odd_count}"
temp.close
pre_csv = File.open(output)
def parseEmailDateLine(line)
arr = line.split
day = arr[0].chomp(",")
time = arr[4].split(":").map{|s| s.to_i }
offset = arr[5].length == 5 ? arr[5].insert(3,":") : "+00:00"
row = []
time = Time.new(arr[3], arr[2], arr[1], time[0], time[1], time[3], offset)
local = time.getlocal
row << local
row << arr[0].chomp(",")
row << [arr[1], arr[2], arr[3]].join(" ")
row << local.hour
row << local.day
row << time.year
end
target_csv = CSV.open("rents-email.csv", "w") do |target|
pre_csv.each do |line|
line_csv = parseEmailDateLine(line)
target << line_csv
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment