Преглед изворни кода

Merge branch 'master' of github.com:noahvogt/mini-project

Simon Hammer пре 3 година
родитељ
комит
3b7f37ba3c
1 измењених фајлова са 23 додато и 22 уклоњено
  1. 23 22
      app/src/main/python/mailFunctions.py

+ 23 - 22
app/src/main/python/mailFunctions.py

@@ -1,8 +1,13 @@
-import imaplib, smtplib, ssl, email, os, json
+import imaplib
+import smtplib
+import ssl
+import email
+import json
 from itertools import chain
 
-# format raw string you get from fetching mails
+
 def stringCompiling(inputIterable):
+    # format raw string you get from fetching mails
     # remove first nested iterables
     try:
         unitered = list(chain.from_iterable(inputIterable))
@@ -20,10 +25,6 @@ def stringCompiling(inputIterable):
                     except UnicodeDecodeError:
                         nonNoneList.append(str(item.decode("iso-8859-1")))
                     except AttributeError:
-                        #print(item)
-                        #print(type(item))
-                        #print(inputIterable)
-                        #print(nonNoneList)
                         exit()
                 else:
                     nonNoneList.append(item)
@@ -37,9 +38,11 @@ def stringCompiling(inputIterable):
     else:
         return ""
 
+
 def errorMsgExit(error_msg):
     print("Error: " + error_msg)
 
+
 def checkConnection(host, username, password, port):
     try:
         connection = imaplib.IMAP4_SSL(host, port)
@@ -56,6 +59,7 @@ def checkConnection(host, username, password, port):
 
         return False
 
+
 def connect(host, username, password, port):
     connect = imaplib.IMAP4_SSL(host, port)
     connect.login(username, password)
@@ -65,6 +69,7 @@ def connect(host, username, password, port):
         pass
     return connect
 
+
 def listMailboxes(connection):
     mailboxes = connection.list()
     formatted_mailbox_list = []
@@ -74,21 +79,20 @@ def listMailboxes(connection):
             for raw_box_string in items:
                 box_string = str(raw_box_string)
                 # TODO: handle cases when folder contains subfolders
-                modified_box_string = (box_string[box_string.find('"/" ')+4:-1])
+                modified_box = (box_string[box_string.find('"/" ')+4:-1])
 
                 # strip unneeded "'s surrounding the folder name
-                if modified_box_string.startswith('"') and modified_box_string.endswith('"'):
-                    modified_box_string = modified_box_string[1:-1]
+                if modified_box.startswith('"') and modified_box.endswith('"'):
+                    modified_box = modified_box[1:-1]
 
-                formatted_mailbox_list.append(modified_box_string)
+                formatted_mailbox_list.append(modified_box)
 
     connection.logout()
     return formatted_mailbox_list
 
 
-
-    # check that there are no bytes anymore that cannot be dumped into a json
 def verifyNoBytes(messages, output_list):
+    # check that there are no bytes anymore that cannot be dumped into a json
     for messages in output_list:
         for item in messages:
             #print(type(item))
@@ -104,6 +108,7 @@ def verifyNoBytes(messages, output_list):
                 print("ERROREXIT")
                 exit()
 
+
 def fetchMails(connection, inbox, folderLocal):
     #print("###" + inbox + "###")
     #print(type(inbox))
@@ -115,8 +120,6 @@ def fetchMails(connection, inbox, folderLocal):
     #print("status-------\n" + status)
     #print("messages-------\n" + str(messages))
 
-    # number of top emails to fetch
-    #N = 3
     # total number of emails
     messages_int = int(messages[0])
     #print("message_int------\n" + str(messages_int))
@@ -189,31 +192,29 @@ def fetchMails(connection, inbox, folderLocal):
             else:
                 output_dict['seen'] = "False"
                 # make sure the fetch command doesn't add a SEEN flag
-                connection.store(num, '-FLAGS', '(\Seen)')
+                connection.store(num, '-FLAGS', '(\\Seen)')
 
             output_list.append(output_dict)
 
-
-
     connection.close()
     connection.logout()
 
     verifyNoBytes(messages, output_list)
 
-    print("Finstep")
-
     return json.dumps(output_list)
 
 
-def sendStarttls(host, sendingMail, receivingMail, password, message="", subject="", port=587, cc=[], bcc=[]):
+def sendStarttls(host, sendingMail, receivingMail, password, message="",
+                 subject="", port=587, cc=[], bcc=[]):
     context = ssl.create_default_context()
 
     if type(cc) is not str:
         cc = ",".join(cc)
     if type(bcc) is not str:
         bcc = ",".join(bcc)
-    utf8Message = "Subject: " + subject + "\nCC: " + cc + "\nBCC: " + bcc + "\n\n" + message
-    decoded=utf8Message.encode('cp1252').decode('utf-8')
+    utf8Message = ("Subject: " + subject + "\nCC: " + cc + "\nBCC: " + bcc +
+                   "\n\n" + message)
+    decoded = utf8Message.encode('cp1252').decode('utf-8')
 
     with smtplib.SMTP(host, port) as serverConnection:
         serverConnection.starttls(context=context)