-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback_notifier.py
225 lines (180 loc) · 8.65 KB
/
feedback_notifier.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/ur/bin/env python3
# -----------------------------------------------------------------------------
# eBay Feedback Notifier
# -----------------------------------------------------------------------------
# This script sends a notification email if an eBay user's feedback rating
# changes.
#
# Requirements:
# - Create a free SMTP2GO account at smtp2go.com.
# - Modify the values in the "REQUIRED CUSTOMIZATIONS" section below.
#
# Procedure:
# - Run the script using Python version 3.0 or higher.
#
# Usage: feedback_notifier.py [-h] -u USERNAME [-f FILENAME]
#
# -----------------------------------------------------------------------------
import argparse
import os.path
import re
import smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from urllib.request import urlopen
# -----------------------------------------------------------------------------
# REQUIRED CUSTOMIZATIONS
# -----------------------------------------------------------------------------
EMAIL_RECIPIENT = '[email protected]'
EMAIL_SENDER = '[email protected]'
SMTP2GO_USERNAME = '[email protected]'
SMTP2GO_PASSWORD = 'password'
# -----------------------------------------------------------------------------
# Optional customizations
# -----------------------------------------------------------------------------
DEFAULT_SCORES_FILE = 'scores.txt'
SMTP2GO_SERVER = 'mail.smtp2go.com'
SMTP2GO_PORT = 2525 # 8025, 587 and 25 can also be used
EMAIL_SUBJECT = 'eBay feedback notification'
EBAY_USR_URL = 'https://www.ebay.com/usr/'
def get_cmdline_args():
"""Return arguments from the command-line."""
parser = argparse.ArgumentParser(description='An eBay feedback notifier.')
parser.add_argument('-u', action='store', required=True,
help='an eBay username', dest='username')
parser.add_argument('-f', action='store', required=False,
help='a text file to write feedback scores ' + \
'(default: scores.txt)', dest='filename',
default=DEFAULT_SCORES_FILE)
return parser.parse_args().username, parser.parse_args().filename
def send_smtp2go_email(sender, recipient, subject, body):
"""Send an email through an SMTP server."""
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
text_message = MIMEText(body, 'plain')
msg.attach(text_message)
mailServer = smtplib.SMTP(SMTP2GO_SERVER, SMTP2GO_PORT)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(SMTP2GO_USERNAME, SMTP2GO_PASSWORD)
mailServer.sendmail(sender, recipient, msg.as_string())
mailServer.close()
class Notifier:
"""Class that send an email if a user's feedback changes."""
def __init__(self, username, scores_file):
"""Instantiate a Notifier object."""
self.username = username
self.profile_url = EBAY_USR_URL + self.username
self.scores_file = scores_file
self.old_scores = {'overall': 0, 'positive': 0,
'neutral': 0, 'negative': 0}
self.new_scores = {'overall': 0, 'positive': 0,
'neutral': 0, 'negative': 0}
self.errors = []
self.print_header()
self.get_old_scores()
self.get_new_scores()
self.compare_scores()
def compare_scores(self):
"""Check for score changes and send an email if there are any."""
# Scores file exists
if self.old_scores_exist:
# Check for score changes
if self.new_scores['overall'] != self.old_scores['overall'] or \
self.new_scores['positive'] != self.old_scores['positive'] or \
self.new_scores['neutral'] != self.old_scores['neutral'] or \
self.new_scores['negative'] != self.old_scores['negative']:
print('Feedback changed. Sending email notification ... ',
end=""),
# Build the email body
email_body = self.username + '\'s feedback changes:\n\n'
for key in self.new_scores:
if self.new_scores[key] != self.old_scores[key]:
email_body += key + ': ' + str(self.old_scores[key]) \
+ ' -> ' + str(self.new_scores[key]) + \
'\n'
# Email using SMTP2GO
send_smtp2go_email(EMAIL_SENDER, EMAIL_RECIPIENT, EMAIL_SUBJECT,
email_body)
print('done.')
else:
print('Feedback has not changed.')
def get_new_scores(self):
"""Read new feedback scores from a webpage."""
# Download user profile webpage
print('\nDownloading new values from: %s\n' %(self.profile_url))
html_content = urlopen(self.profile_url).read().decode('utf-8')
# Get the new overall score
pattern = self.username + "\'s feedback score is (\d+)"
match = re.search(pattern, html_content, re.IGNORECASE);
if match == None:
self.errors.append('overall score not found')
self.new_scores['overall'] = '0'
else:
# Insert comma separators
self.new_scores['overall'] = format(int(match.group(1)), ',d')
print('new overall score : ' + self.new_scores['overall'])
# Get the new positive, neutral, and negative scores
for key in self.new_scores:
if key != 'overall':
# Get the new scores
pattern = r"title=\"" + key + r"\"><div class=\"score\">" + \
"<span class=\"gspr icf[npt]\"></span><span " + \
"class=\"num\">([\d\,]+)</span>"
match = re.search(pattern, html_content, re.IGNORECASE);
if match == None:
self.errors.append(key + ' score not found')
self.new_scores[key] = '0'
else:
self.new_scores[key] = match.group(1)
print('new ' + key + ' score: ' + self.new_scores[key])
# Handle errors
if len(self.errors) == 0:
print('\nNo errors\n')
else:
print('\nErrors: ' + str(self.errors) + '\n')
# Write new values to file
with open(self.scores_file, 'w') as f:
s = self.time_stamp + '\n\n' + \
'overall: ' + str(self.new_scores['overall']) + '\n' + \
'positive: ' + str(self.new_scores['positive']) + '\n' + \
'neutral: ' + str(self.new_scores['neutral']) + '\n' + \
'negative: ' + str(self.new_scores['negative'])
f.write(s)
f.closed
def get_old_scores(self):
"""Read old feedback scores from a file."""
# Check for an existing scores file
self.old_scores_exist = os.path.isfile(self.scores_file)
# Scores file exists
if self.old_scores_exist:
# Get the old scores
with open(self.scores_file, 'r') as f:
print('Loading old values from: %s\n' %(self.scores_file))
f.readline() # discard date
f.readline() # discard newline
for key in self.old_scores:
pattern = key + ':\s+([\d\,]+)'
match = re.search(pattern, f.readline())
if match == None:
self.errors.append('old ' + key + 'score not found')
else:
self.old_scores[key] = match.group(1)
print('old ' + key + ' score : ' + \
self.old_scores[key])
f.closed
def print_header(self):
"""Print the timestamp and username."""
self.time_stamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
print('\n' + self.time_stamp + '\n')
print('eBay username: ' + self.username + '\n')
# -----------------------------------------------------------------------------
# __main__
# -----------------------------------------------------------------------------
if __name__ == "__main__":
ebay_id, scores_file = get_cmdline_args()
notifier = Notifier(ebay_id, scores_file)