This repository was archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_POP.py
More file actions
149 lines (115 loc) · 5.66 KB
/
exec_POP.py
File metadata and controls
149 lines (115 loc) · 5.66 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
#!/usr/bin/python
# exec_POP.py
#### Written by:
# Erika Burdon
# Software Engineering Student
# University of Victoria British Columbia, CANADA
# https://github.com/eburdon
# eburdonGIT@gmail.com
# All comments and critques on this script are welcome!
#
# website: web.uvic.ca/~eburdon
# LinkedIn: /in/eburdon
# Riipen: /users/1017
########
# Connects to GMail and parses Transparent Language emails with POP3
import poplib # Getting emails
import email # unpacking MIME messages
from email import parser, FeedParser
from optparse import OptionParser
# Import MY modules
import myGlobals
from common import *
def executePOP():
# Connect to server
host = 'pop.gmail.com'
pop_conn = poplib.POP3_SSL(myGlobals.host)
# print pop_conn.getwelcome()
pop_conn.user(myGlobals.usrnm)
pop_conn.pass_(myGlobals.usrpss)
# Get ALL messages from server
messages = [pop_conn.retr(i) for i in range (1, len(pop_conn.list()[1]) + 1)]
messages = ["\n".join(mssg[1]) for mssg in messages]
# Parse message from email object
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
# SET UP DATA ARRAYS
matches = [] # Messages with 'German Word of Day' subject
delList = [] # Lists index vals of vocab emails for later del
newWord = [] # Array of all new german words (confirmation)
# Get emails with new vocabulary words; Construct 'matches'
get_vocab_messages(messages, matches, delList)
# Decide if program will continue (are there new vocab words?)
get_status_new_vocab(pop_conn, matches)
# I can now iterate through 'matches' and get body messages
for curr in matches:
for part in curr.walk():
# Multipart is just a container; Skip
if part.get_content_maintype() == 'multipart':
continue
if part.get_content_maintype() == 'text':
# Email HTML formating; Each msg has one. SKIP
if part.get_content_type() == 'text/hmtl':
continue;
# Actual content I want to parse
if part.get_content_type() == 'text/plain':
# Message is NOT a multipart; Payload
# returns a string of words (not list)
fullMsg=part.get_payload(decode=True).split()
# ----- Initalize write to file elements -----
wod = ["Word: "] #Of the day
meaning = ["Means: "]
prtspch = ["Part", "of", "speech:"]
exSentence = ["Example", "sentence:"]
exTrSentence = ["This", "means:"]
# --------------- Parsing vars ---------------
vFlag = False # Is word a verb?
exStart = 0 # Example sentence start (Ger)
exEnd = 0 # Example sentence end
## --------------- Parse email ---------------
# Get part of speech (verb, adjective, etc...)
get_part_speech(vFlag, prtspch, fullMsg)
# Get word (German) & meaning (english)
get_DE_word(vFlag,
newWord, wod, meaning, fullMsg)
# Get example/word in context of a sentence;
# Returns end of example so translations knows
# where to start.
exEnd = get_DE_example(fullMsg, exStart,
exEnd, exSentence)
# Get translation of example sentence
get_EN_example(fullMsg, exEnd, exTrSentence)
## --------- Add each entry to file ----------
# Opens the file for appending; pointer will
# be at the end of the file
# If file does not exist, it will be created
fp = open(myGlobals.saveToFile, "a")
add_element(fp, wod)
add_element(fp, meaning)
add_element(fp, prtspch)
add_element(fp, exSentence)
add_element(fp, exTrSentence)
# End of entry
fp.write("---------------------------\n")
# Flushes any unwritten information and
# closes file object; Good practice to
# include
fp.close()
## ----------------------------------------------------------
# Before closing connection, I want to delete the emails
# (The purpose of this script is to de-clutter my inbox)
# But! I want user confirmation -- they may want to read
# the original message to, e.g., listen to the audio clip.
## Note: POP3 does not support marking email as 'read';
## They are simply 'seen' by the python perspective/user
## Note: Users can find all deleted emails in 'Trash'
if (len(newWord) !=0 ):
confirm_new(newWord)
delAction = confirm_yes_no("Are you sure you want to delete their original messages from your Google inbox?\n", "no")
if delAction == False:
print "\nNo emails deleted.\n"
elif delAction == True:
delete_vocab_emails(pop_conn, delList)
else:
print "\nSystem error!\n"
sys.exit(1)
pop_conn.quit()