Created
July 8, 2018 08:10
-
-
Save modernNeo/374e6e62bdecbfdafa3be5e5c7fe7608 to your computer and use it in GitHub Desktop.
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
from time import strptime | |
import datetime | |
import base64 | |
#code works if the body contains a date stamp using the following format | |
# Sat, 7 Jul 2018 19:53:39 -0700 (PDT) | |
# example body I used started with following string | |
# Delivered-To: [email protected]\nReceived: by 2002:adf:885c:0:0:0:0:0 with SMTP id e28-v6csp1111454wre;\n Sat, 7 Jul 2018 19:53:39 -0700 (PDT) | |
def reverseFind(string, startingIndex, charToFind): | |
for i in range(startingIndex-1, 0, -1): | |
if string[i] is charToFind: | |
return i | |
def extract_date(decoded_email_body): | |
index = reverseFind(decoded_email_body,decoded_email_body.index("(PDT)"), " ") | |
last_index = reverseFind(decoded_email_body,index, " ") | |
for x in range(0,6): | |
index = reverseFind(decoded_email_body,index, " ") | |
return decoded_email_body[index+1:last_index] | |
def convert_datetime_string_to_naive_datetime_object(date_from_email): | |
indexBeforeDate = date_from_email.find(" ", 1) | |
indexAfterDay = date_from_email.find(" ", indexBeforeDate+ 1) | |
day = int(date_from_email[indexBeforeDate+1:indexAfterDay]) | |
indexAfterMonth = date_from_email.find(" ", indexAfterDay +1) | |
month = date_from_email[indexAfterDay+1:indexAfterMonth] | |
month = int(strptime(month,'%b').tm_mon) | |
indexAfterYear = date_from_email.find(" ", indexAfterMonth + 1) | |
year = int(date_from_email[indexAfterMonth+1:indexAfterYear]) | |
indexAfterHour = date_from_email.find(":", indexAfterYear + 1) | |
hour = int(date_from_email[indexAfterYear+1:indexAfterHour]) | |
indexAfterMinute = date_from_email.find(":", indexAfterHour + 1) | |
minute = int(date_from_email[indexAfterHour+1:indexAfterMinute]) | |
second = int(date_from_email[indexAfterMinute+1:]) | |
return datetime.datetime(year, month, day, hour, minute, second) | |
body=str(base64.b64decode(body)) | |
date=extract_date(body) | |
datetime_date=convert_datetime_string_to_naive_datetime_object(date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment