-
Notifications
You must be signed in to change notification settings - Fork 10
/
robot.py
240 lines (200 loc) · 8.77 KB
/
robot.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
import time
import random
import shelve
#import os
import platform
import sys
from tools import login
from tools import greet
from tools import monitor
from tools import loadpic
def storeLog(log):
f = open('log.txt', 'a')
f.write(log)
f.close()
#防止重复评论
def loadLastId(isPost = True):
database = shelve.open("id.dat") #第一次运行时会自动创建id.dat
if(isPost):
try:
return database['lastidPost']
except:
return 3623934551198704
else:
try:
return database['lastidReply']
except:
return 3624611847141810
database.close()
#存储上次回复的id
def storeLastId(lastid, isPost = True):
database = shelve.open("id.dat")
if(isPost):
database['lastidPost'] = lastid
else:
database['lastidReply'] = lastid
database.close()
def run():
client = login.login()
lastid = loadLastId()
inter = 190 #检查间隔
if client==None:
raise TypeError()
#启动时的测试微博
myPic = loadpic.pic()
client.statuses.update.post(status=greet.hello(101))
client.statuses.upload.post(status=greet.hello(100), pic = myPic)
log = "服务器再次启动! "
print log.decode('utf-8')
while True:
try:
inter += 1 #加1秒
now = time.strftime('%M%S',time.localtime(time.time()))
hour = time.strftime('%H',time.localtime(time.time()))
#每天凌晨1点重新授权
if hour == '01' and now == '0000':
break
#由于weibo API限制,不能发布自己可见微博,所以我设置了一个密友可见,密友即为我自己的另外一个账号,用来监控cpu温度和运行时间
if hour == '22' and now == '0000':
print "sending a monitor weibo"
monitor_info = ''
if platform.system()=='Linux':
monitor_info += monitor.monitor_cpu_temp()
monitor_info += monitor.monitor_runtime()
monitor_info += monitor.monitor_http()
try:
client.statuses.update.post(status=monitor_info, visible = 2)
except:
pass
log = "Send a monitor weibo succesfully! %s时%s分%s秒: \n" %(hour,now[0:2],now[2:])
print log.decode('utf-8')
storeLog(log)
#发微
if now == '4500' and hour in ['07', '12', '11', '17','18','20','22','23']:
print 'sending a normal weibo'
monitor_info = ""
if random.choice(range(2)):
if platform.system()=='Linux':
monitor_info += monitor.monitor_cpu_temp()
else:
monitor_info += monitor.monitor_http()
elif random.choice(range(2)):
monitor_info += monitor.monitor_http()
else:
monitor_info += monitor.monitor_runtime()
greeting = ""
greeting += greet.hello(hour)
myPic = loadpic.pic()
if random.choice(range(2)):
content = '%s' %(greeting)
else:
content = '%s%s' %(greeting, monitor_info)
try:
#随机选择发图或者不发图,1/2可能性发图
if random.choice(range(2)):
client.statuses.update.post(status=content)
else:
client.statuses.upload.post(status=content, pic = myPic)
except Exception, e:
print e
myPic.close()
log = "Send a normal weibo succesfully! %s时%s分%s秒 \n" %(hour,now[0:2],now[2:])
print log.decode('utf-8')
storeLog(log)
time.sleep(1)
'''
if inter > 180:
try:
except Exception,e:
print e
'''
#对最新 @我 的进行回复,200s一次,包括评论原创微博,回复评论
if inter > 200:
try:
#对我微博进行评论的进行回复#
lastCommentid = loadLastId(False)
mentions = client.comments.to_me.get(since_id = lastCommentid,count = 10)
for weiboInfo in mentions['comments']:
lastCommentid = weiboInfo['id'] #评论的id
lastPostId = weiboInfo['status']['id'] #微博的id
print ("评论的id:%d 微博的id:%d" % (lastCommentid,lastPostId)).decode('utf-8')
storeLastId(lastCommentid, False)
myComment = greet.comment()
client.comments.reply.post(id = lastPostId, cid = lastCommentid, comment = myComment)
log = 'replay a comment successfully! %s时%s分%s秒 id:%d comment:%s \n' %(hour, now[0:2], now[2:], lastid, myComment)
print log.decode('utf-8')
storeLog(log)
#对最新 @我 的原创微博进行评论#
#获取原创的最新微博的id
lastid = loadLastId()
mentions = client.statuses.mentions.get(since_id = lastid, filter_by_type = 1)
for weiboInfo in mentions['statuses']:
lastid = weiboInfo['id']
print lastid
storeLastId(lastid)
myComment = ""
myComment = greet.comment()
client.comments.create.post(id = lastid, comment = myComment)
log = 'send a comment successfully! %s时%s分%s秒:id:%d comment:%s \n' %(hour, now[0:2], now[2:], lastid, myComment)
print log.decode('utf-8')
storeLog(log)
#对最新 @我 的评论进行回复#
lastCommentid = loadLastId(False)
mentions = client.comments.mentions.get(since_id = lastCommentid)
for weiboInfo in mentions['comments']:
lastCommentid = weiboInfo['id'] #评论的id
lastPostId = weiboInfo['status']['id'] #微博的id
print ("评论的id:%d 微博的id:%d" % (lastCommentid,lastPostId)).decode('utf-8')
storeLastId(lastCommentid, False)
myComment = greet.comment()
client.comments.reply.post(id = lastPostId, cid = lastCommentid, comment = myComment)
log = 'replay a comment successfully! %s时%s分%s秒 id:%d comment:%s \n' %(hour, now[0:2],now[2:], lastid, myComment)
print log.decode('utf-8')
storeLog(log)
except Exception, e:
print e
inter = 0
time.sleep(1)#while循环每次间隔一秒
except Exception, e:
print e
def AddSomeConfig():
WorkPath = sys.path[0].replace('\\','/')
#print WorkPath
filepath = WorkPath+'/WorkPath.json'
fin = open(filepath,'r')
ConfigInfo = fin.read()
#print ConfigInfo
js = eval(ConfigInfo)
#print js
sytime = (time.localtime())
js.setdefault('WorkPath',WorkPath)
js.setdefault('Year',sytime.tm_year)
js.setdefault('Month',sytime.tm_mon)
js.setdefault('Day',sytime.tm_mday)
js.setdefault('Hour',sytime.tm_hour)
js.setdefault('Min',sytime.tm_min)
js.setdefault('Sec',sytime.tm_sec)
js['WorkPath']=WorkPath
js['Year']=sytime.tm_year
js['Month']=sytime.tm_mon
js['Day']=sytime.tm_mday
js['Hour']=sytime.tm_hour
js['Min']=sytime.tm_min
js['Sec']=sytime.tm_sec
ConfigInfo = str(js)
#print ConfigInfo
fin.close()
fout = open(filepath,'w')
fout.write(ConfigInfo)
fout.close()
return True
if __name__=="__main__":
while True:
try:
AddSomeConfig()
#print greet.hello('101')
#break
run()
except TypeError:
break