Issue about Python imaplib library on method append - python

I'm trying to write an imapsync software that connects on host 1 to account1#host1.com and copy messages and folder to account2#host2.com host2.
Supposing I've already fetched the selected message with his UID with:
msg = connection.fetch(idOne, '(RFC822)'))
and msg is a good message, below you have the code I've tried to append the message:
date = connection.fetch(idOne, '(INTERNALDATE)')[1][0]
date = date.split("\"")[1]
authconnection1.append(folder, "", date, msg)
Error is:
ValueError: date_time not of a known type
I've tried many other possible solutions (with dateutil to convert date string to datetime object, using imaplib.Time2Internaldate I got the same error above ValueError: date_time not of a known type), but no one seems to work. I've searched around the network but nobody seems to have this issue.
Any idea? I'm getting very frustrated of it...
Thank you very much
Update:
I've resolved the date_time issue, because the "append" method of imaplib wants that date_time is an integer of seconds, so to retrieve the date I've written this code:
# Fetch message from host1
msg = connection.fetch(idOne, '(RFC822)')
# retrieve this message internal date
date = connection.fetch(idOne, '(INTERNALDATE)')[1][0].split("\"")[1]
# Cast str date to datetime object
date = parser.parse(date)
# Removes tzinfo
date = date.replace(tzinfo=None)
# Calculates total seconds between today and the message internal date
date = (datetime.datetime.now()-date).total_seconds()
# Makes the append of the message
authconnection1.append(folder, "", date, msg)
But now this fails with error:
TypeError: expected string or buffer
So the issue is only changed...
Any ideas?
Update (RESOLVED):
imaplib is not working fine, so I've made a workaround for append messages with right date/time. This is my code, I hope it will help everybody:
Function to convert date in right format:
def convertDate(date):
from time import struct_time
import datetime
from dateutil import parser
date = parser.parse(date)
date = date.timetuple()
return date
Main code:
#Get message
msg = authconnection.fetch(idOne, '(RFC822)')
#Get message date
date = authconnection.fetch(idOne, '(INTERNALDATE)')[1][0].split("\"")[1]
#Apply my function I've written above
date = convertDate(date)
#Append message with right date
authconnection1.append(folder, flags, date, msg[1][0][1])

Related

Issue with calculating password age date in Python

