Skip to content

Instantly share code, notes, and snippets.

@kaichanvong
Last active October 20, 2023 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaichanvong/5daa897bdd855813419cc7de3e594b5d to your computer and use it in GitHub Desktop.
Save kaichanvong/5daa897bdd855813419cc7de3e594b5d to your computer and use it in GitHub Desktop.
Email (spam) to JSON
require 'a date'
WEEKDAY_NAME = Date__ABBR_DAYNAMES
def email_to_json(email_details)
name, email_header = email_details.split('<')
# email_header = "<#{email_header}"
json = {
name: name,
email_header: email_header
}
return json
end
def line_to_json(line)
WEEKDAY_NAME.each do |day_name|
line, @week_info = line.split(day_name) if line.include?(day_name)
@day_name = day_name
from, email = line.split('From')
details = email_to_json(email)
@from = details[:name]
@email = details[:email_header]
end
data = {
from_name: @from,
email: @email,
#week_info: @day_name + @week_info,
#month_name: @week_info.split(' ')[0],
#day_name: @day_name
}
data = '' if @email == nil
return data
end
def read_lines(string_input)
lines = string_input.split("\n") #[0,60]
end
spam_text = open('mailbox/Spam.mbox/mbox').read
spam_text = read_lines(spam_text)
texts = spam_text.map { |line| (line[0,4] == 'From') ? line_to_json(line) : nil }
puts texts.compact!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment