-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom.py
More file actions
202 lines (164 loc) · 6.2 KB
/
com.py
File metadata and controls
202 lines (164 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#This implementation gets all mails from a running outlook instance witch are from a given sender and downloads the pdfs into the folder ./downloads
#Author: Jonathan Wyss
##Notes
#https://learn.microsoft.com/en-us/office/vba/api/outlook.items
#https://learn.microsoft.com/en-us/office/vba/api/outlook.attachment.saveasfile
import win32com.client
import os
from dotenv import load_dotenv
import sqlite3
from pop3 import err,log
import pythoncom
from gui import getResourcePath
import tempfile
def getAppDir():
path = tempfile.gettempdir() + "\\"
applocal = os.getenv('LOCALAPPDATA')
applocal = os.path.join(os.path.expanduser("~"),"AppData","Local")
appname = "Jira-Flow"
appdir = os.path.join(applocal,appname)
appdir += "\\"
return appdir
#PATH = os.getcwd() + "\\downloads\\"
PATH = tempfile.gettempdir() + "\\"
print(PATH)
INBOXNR = 6
load_dotenv(getAppDir() + "config")
pythoncom.CoInitialize()
filterName = os.getenv("FilterName")
class Entry:
def __init__(self,uid,path,creationDate):
self.uid = uid
self.path = path
self.creationDate = creationDate
def __eq__(self,other):
return self.path == other
def __str__(self):
return str(self.path) + " " + str(self.creationDate)
def init():
try:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
except:
log("error no connection to outlook",level=err.ERROR)
return outlook
def getEntryIDDb(filename="mail.db"):
connection = sqlite3.connect(getAppDir() + filename)
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS outlook (
date_added TEXT DEFAULT CURRENT_TIMESTAMP,
id INTEGER)""")
connection.commit()
cursor.execute("select * from outlook")
entryIDMails = cursor.fetchall()
for uid in entryIDMails:
log("EntryIDFromDB: ", uid,level=err.ULTRA)
connection.close()
return entryIDMails
def addEntryIDDb(entryIDList,filename="mail.db"):
connection = sqlite3.connect(getAppDir() + filename)
cursor = connection.cursor()
newAddedList = []
for uid in entryIDList:
#iUid = uid.decode().split()[1]
log("entryID:",uid)
cursor.execute("""SELECT EXISTS(SELECT 1 FROM outlook WHERE id = ?)""",(uid,))
doesExist = cursor.fetchone()
log("Return Exist:",doesExist,level=err.ULTRA)
if doesExist[0] == 0:
log("addUid to db:",uid)
cursor.execute("""INSERT INTO outlook(id) VALUES(?)""",(uid,))
newAddedList.append(uid)
connection.commit()
connection.close()
return newAddedList
def rmEntryIDDB(entryId,filename="mail.db"):
connection = sqlite3.connect(getAppDir() + filename)
cursor = connection.cursor()
cursor.execute("""DELETE FROM outlook WHERE id = ? RETURNING id""",(entryId,))
log("Remove result:",cursor.fetchone())
connection.commit()
connection.close()
def rmLastEntryIDDB(filename="mail.db"):
connection = sqlite3.connect(getAppDir() + filename)
cursor = connection.cursor()
cursor.execute("""SELECT * from outlook ORDER BY rowid DESC LIMIT 1""")
lastId = cursor.fetchone()
connection.close()
log("latest entry in db:",lastId)
rmEntryIDDB(lastId[1])
def getFileName(filename):
dotList = str(filename).split('.')
ending = dotList[len(dotList)-1]#use the last entry
return ending
def getMAPI(outlook):
try:
outlook = win32com.client.GetActiveObject("Outlook.Application")
except:
log("could not get active outlook instance!",level=err.ERROR)
return "ERROR MAPI"
outlook = outlook.GetNameSpace("MAPI")
return outlook
def getEntryIDMail(filterName,outlook,inboxnr=INBOXNR):
outlook = getMAPI(outlook)
if type(outlook) == str:#ERROR ocured
log("getEntryIDMail: ",outlook,level=err.ERROR)
yield outlook #pass up the error
return #stop generator
inbox = outlook.GetDefaultFolder(inboxnr)
print("Folder:",inbox.Name)
messages = inbox.Items
log("count messages:",len(messages))
for m in messages:
try:
sender = m.Sender
except:
log("no Sender object in message")
else:
if str(filterName).strip().lower() in str(m.Sender).strip().lower():
yield m.EntryID
else:
pass
#log("Sender",m.Sender,"does not match",filterName)
def downloadAllAttachements(fileEnding,filterName,outlook,path=PATH,inboxnr=INBOXNR):
outlook = getMAPI(outlook)
inbox = outlook.GetDefaultFolder(inboxnr)
print("Folder:",inbox.Name)
messages = inbox.Items
for m in messages:
try:
sender = m.Sender
except:
pass
else:
if str(filterName).lower() in str(m.Sender).lower():
print("From:",m.Sender,"Title:",m, "EntryID:",m.EntryID)
for att in m.Attachments:
print("Attachements:",att)
if getFileName(att) == fileEnding:
path = path + str(att)
att.SaveAsFile(path)
print("download to:",PATH)
def getAttachements(entryID,outlook,fileEnding="pdf",path=PATH,inboxnr=INBOXNR):
outlook = getMAPI(outlook)
inbox = outlook.GetDefaultFolder(inboxnr)
messages = inbox.Items
for m in messages:
if m.EntryID == entryID:
print("From:",m.Sender,"Title:",m, "EntryID:",m.EntryID)
for att in m.Attachments:
if getFileName(att) == fileEnding:
path = path + str(att)
yield Entry(m.EntryID,path,m.CreationTime)
def downloadAttachements(entryID,outlook,fileEnding="pdf",path=PATH,inboxnr=INBOXNR):
outlook = getMAPI(outlook)
inbox = outlook.GetDefaultFolder(inboxnr)
messages = inbox.Items
for m in messages:
if m.EntryID == entryID:
print("From:",m.Sender,"Title:",m, "EntryID:",m.EntryID)
for att in m.Attachments:
print("Attachements:",att)
if getFileName(att) == fileEnding:
path = path + str(att)
att.SaveAsFile(path)
print("download to:",path)