功能需求:
1,实现手动 执行跟停止
2,间隔时间自定义
3,延迟不定时回复
4,回复内容自定义
效果 贴切真实回复,非机器回复那种。
页面展示
#执行顶贴
@csrf_exempt
def runtoppost(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)
tconf=toppostconf.objects.get(toppostlinkuser_id=userinfo.id)
if request.method=="POST":
url=request.POST.get("url")
content=request.POST.get("content")
tophour=request.POST.get("tophour")
topmin=request.POST.get("topmin")
topsec=request.POST.get("topsec")
lxname=request.POST.get("lxname")
lxpsw=request.POST.get("lxpsw")
cookinfo=request.POST.get("cookiearea")
met=request.POST.get("met")
rands=request.POST.get("rands")
rande=request.POST.get("rande")
hourinfo=""
minuteinfo=""
secondinfo=""
cookieinfo=""
if tophour =="0":
hourinfo=0
if tophour !="0":
hourinfo=tophour
if topmin =="0":
minuteinfo=0
if topmin !="0":
minuteinfo=topmin
if topsec =="0":
secondinfo=0
if topsec !="0":
secondinfo=topsec
if url=="":
return HttpResponse('{"topcode":"urlfail"}',content_type="application/json")
elif content=="":
return HttpResponse('{"topcode":"contentfail"}',content_type="application/json")
elif not (re.findall(r"^\d+$",str(tophour))) or int(tophour)<0 or int(tophour)>23:
return HttpResponse('{"topcode":"hourfail"}',content_type="application/json")
elif not (re.findall(r"^\d+$",str(topmin))) or int(topmin)<0 or int(topmin)>59:
return HttpResponse('{"topcode":"minfail"}',content_type="application/json")
elif not (re.findall(r"^\d+$",str(topsec))) or int(topsec)<0 or int(topsec)>59:
return HttpResponse('{"topcode":"secfail"}',content_type="application/json")
elif int(tophour)==int(topmin)==int(topsec)==0:
return HttpResponse('{"topcode":"zerofail"}',content_type="application/json")
elif tconf.toppost_status==0:
return HttpResponse('{"topcode":"noAuthority"}',content_type="application/json")
elif met=="loginmethod" and ((not lxname) or (not lxpsw)):
return HttpResponse('{"topcode":"null_namepsw"}',content_type="application/json")
elif met=="cookiemethod" and (not cookinfo):
return HttpResponse('{"topcode":"nullcookie"}',content_type="application/json")
elif not re.findall(r"【.+?】",str(content)):
return HttpResponse('{"topcode":"refail"}',content_type="application/json")
elif rands=="" or rande=="":
return HttpResponse('{"topcode":"randnull"}',content_type="application/json")
elif (not re.findall(r"^[0-9]\d*$",rands)) or (not re.findall(r"^[0-9]\d*$",rande)):
return HttpResponse('{"topcode":"randint"}',content_type="application/json")
elif rande
return HttpResponse('{"topcode":"randsmall"}',content_type="application/json")
else:
if met=="cookiemethod":
#执行通道2逻辑
cookieinfo=cookinfo
else:
#执行通道1逻辑
cookieinfo=gettoppostcookie(lxname,lxpsw)
if cookieinfo == "loginerr":
return HttpResponse('{"topcode":"lxlogin"}',content_type="application/json")
cookinfo=cookieinfo
topinfo=toppostinfo.objects.filter(toppostinfolinkuser_id=userinfo.id)
if topinfo:
topinfo=toppostinfo.objects.get(toppostinfolinkuser_id=userinfo.id)
topinfo.toppostinfo_url=url
topinfo.toppostinfo_content=content
topinfo.toppostinfo_hour=tophour
topinfo.toppostinfo_minute=topmin
topinfo.toppostinfo_second=topsec
topinfo.toppostinfo_status=1
topinfo.toppostinfo_cookies=cookinfo
topinfo.toppostinfo_randstart=rands
topinfo.toppostinfo_randend=rande
if met=="loginmethod":
topinfo.toppostinfo_lxname=lxname
topinfo.toppostinfo_lxpsw=lxpsw
topinfo.save()
# 执行定时配置
sched.add_job(trigger='interval',func=top_post,args=[url,content,cookinfo,rands,rande],hours=int(hourinfo),minutes=int(minuteinfo),seconds=int(secondinfo),id="tp_"+uname)
return HttpResponse('{"topcode":"success"}',content_type="application/json")
else:
toppostinfo.objects.get_or_create(toppostinfo_url=url,
toppostinfo_content=content,
toppostinfolinkuser_id=userinfo.id,
toppostinfo_hour=tophour,
toppostinfo_minute=topmin,
toppostinfo_second=topsec,
toppostinfo_status=1,
toppostinfo_cookies=cookinfo,
toppost_name="tp_"+uname,
toppostinfo_randstart=rands,
toppostinfo_randend=rande
)
topinfo=toppostinfo.objects.get(toppostinfolinkuser_id=userinfo.id)
if met=="loginmethod":
topinfo.toppostinfo_lxname=lxname
topinfo.toppostinfo_lxpsw=lxpsw
topinfo.save()
sched.add_job(trigger='interval',func=top_post,args=[url,content,cookinfo,rands,rande],hours=int(hourinfo),minutes=int(minuteinfo),seconds=int(secondinfo),id="tp_"+uname)
return HttpResponse('{"topcode":"success"}',content_type="application/json")
elif admin_exist:
return redirect("/boss/")
else:
return redirect("/login/")
#暂停顶贴
@csrf_exempt
def killtoppost(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)
if request.method=="POST":
#状态改为0
topinfo=toppostinfo.objects.get(toppostinfolinkuser_id=userinfo.id)
topinfo.toppostinfo_status=0
topinfo.save(update_fields=["toppostinfo_status"])
#暂停顶贴
sched.remove_job("tp_"+uname)
return HttpResponse('{"topcode":"success"}',content_type="application/json")
elif admin_exist:
return redirect("/boss/")
else:
return redirect("/login/")
对应的ajax:
function runtop(){
var met=$("input[name='tongdao']:checked").attr("id");
var url=document.getElementById("topurl").value;
var content=document.getElementById("toparea").value;
var tophour=document.getElementById("tophour").value;
var topmin=document.getElementById("topmin").value;
var topsec=document.getElementById("topsec").value;
var lxname=document.getElementById("lxname").value;
var lxpsw=document.getElementById("lxpsw").value;
var cookiearea=document.getElementById("cookiearea").value;
var rands=document.getElementById("rands").value;
var rande=document.getElementById("rande").value;
layer.open({
type: 0,
title:false,
skin: 'layui-layer-demo',
area: ['200px', '120px'],
shadeClose: true,
content: '确定要执行吗?',
btn:["确定","取消"],
btn1:function(){
$.ajax({
cache:false,
type:"POST",
url:"/runtoppost/",
data:{"url":url,"content":content,"tophour":tophour,"topmin":topmin,"topsec":topsec,"lxname":lxname,"lxpsw":lxpsw,"cookiearea":cookiearea,"met":met,"rands":rands,"rande":rande},
async:true,
beforeSend:function(xhr,settings){
xhr.setRequestHeader("X-CSRFToken","{{csrf_token}}");
},
success:function(data){
if(data.topcode=="success"){
window.layer.closeAll();
layer.msg("执行成功",{
time:1000
},function(){
window.location.reload();
});
}else if(data.topcode=="urlfail"){
layer.msg("帖子链接不能为空");
}else if(data.topcode=="contentfail"){
layer.msg("回复内容不能为空");
}else if(data.topcode=="hourfail"){
layer.msg("时钟请输入0到23整数");
}else if(data.topcode=="minfail"){
layer.msg("分钟请输入0到59整数");
}else if(data.topcode=="secfail"){
layer.msg("秒钟请输入0到59整数");
}else if(data.topcode=="zerofail"){
layer.msg("时分秒不能同时为0");
}else if(data.topcode=="noAuthority"){
layer.msg("您没有权限");
}else if(data.topcode=="null_namepsw"){
layer.msg("lx账号或密码不能为空");
}else if(data.topcode=="nullcookie"){
layer.msg("cookie不能为空");
}else if(data.topcode=="lxlogin"){
layer.msg("lx登录失败");
}else if(data.topcode=="refail"){
layer.msg("回复内容格式不对");
}else if(data.topcode=="randnull"){
layer.msg("延迟时间不能为空");
}else if(data.topcode=="randint"){
layer.msg("延迟时间只能整数");
}else if(data.topcode=="randsmall"){
layer.msg("延迟开始时间不能大于结束时间");
}else{
layer.msg("操作失败");
}
}
})
},
btn2:function(){
layer.msg("取消成功");
return true;
},
});
};
function killtop(){
layer.open({
type: 0,
title:false,
skin: 'layui-layer-demo',
area: ['200px', '120px'],
shadeClose: true,
content: '确定要暂停吗?',
btn:["确定","取消"],
btn1:function(){
$.ajax({
cache:false,
type:"POST",
url:"/killtoppost/",
data:{},
async:true,
beforeSend:function(xhr,settings){
xhr.setRequestHeader("X-CSRFToken","{{csrf_token}}");
},
success:function(data){
if(data.topcode=="success"){
window.layer.closeAll();
layer.msg("暂停成功",{
time:1000
},function(){
window.location.reload();
});
}else{
layer.msg("暂停失败");
}
}
})
},
btn2:function(){
layer.msg("取消成功");
return true;
},
});
};
定时任务的方法:
# 顶贴任务 遍历出来topcontent 调用下面方法
def top_post(baseurl,topcontent,urlckooie,rands,rande):
tsleep=random.randint(int(rands),int(rande))
time.sleep(tsleep)
allparam=re.findall(r"\?(.+)",baseurl)
bid=re.findall(r"bid=(\d+)",str(allparam))
lxt=re.findall(r"lxt=(.+?)",str(allparam))
r=re.findall(r"_r=(\d+)",str(allparam))
mid=re.findall(r"id=(\d+)",str(allparam))
newurl="http://xxx.net/writerlyappnew.php?gorders=1&gpage=1&npp=1&bid="+str(bid[0])+"&pl=1&lxt="+str(lxt[0])+"&vs=1&dd=0&_r="+str(r[0])
newheader={"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"}
newheader["Cookie"]=urlckooie
newparam="&id="+str(mid[0])+"&95aa=3da3&reference=4949&rand=14377&submit_38=%E5%9B%9E%E5%A4%8D"
contentlist=re.findall(r"【(.+?)】",topcontent)
#随机取回复列表的值
con=random.choice(contentlist)
cont=parse.quote(con)
aram="content="+cont+newparam
requests.post(newurl,data=aram,headers=newheader)
分析:
主要定时器apscheduler的应用来实现接口的自动请求
执行-->定时器方法.add_job()
停止-->定时器方法.remove_job()
定时器每次都调用一次接口请求(即回帖)
接口请求时加上时间范围的随机取值 达到延迟的效果,回复内容多个且随机取值回复,这样就不会每次回复时间等长且回复内容一模一样了。