Skip to content

Commit dc6b658

Browse files
committed
Merge pull request #12 from xiyoulaoyuanjia/RFHW-5
Add emaill notification
2 parents 5cb2d6f + 085a3fd commit dc6b658

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

main.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4+
35
'''
46
#=============================================================================
57
# FileName: main.py
@@ -17,6 +19,7 @@
1719
from apscheduler.scheduler import Scheduler
1820
from email.mime.text import MIMEText
1921
from conf import *
22+
from optparse import OptionParser
2023

2124
import smtplib
2225
import sys
@@ -60,6 +63,14 @@ def __init__(self):
6063
},
6164
'href' : "^/nForum/article/Career_Campus/\d+$",
6265
},
66+
{
67+
'host' : 'http://bbs.byr.cn',
68+
'url' : 'http://bbs.byr.cn/#!board/Job',
69+
'headers' : {
70+
"X-Requested-With" : "XMLHttpRequest",
71+
},
72+
'href' : "^/article/Job/\d+$",
73+
},
6374
)
6475

6576
def _parse_html_to_urls(self, host, url, headers, href):
@@ -162,7 +173,7 @@ def generate_page(self):
162173
</html>
163174
''' % self._get_web_urls_from_redis()
164175

165-
def send_massage(self):
176+
def send_massage(self, *args, **kwargs):
166177
msg_num, content = self._get_message_urls_from_redis()
167178
if msg_num <= 0 :
168179
print "none messages to send..."
@@ -174,12 +185,21 @@ def send_massage(self):
174185
msg["Accept-Charset"]="ISO-8859-1, utf-8"
175186
msg['Subject'] = sub
176187
msg['From'] = send_mail_address
177-
msg['to'] = to_adress = "139SMSserver<" + RECEIVE_MAIL_USER + "@" + RECEIVE_MAIL_POSTFIX + ">"
178188
try:
179189
stp = smtplib.SMTP()
180190
stp.connect(SEND_MAIL_HOST)
191+
# NOTE(xiyoulaoyuanjia): it get error do not have
192+
# it smtplib.SMTPException: SMTP AUTH extension not supported by server
193+
stp.starttls()
181194
stp.login(SEND_MAIL_USER, SEND_MAIL_PASSWORD)
182-
stp.sendmail(send_mail_address, to_adress, msg.as_string())
195+
# FIX(xiyoulaoyuanjia): here if sms get error. do not
196+
# send email notification
197+
if kwargs['sms']:
198+
msg['to'] = to_adress = "139SMSserver<" + RECEIVE_MAIL_USER_139 + "@" + RECEIVE_MAIL_POSTFIX_139 + ">"
199+
stp.sendmail(send_mail_address, to_adress, msg.as_string())
200+
if kwargs['email']:
201+
msg['to'] = to_adress = RECEIVE_MAIL_USER + "@" + RECEIVE_MAIL_POSTFIX
202+
stp.sendmail(send_mail_address, to_adress, msg.as_string())
183203
print "send message sucessfully..."
184204
self._refresh_message_urls_in_redis()
185205
except Exception, e:
@@ -199,13 +219,19 @@ def run(self):
199219

200220
if __name__ == '__main__':
201221

222+
parser = OptionParser(description='a crawer which get jobs info.')
223+
parser.add_option('-s', '--sms', dest='sms', action='store_true',
224+
help='send sms mode')
225+
parser.add_option('-e', '--email', dest='email', action='store_true',
226+
help='send email mode')
227+
(options, args) = parser.parse_args(args=sys.argv[1:])
202228
crawler = Crawler()
203229
crawler.run()
204230

205231
sched = Scheduler()
206232
sched.start()
207233
sched.add_interval_job(crawler.run, hours=CRAWLER_FREQUENCE_HOURS)
208-
sched.add_interval_job(crawler.send_massage, minutes=MESSAGE_FREQUENCE_MINUTES)
234+
sched.add_interval_job(crawler.send_massage, minutes=MESSAGE_FREQUENCE_MINUTES, kwargs=options.__dict__)
209235

210236

211237
try:

0 commit comments

Comments
 (0)