Discuz X 发帖提交器一点实现

来源:互联网 发布:淘宝魔豆妈妈申请条件 编辑:程序博客网 时间:2024/04/28 23:46
#!/usr/bin/python#coding=utf-8#post by EvilBinary 小E 实现Discuz X 的post自动提交。#Filename: post.pyimport urllib,urllib2,cookielib,re,osimport fnmatch,sys,time,randomurl ='http://bbs.xxxxxx.net/'cookie=cookielib.CookieJar()def login(username,password):action='member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=LIQ5i'logindata=(('username',username), ('password',password))opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))agents = ["Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)","Internet Explorer 7 (Windows Vista); Mozilla/4.0 ","Google Chrome 0.2.149.29 (Windows XP)","Opera 9.25 (Windows Vista)","Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)","Opera/8.00 (Windows NT 5.1; U; en)"]agent = random.choice(agents)#print agent#opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)')]opener.addheaders=[('User-agent',agent)]urllib2.install_opener(opener)req=urllib2.Request(url+action,urllib.urlencode(logindata))content = ""  err_count = 0flage = True  while flage:try:u=urllib2.urlopen(req)content=u.read()flage=FalsegetResultInfo(content)except urllib2.HTTPError, e:if err_count > 10:exit(1)err_count += 1print eflage = Trueif e.getcode()== 403:print "Wait for 5 minutes..."        time.sleep(5 * 60)#print contentreturn contentdef getResultInfo(content):p = re.compile('<div\s*id="messagetext"\s*class="alert_error"\s*>\s*<p>(.*?)\s*<script\s*')m = p.findall(content)if len(m) > 0:for i in m:print  i.replace('</p>','').decode('gbk','ignore')return m[0].replace('</p>','')else:return ''def post(fid,title,contents):action='forum.php?mod=post&action=newthread&fid='+fidrequest=urllib2.Request(url+action,urllib.urlencode(''))content = ""  err_count = 0flage = True  while flage:try:page=urllib2.urlopen(request)content=page.read()flage=Falseexcept urllib2.HTTPError, e:if err_count > 10:exit(1)err_count += 1print eflage = Trueif e.getcode() == 403:print "Wait for 5 minutes..."time.sleep(5 * 60)#print content.decode('gbk')try:getResultInfo(content)str='<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>'reObj=re.compile(str)allMatch=reObj.findall(content)formhash=allMatch[0]#print formhashstr='<input\s*type="hidden"\s*name="posttime"\s*id="posttime"\s*value="(.*?)"\s*\/>'reObj=re.compile(str)allMatch=reObj.findall(content)posttime=allMatch[0]#print posttimeexcept Exception,e:print e#print contentexit(1)#print contentpostdata=(("formhash",formhash),("posttime",posttime),("wysiwyg","1"),("subject",title),("message",contents),("save",""),("usesig","1"),("allownoticeauthor","1")) #,("typeid","28")params=urllib.urlencode(postdata,'gbk')#print paramsaction='forum.php?mod=post&action=newthread&fid='+fid+'&extra=&topicsubmit=yes'request=urllib2.Request(url+action,params)content = ""  err_count = 0flage=True  while flage:try:page=urllib2.urlopen(request)content=page.read()flage=Falseexcept urllib2.HTTPError, e:if err_count > 10:exit(1)err_count += 1print eflage = Trueif e.getcode() == 403:print "Wait for 5 minutes..."time.sleep(5 * 60)getResultInfo(content)#f=open('test.html',"w")#f.write(content)#f.close()def postAllFile(path,fid):if(path==''):path='./'print 'null'else:for fileName in os.listdir (path):if fnmatch.fnmatch ( fileName, '*.txt' ):initsize=900print 'Posting... '+fileNamef=open(path+fileName,"r")contents=f.read()fileName=fileName.replace('.txt','')count=0totallen=len(contents)oder=0while count<= totallen:if count==0:fileName=unicode(fileName,'utf-8','ignore').encode('gbk','ignore')else:fileName=unicode(fileName+'继'+str(oder),'utf-8','ignore').encode('gbk','ignore')c= unicode(contents[count:count+initsize],'utf-8','ignore').encode('gbk','ignore')post(fid,fileName,c)#time.sleep(20)#print fileName#print ccount+=initsizeoder+=1f.close()if __name__ == "__main__":fid=0if len(sys.argv) < 2:print "usage :", sys.argv[0], "<username> <password> <url> <fid> "exit(1)else:try:username = sys.argv[1]password=sys.argv[2]if len(sys.argv) >3:url=sys.argv[3]if len(sys.argv)>4:fid=sys.argv[4]#print username+' '+password+' '+url+' '+fidlogin(username,password)postAllFile('./',fid)except Exception,e:print 'Error'print e