#!/usr/bin/python
#coding=utf-8
#post by EvilBinary 小E 实现Discuz X 的post自动提交。
#Filename: post.py

import urllib,urllib2,cookielib,re,os
import fnmatch,sys,time,random

url ='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 = 0
	flage = True
  	while flage:
		try:
			u=urllib2.urlopen(req)
			content=u.read()
			flage=False
			getResultInfo(content)
		except urllib2.HTTPError, e:
			if err_count > 10:
				exit(1)
			err_count += 1
			print e
			flage = True
			if e.getcode()== 403:
				print "Wait for 5 minutes..."
        		time.sleep(5 * 60)

	#print content
	return content

def 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='+fid
	request=urllib2.Request(url+action,urllib.urlencode(''))
	content = ""
  	err_count = 0
	flage = True
  	while flage:
		try:
			page=urllib2.urlopen(request)
			content=page.read()

			flage=False
		except urllib2.HTTPError, e:
			if err_count > 10:
				exit(1)
			err_count += 1
			print e
			flage = True
			if 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 formhash
		str='<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 posttime
	except Exception,e:
		print e
		#print content
		exit(1)
	#print content
	postdata=(("formhash",formhash),("posttime",posttime),("wysiwyg","1"),("subject",title),("message",contents),("save",""),("usesig","1"),("allownoticeauthor","1")) #,("typeid","28")
	params=urllib.urlencode(postdata,'gbk')
	#print params
	action='forum.php?mod=post&action=newthread&fid='+fid+'&extra=&topicsubmit=yes'
	request=urllib2.Request(url+action,params)
	content = ""
  	err_count = 0
	flage=True
  	while flage:
		try:
			page=urllib2.urlopen(request)
			content=page.read()
			flage=False
		except urllib2.HTTPError, e:
			if err_count > 10:
				exit(1)
			err_count += 1
			print e
			flage = True
			if 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=900
				print 'Posting... '+fileName
				f=open(path+fileName,"r")
				contents=f.read()
				fileName=fileName.replace('.txt','')
				count=0
				totallen=len(contents)
				oder=0
				while 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 c
					count+=initsize
					oder+=1
				f.close()
				

if __name__ == "__main__":
	fid=0
	if 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+' '+fid
			login(username,password)
			postAllFile('./',fid)
		except Exception,e:
			print 'Error'
			print e

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