Last active
October 20, 2023 11:10
-
-
Save kaichanvong/5daa897bdd855813419cc7de3e594b5d to your computer and use it in GitHub Desktop.
Email (spam) to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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