-
Notifications
You must be signed in to change notification settings - Fork 13
/
toWechat.py
68 lines (56 loc) · 2.05 KB
/
toWechat.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
# encoding=utf8
#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import time
#update your userids
userIds='orPQ800sg4nMZkuMKxH01LiFpNxsyvWM149999'
def getnewesttitle():
url = 'https://www.hostloc.com/forum.php?mod=forumdisplay&fid=45&filter=author&orderby=dateline'
headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'}
requests.adapters.DEFAULT_RETRIES = 5
s = requests.session()
s.keep_alive = False
r = s.get(url, headers=headers)
soup = BeautifulSoup(r.text,'html.parser')
newest = soup.find('span',class_='by')
pid = r.text[r.text.find('tid')+4:r.text.find('tid')+10]
post_url = "https://www.hostloc.com/thread-{}-1-1.html".format(pid)
#print post_url
print ('monitor is runing ,please wait for a monent')
resultArr = [newest.parent.text,post_url]
return resultArr
def sendmessage(newesttitle,postUrl):
finalUrl = 'http://wxmsg.dingliqc.com/send?msg='+ newesttitle +'&userIds=' + userIds + '&url=' + postUrl
requests.adapters.DEFAULT_RETRIES = 5
s = requests.session()
s.keep_alive = False
s.get(finalUrl)
print('send a new message to wechat')
firstArr = getnewesttitle()
newesttitle = firstArr[0]
sendmessage(firstArr[0],firstArr[1])
while True:
try:
time.sleep(30)
try:
newArr = getnewesttitle()
finally:
time.sleep(5)
pass
thenexttitle = newArr[0]
postUrl = newArr[1]
print('monitoring...')
print 'old message is ' + newesttitle.encode('utf-8')
print 'new message is ' + thenexttitle.encode('utf-8')
print postUrl.encode('utf-8')
if thenexttitle != newesttitle:
newesttitle = thenexttitle
print('find new message ,reading....')
sendmessage(thenexttitle,postUrl)
pass
else:
pass
except RuntimeError:
print(RuntimeError)
pass