#群发消息
@csrf_exempt
def sendmsg(request):
uname=request.session.get("username","")
user_exist=bkuser.objects.filter(bkuser_name=uname)
admin_exist=bkadmin.objects.filter(bkadmin_name=uname)
if user_exist:
userinfo=bkuser.objects.get(bkuser_name=uname)
sendinfo=usend.objects.get(sendlinkuser_id=userinfo.id)
if request.method=="POST":
met=request.POST.get("met")
lxname=request.POST.get("lxname")
lxpsw=request.POST.get("lxpsw")
cookinfo=request.POST.get("cookiearea")
ltid=request.POST.get("ltid")
key=request.POST.get("key")
sendvalue=request.POST.get("sendvalue")
cookieinfo=""
if met=="loginmethod" and ((not lxname) or (not lxpsw)):
return HttpResponse('{"scode":"null_namepsw"}',content_type="application/json")
elif met=="cookiemethod" and (not cookinfo):
return HttpResponse('{"scode":"null_cookie"}',content_type="application/json")
elif not ltid:
return HttpResponse('{"scode":"null_ltid"}',content_type="application/json")
elif not sendvalue:
return HttpResponse('{"scode":"null_value"}',content_type="application/json")
elif sendinfo.sendnum==0:
return HttpResponse('{"scode":"zero_num"}',content_type="application/json")
else:
if met=="cookiemethod":
#执行通道2逻辑
cookieinfo=cookinfo
else:
#执行通道1逻辑
cookieinfo=gettoppostcookie(lxname,lxpsw)
if cookieinfo == "loginerr":
return HttpResponse('{"scode":"lxlogin"}',content_type="application/json")
#获取cookie
cookinfo=cookieinfo
#存入数据库
sendinfo.sendstatus=1
sendinfo.lxcookie=cookinfo
sendinfo.sendcontent=sendvalue
sendinfo.sendscreen=key
if met=="loginmethod":
sendinfo.lxname=lxname
sendinfo.lxpsw=lxpsw
sendinfo.sendlog="media/sendlog/"+uname+"/"+uname+".txt"
sendinfo.sendltid=ltid
sendinfo.save()
#后台运行群发任务
sendplan=threading.Thread(target=sendthread, args=(sendinfo.id,ltid,uname))
sendplan.start()
return HttpResponse('{"scode":"success"}',content_type="application/json")
elif admin_exist:
return redirect("/boss/")
else:
return redirect("/login/")
#下载发送日志
def downsendlog(request,send_id):
uname=request.session.get("username","")
user_exist=bkuser.objects.filter(bkuser_name=uname)
admin_exist=bkadmin.objects.filter(bkadmin_name=uname)
if user_exist:
sendinfo=usend.objects.get(id=send_id)
the_file_name=sendinfo.sendlog
with open(the_file_name, 'rb') as model_excel:
result = model_excel.read()
response = HttpResponse(result)
response['Content-Disposition'] = 'attachment;filename="'+str(uname)+'.txt"'
return response
elif admin_exist:
return redirect("/boss/")
else:
return redirect("/login/")
定时器任务源码
#群发线程
def sendthread(sendid,ltid,uname):
#创建文件夹
filedir=settings.MEDIA_ROOT+"/sendlog/"+uname
if not os.path.exists(filedir):
os.makedirs(filedir)
txtfile=filedir+"/"+uname+".txt"
f=open(txtfile,'w')
sendinfo=usend.objects.get(id=sendid)
#剩余次数
countnum=int(sendinfo.sendnum)
#获取lxt
lxt=re.findall(r"lxt=(.+?);",str(sendinfo.lxcookie))
#关键字装成字典
keyvalue=re.findall(r"【(.+?)】",str(sendinfo.sendscreen))
keynum=[]
for le in range(len(keyvalue)):
keynum.append(le)
keydict=dict(zip(keynum,keyvalue))
#获取论坛总人数
lt="http://xxx.com/list.php?bid="+ltid
ltres=urllib.request.urlopen(lt)
ltres=ltres.read().decode()
onlinenum=re.findall(r"(\d+)人在线",str(ltres))
if onlinenum:
if int(onlinenum[0])>0:
allpage=int(onlinenum[0])//15+1 #每隔15条数据分页
batchnum=int(onlinenum[0])//450+1 #每隔900条数据遍历一次
for bat in range(batchnum):
if bat==batchnum-1:
startnum=30*bat+1
endnum=allpage
numlist=[]
for n in range(startnum,endnum):
#获取论坛用户列表
onlineurl="http://xxx.com/cshow.php?showtypes=1&bid="+ltid+"&p="+str(n)
onlineres=urllib.request.urlopen(onlineurl)
onlineres=onlineres.read().decode()
userall=re.findall(r">(.+?)<\/a>",str(onlineres))
for m in range(len(userall)):
userone=userall[m]
inexist=0
for x in keydict.keys():
if (keydict[x] not in userone) and ("(" in userone) and (")" in userone):
inexist+=0
else:
inexist+=1
if inexist==0:
numid=re.findall(r"\((\d+)\)",str(userone))
numlist.append(numid[0])
#遍历出每个id
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
endwrite=str(nowtime)+" 剩余次数"+str(countnum)
f.write(endwrite)
f.write("\n")
f.write("开始执行群发...")
f.write("\n")
for num in range(len(numlist)):
#随机等待
time.sleep(10)
#剩余次数不等于0 才会执行添加好友跟群发消息
if countnum != 0:
#添加好友
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
addurl="http://clubc.xxx.com/friendnew/iAddfriend.php?cd=0&lxt="+str(lxt[0])+"&_r="+str(r)+"&vs=1&dd=0"
addparam="friendid="+str(numlist[num])+"&totypeid=0&type=1&backurl=http%253a%252f%252fclubc.xxx.com%252ffriendnew%252fAddFriend.php%253ffriendid%253d"+str(numlist[num])+"%2526backurl%253d&fromurl=&flag=2&pagetag=0&nomsg=0&submit_7=+%E6%8F%90%E4%BA%A4+"
addheader={"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"}
addheader["Cookie"]=sendinfo.lxcookie
requests.post(addurl,data=addparam,headers=addheader)
#获取mu的值
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
muurl="http://xxx.com/msglistbysender.php?sdid="+str(numlist[num])+"&lxt="+str(lxt[0])+"&vs=1&_r="+str(r)
muparam={"vs":"1","_r":"532569890"}
muparam["sdid"]=str(numlist[num])
muparam["lxt"]=str(lxt[0])
muparam["_r"]=str(r)
mures=requests.get(muurl,data=muparam,headers=addheader)
mu=re.findall(r"mu=(.+?)\&",str(mures.text))
#执行群发消息操作
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
sendurl="http://xxx.com/send.php?mu="+str(mu[0])+"&uid="+str(numlist[num])+"&lxt="+str(lxt[0])+"&vs=1&_r="+str(r)+"&dd=0"
content=parse.quote(sendinfo.sendcontent)
param="content="+content+"&rlyface=0&receiverid="+str(numlist[num])+"&returnUrl=msglistbysender.php%253fsdid%253d"+str(numlist[num])+"%2526vs%253d1%2526_r%253d"+str(r)+"&havemore=0&content2=&msgtype=1&classid=1&autoback=1&submit_74=+%E5%9B%9E%E5%A4%8D+"
sendres=requests.post(sendurl,data=param,headers=addheader)
if "发送成功" in sendres.text:
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
writesuccess=str(nowtime)+" 用户ID"+str(numlist[num])+"发送消息成功"
f.write(writesuccess)
f.write("\n")
countnum=countnum-1
else:
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
writefail=str(nowtime)+" 用户ID"+str(numlist[num])+"发送消息失败,已发送加友申请"
f.write(writefail)
f.write("\n")
#判断剩余次数是否为0
if countnum==0:
break
else:
startnum=30*bat+1
endnum=30*bat+30+1
numlist=[]
for n in range(startnum,endnum):
#获取论坛用户列表
onlineurl="http://xxx.com/cshow.php?showtypes=1&bid="+ltid+"&p="+str(n)
onlineres=urllib.request.urlopen(onlineurl)
onlineres=onlineres.read().decode()
userall=re.findall(r">(.+?)<\/a>",str(onlineres))
for m in range(len(userall)):
userone=userall[m]
inexist=0
for x in keydict.keys():
if (keydict[x] not in userone) and ("(" in userone) and (")" in userone):
inexist+=0
else:
inexist+=1
if inexist==0:
numid=re.findall(r"\((\d+)\)",str(userone))
numlist.append(numid[0])
#遍历出每个id
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
endwrite=str(nowtime)+" 剩余次数"+str(countnum)
f.write(endwrite)
f.write("\n")
f.write("开始执行群发...")
f.write("\n")
for num in range(len(numlist)):
#随机等待
time.sleep(10)
#剩余次数不等于0 才会执行添加好友跟群发消息
if countnum != 0:
#添加好友
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
addurl="http://xxx.com/friendnew/iAddfriend.php?cd=0&lxt="+str(lxt[0])+"&_r="+str(r)+"&vs=1&dd=0"
addparam="friendid="+str(numlist[num])+"&totypeid=0&type=1&backurl=http%253a%252f%252fclubc.xxx.com%252ffriendnew%252fAddFriend.php%253ffriendid%253d"+str(numlist[num])+"%2526backurl%253d&fromurl=&flag=2&pagetag=0&nomsg=0&submit_7=+%E6%8F%90%E4%BA%A4+"
addheader={"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"}
addheader["Cookie"]=sendinfo.lxcookie
requests.post(addurl,data=addparam,headers=addheader)
#获取mu的值
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
muurl="http://xxx.com/msglistbysender.php?sdid="+str(numlist[num])+"&lxt="+str(lxt[0])+"&vs=1&_r="+str(r)
muparam={"vs":"1","_r":"532569890"}
muparam["sdid"]=str(numlist[num])
muparam["lxt"]=str(lxt[0])
muparam["_r"]=str(r)
mures=requests.get(muurl,data=muparam,headers=addheader)
mu=re.findall(r"mu=(.+?)\&",str(mures.text))
#执行群发消息操作
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
nowtime=datetime.datetime.strptime(nowtime,"%Y-%m-%d %H:%M:%S")
nowtime=nowtime.timetuple()
r=int(time.mktime(nowtime))
sendurl="http://xxx.com/send.php?mu="+str(mu[0])+"&uid="+str(numlist[num])+"&lxt="+str(lxt[0])+"&vs=1&_r="+str(r)+"&dd=0"
content=parse.quote(sendinfo.sendcontent)
param="content="+content+"&rlyface=0&receiverid="+str(numlist[num])+"&returnUrl=msglistbysender.php%253fsdid%253d"+str(numlist[num])+"%2526vs%253d1%2526_r%253d"+str(r)+"&havemore=0&content2=&msgtype=1&classid=1&autoback=1&submit_74=+%E5%9B%9E%E5%A4%8D+"
sendres=requests.post(sendurl,data=param,headers=addheader)
if "发送成功" in sendres.text:
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
writesuccess=str(nowtime)+" 用户ID"+str(numlist[num])+"发送消息成功"
f.write(writesuccess)
f.write("\n")
countnum=countnum-1
else:
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
writefail=str(nowtime)+" 用户ID"+str(numlist[num])+"发送消息失败,已发送加友申请"
f.write(writefail)
f.write("\n")
#判断剩余次数是否为0
if countnum==0:
break
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
endwrite=str(nowtime)+"群发已结束"
f.write(endwrite)
sendinfo.sendnum=countnum
sendinfo.sendstatus=0
sendinfo.save()
else:
nowtime=time.strftime("%Y-%m-%d %H:%M:%S")
endwrite=str(nowtime)+"群发已结束"
f.write(endwrite)
sendinfo.sendnum=countnum
sendinfo.sendstatus=0
sendinfo.save()
大体逻辑爬虫在线人数用户ID 然后遍历群发接口发送消息