from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import requests
import smtplib
import time
import ConfigParser
def monitor():
config = ConfigParser.ConfigParser()
config.read('./ds.config')
duoshuo_account = {}
email_info = {}
period_time = {}
items2dict(duoshuo_account, config.items('duoshuo_account'))
items2dict(email_info, config.items('email_info'))
items2dict(period_time, config.items('period_time'))
current_count, meta = get_duoshuo_log(duoshuo_account)
last_count = current_count
name = duoshuo_account.get('name')
period = int(period_time.get('period'))
while True:
try:
current_count, meta = get_duoshuo_log(duoshuo_account)
if (len(meta)) > 0 and (current_count > last_count):
send_email(email_info, name, current_count, (current_count - last_count), meta)
last_count = current_count
time.sleep(period)
except Exception, e:
time.sleep(period)
def items2dict(options_dict, items_list):
for item in items_list:
options_dict[item[0]] = item[1]
def get_duoshuo_log(duoshuo_account):
url = 'http://api.duoshuo.com/log/list.json?' \
+ 'short_name=' + duoshuo_account.get('name') \
+ '&secret=' + duoshuo_account.get('secret') + '&limit=5000'
r = requests.get(url)
resp = r.json()
if (resp['code'] == 0):
length = len(resp['response'])
meta = resp['response'][length - 1]['meta']
action = resp['response'][length - 1]['action']
if (action == 'create'):
return length, meta
else:
return length, {}
return 0, {}
def send_email(email_info, name, current_count, count, meta):
last_meta_message = u'最新评论信息:' \
+ u'\n用户地址:' + unicode(meta.get('ip')) \
+ u'\n用户昵称:' + unicode(meta.get('author_name')) \
+ u'\n用户邮箱:' + unicode(meta.get('author_email')) \
+ u'\n用户网站:' + unicode(meta.get('author_url')) \
+ u'\n评论时间:' + unicode(meta.get('created_at')) \
+ u'\n评论内容:' + unicode(meta.get('message')) \
+ u'\n审核状态:' + unicode(meta.get('status'))
duoshuo_admin_url = 'http://' + name + '.duoshuo.com/admin/'
text = u'后台记录变更数:' + str(count) + u'\n多说后台:' + duoshuo_admin_url + u'\n\n' + last_meta_message;
msg = MIMEText(text, 'plain', 'utf-8')
msg['Subject'] = u'多说评论通知 #' + str(current_count)
msg['From'] = email_info.get('from_address')
msg['To'] = email_info.get('to_address')
server = smtplib.SMTP()
server.connect(email_info.get('email_host'))
server.login(email_info.get('from_address'), email_info.get('password'))
server.sendmail(email_info.get('from_address'), [email_info.get('to_address')], msg.as_string())
server.close()
if __name__ == '__main__':
monitor()