I'm rebuilding my PowerShell script with Python.
Sole purpose of that is to easily insert data to Postgre DB tables with Python.
PowerShell script works as it should, but when it comes to Python I encountered a obstacle.
In this loop, I gather all data - most like direct reports from specific AD account. As you may see, I already gather account name, name/surname, e-mail address and password last set date.
The problem is to calculate password age. In PS it was pretty easy, but when I ran a code in Python, I get an Exception has occurred: TypeError can't subtract offset-naive and offset-aware datetimes
Here is the part of the code:
reports = getDirectReports(managers_name)
for users in reports:
for user in users.directReports.values:
if 'cn=ext.' in user.lower():
user_details = getUserDetails(user)
print(user_details[0].cn.value)
print(user_details[0].givenName.value)
print(user_details[0].sn.value)
print(user_details[0].pwdLastSet.value)
print(user_details[0].mail.value)
current_date = datetime.now()
start_date = user_details[0].pwdLastSet.value
if (start_date == 0):
password_age = 'NULL'
else:
password_age = current_date - start_date ```

Win32com outlook filter by date stopped working

I have a script, that shows email's from some range of dates. It was working till 29 of april, and now, it doesn't filter correctly...
import win32com.client
import os
from datetime import datetime, timedelta
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace('MAPI')
messages = mapi.Folders("myemail#gmail.com").Folders("Inbox").Items
today = datetime.today()
start_time = today.replace(month=4,day=20, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
end_time = today.replace(month=5,day=2,hour=20, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + start_time
+ "' And [ReceivedTime] <= '" + end_time + "'")
messages.Sort("[ReceivedTime]", Descending=True)
for message in list(messages)[:5]:
print(message.Subject, message.ReceivedTime, message.SenderEmailAddress)
I was using https://www.codeforests.com/2021/05/16/python-reading-email-from-outlook-2/ as a tutorial.
Any ideas why it's not working as intended?
Script doesn't show any messages at all right now.
Make sure that dates are formatted properly. Although dates and times are typically stored with a Date format, the Find and Restrict methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. For example, a VBA macro which shows how to use the Format function:
sFilter = "[ReceivedTime] > '" & Format("5/02/22 3:30pm", "ddddd h:nn AMPM") & "'"
Also it makes sense to Sort the collection before applying filters.
Read more about the Find/FindNext or Restrict methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder

warnings.warn("DateTimeField %s received a naive datetime (%s)"

I use django simple-history to get history on my models
I then search the history results by date but I get the error below. How can I format the date?
RuntimeWarning: DateTimeField HistoricalIssue.history_date received a naive datetime (2022-04-13 10:34:32) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
def SearchByDate(request):
date_search = request.POST['date-search']
if date_search:
admin_hist_search_results = Issue.history.filter(history_date=date_search)
First, keep in mind that this is not an error, but "only" a warning. It mentions that the incoming timestamp (which you store in variable date_search) does not have timezone information, while you compare it with a timestamp field (history_date on model Issue) that does have timezone information. This could potentially lead to issues.
If you know the timezone coming in from the request, you can add that information to the timestamp, for example:
import pytz
date_as_string = request.POST['date-search']
parsed_date = datetime.strptime(date_as_string, '%Y-%m-%d')
amsterdam_timezone = pytz.timezone('Europe/Amsterdam')
date_search = amsterdam_timezone.localize(parsed_date)

localize date time in python

Overview I receive the timestamp from server_x, my application is on server_y, both are in different regions, my application calls server_x api and receives json which has timestamp, now i need to perform some calculation on server_y, for that i need to make sure that the timestamp i receive from server_x could be used to covert the local datetime of server_y , so both are in sync
I want to convert datetime.now() to the timezone I receive from server for e.g., UTC-07:00
Current solution, I pass server_timestamp to the function and then I pass its zone info to the datetime.now
Server_timestamp = "2020-04-04T10:24:49.000-0700"
dt = datetime.strptime(Server_timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
convert_local = datetime.now(dt.tzinfo)
Problem:
I need to save the timezone of the server in db and then use that instead of passing server_timestamp everytime, the tzinfo gives a type datetime.timezone = UTC-07:00, after storing this string how can I use it to change the localtime.
Here's a function that utilizes the datetime library to convert a datetime object from one timezone to another:
from datetime import datetime
import pytz
def convert_tz(dt, current_tz, out_tz):
return dt.replace(tzinfo=current_tz).astimezone(tz=out_tz)
now = datetime.now(tz=pytz.timezone('US/Eastern'))
convert = datetime.now().astimezone().tzinfo
print(now)
print(utc_to_local(now, now.tzinfo, convert))
Output:
2020-05-10 17:02:44.245703-04:00
2020-05-10 16:02:44.245703-05:00
I used the pytz library for demonstration purposes. For you, to get the server's timezone, use the line datetime.now().astimezone().tzinfo.
I implemented a solution.
I now save the last 5 char of the timestamp in the db "-0700".
time_zone = query_from_db
tz = datetime.strptime(time_zone, "%z")
datetime_now = datetime.now(tz.tzinfo)

How to get date in correct format for YouTube upload?

I am running a python script on OSX to upload video file (single_file) to YouTube:
# define recording date as date of file modification
# https://developers.google.com/youtube/v3/docs/videos#resource
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).isoformat("T")+"Z"
# define video title as file name
filename, file_extension = os.path.splitext(os.path.basename(single_file))
try:
initialize_upload(youtube, args, single_file, title, recordingDate)
except HttpError, e:
print " An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
in some cases it works well, but in others Google returns the following error -
Invalid value for: Invalid format: \"2017-09-22T22:50:55Z\" is malformed at \"Z\"
How should I fix it to get correct date from the file? YouTube expects the value in ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) format.
The link you shared in your question clearly states the format
The value is specified in ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) format.
So your issue is when then the microsecond info is not available, the isoformat will not have microsecond. Below code shows the difference
>>> current_date = datetime.now()
>>> current_date.isoformat()
'2018-05-20T10:18:26.785085'
>>> current_date.replace(microsecond=0).isoformat()
'2018-05-20T10:18:26'
So for the files which it works the microsecond would be coming is non-zero. So solution is simple
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).replace(microsecond=0).isoformat("T")+".0Z"
This will make sure the microsecond is always truncated and set as .0 later

Categories

Resources