-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmth_notifier.py
More file actions
executable file
·176 lines (159 loc) · 5.45 KB
/
smth_notifier.py
File metadata and controls
executable file
·176 lines (159 loc) · 5.45 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
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
#-------------------------------------------------------------------------------
# Name: smth_notifier
# Purpose: continuously monitor newsmth borad and notify user by email when desired post published.
#
# Author: Hao Wei
#
# Created: 13-01-2014
# Copyright: (c) whao 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/python
# -*- coding: utf-8 -*-
from multiprocessing import Process, Queue
from time import sleep
import urllib2
import sys
import re
import signal
import httplib
import time
import smtplib
import os
from email.mime import text
smth_prot = "http"
smth_host = "www.newsmth.net"
smth_ip = "42.62.43.22"
latest_page_uri = "bbsdoc.php?board=ITjob&ftype=6"
def log(cont):
#f = open("smth.log","a")
#f.write(cont)
#f.write("\n")
#f.close()
print cont.encode("utf-8")
def parse_post(post_list):
notifiers = {}
#if True:
try:
fo = urllib2.urlopen(urllib2.Request("%s://%s/%s" % (smth_prot,smth_ip,latest_page_uri), None, {"HOST": smth_host}, smth_host))
rd = fo.read()
rd = rd.decode('gbk')
fo.close()
lst = rd.split('\n')
regstr = r"""c\.o\((\d+),\d+,'([\d\w]+)','([a-z\@\s]+)',\d+,'(.+)',\d+,\d+,\d+\)"""
regstr_c = re.compile(regstr,re.M)
cnt = 0
for tr in lst:
regstr_m = regstr_c.search(tr)
if regstr_m:
cnt += 1
post_id = int(regstr_m.group(1))
post_author = regstr_m.group(2)
post_flag = regstr_m.group(3)
post_title = regstr_m.group(4)
#print("[%10d][%15s]%s" % (int(post_id),post_author,post_title))
if not post_list.has_key(post_id) or post_list[post_id].title != post_title:
if not u"实习" in post_title and not u"招聘" in post_title:
post = Post(post_id,post_author,post_flag,post_title)
notifiers[post_id] = post
except:
print "Meet error"
return None
for k in notifiers.keys():
post_list[k] = notifiers[k]
return notifiers
#print "process %d finished!" % id
def build_message(notifiers):
subject = u"水木兼职版新主题"
toaddrs = ["haow05@126.com"]
content = u""
for k in notifiers:
p = notifiers[k]
content += u"""<p><a href="%s">%s:%s</a></p>\n""" % (p.get_url(),p.author,p.title)
return (toaddrs,subject,content)
class Post:
board = "ITjob"
author= None
id = None
title = None
flag = None
content = None
def get_url(self):
return "%s://%s/nForum/article/%s/%d" % (smth_prot,smth_host,self.board,self.id)
def __init__(self,id,author,flag,title):
self.id = id
self.title = title
self.flag = flag
self.author = author
# Specifying the from and to addresses
def send_mail(toaddrs = ['haow05@126.com'],subject = "Mail test",content="test mail"):
from_txt = 'NewSmth Notifier <notifier.apptest@gmail.com>'
from_addr = 'notifier.apptest@gmail.com'
# Writing the message (this message will appear in the email)
msg = text.MIMEText(content,'html','utf-8')
msg['From'] = from_txt
msg['To'] = ";".join(toaddrs)
msg['Subject']= subject
# Gmail Login
#Fill in your username of gmail
mail_username = 'user'
#then password
mail_password = '123456'
# Sending the mail
try:
#server = smtplib.SMTP('smtp.gmail.com', 25)
server = smtplib.SMTP('173.194.72.108', 25)
server.ehlo()
server.starttls()
server.ehlo()
except:
log( 'Cannot Connect')
# login with username & password
# print 'loginning ...'
try:
server.login(mail_username,mail_password)
except:
log( 'Cannot Login')
try:
server.sendmail(from_addr, toaddrs, msg.as_string() )
server.quit()
except:
log( 'Cannot Send Mail')
if __name__ == '__main__':
post_list = {}
try:
if os.fork() > 0:
os._exit(0) # exit father…
except OSError, error:
print 'fork #1 failed: %d (%s) '% (error.errno, error.strerror)
os._exit(1)
#signal.signal(signal.SIGCHLD, signal.SIG_IGN)
os.chdir("/")
os.setsid()
os.umask(0)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
si = file("/dev/null", 'r')
so = file("/tmp/tmp_1234.log", 'a+', 0)
#se = file("/tmp/tmp_1234.log", 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(so.fileno(), sys.stderr.fileno())
cnt =0
while 1:
#if True:
try:
notifiers = parse_post(post_list)
if(notifiers):
(toaddrs ,subject, content ) = build_message(notifiers)
log( time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) )
log( content)
send_mail(toaddrs ,subject, content )
#send_notifer(notifiers)
except:
log( "Get a error when get notifiers ")
sleep(5)
continue
log( time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
log( "Loop %d end, begin to sleep" % cnt)
cnt +=1
sleep(10)