#!/usr/bin/env python # encoding: utf-8 import re import urllib import os,datetime import sys import time from selenium import webdriver from BeautifulSoup import BeautifulSoup from DiscuzAPI import DiscuzAPI def getWorkdays(): holiday_list = [] overtime_list = [] days_list = [] workday_list = [] url = 'http://www.baidu.com/s?' + urllib.urlencode({'wd': '日曆'}) date_pattern = re.compile(r'date="[\d]+[-][\d]+[-][\d]+"') driver = webdriver.PhantomJS(service_log_path=os.path.devnull) driver.get(url) html = driver.page_source driver.quit() soup = BeautifulSoup(html) td_div_list = soup.findAll('div',{'class':'op-calendar-new-relative'}) for td_tag in td_div_list: href_tag = str(td_tag.a) date_list = date_pattern.findall(href_tag) if len(date_list) > 0: days_list.append(date_list[0].split('"')[1]) if href_tag.find('休') != -1: holiday_list.append(date_list[0].split('"')[1]) if href_tag.find('班') != -1: overtime_list.append(date_list[0].split('"')[1]) for day_format in days_list: day = datetime.datetime.strptime(day_format, '%Y-%m-%d').date() if day_format in overtime_list: workday_list.append(day) elif day.weekday() not in [5, 6] and day_format not in holiday_list: workday_list.append(day) return workday_list def getRangeThisWeek(workday_list): """獲取本週工做日區間""" today = datetime.date.today() if today not in workday_list: # 非工做日,返回空鏈表 return [] else: for i in range(1,10): pres_day = today - datetime.timedelta(days=i) if pres_day not in workday_list: startday = pres_day + datetime.timedelta(days=1) break for i in range(1,10): next_day = today + datetime.timedelta(days=i) if next_day not in workday_list: endday = next_day - datetime.timedelta(days=1) break return [startday, endday] if __name__ == '__main__': # 從百度「日曆」獲取工做日列表 workday_list = getWorkdays() workday_range = getRangeThisWeek(workday_list) url = "http://bbs.cnfol.wh" username = "report" password = "eQIi}38" robot = DiscuzAPI(url, username, password) robot.login() # 發日報主題貼 fid_wh = 52 fid_fz = 72 msg = u"格式:\n\"\"\"\n今日工做內容:\n\n明日工做計劃:\n\n\"\"\"" today = datetime.date.today() subject = today.strftime('%Y%m%d').encode('utf-8') if today in workday_list: # 檢查主題是否已經存在了,武漢 result = robot.isSubExisted(fid = fid_wh, subject = subject) if result == False: print "wh主題貼發佈:" + subject robot.publish(fid = fid_wh, subject = subject, msg = msg) else: print "wh主題已存在,不重複發佈:" + subject # discuz 發貼時間間隔,默認15s #time.sleep() # 福州日報 result = robot.isSubExisted(fid = fid_fz, subject = subject) if result == False: print "fz主題貼發佈:" + subject robot.publish(fid = fid_fz, subject = subject, msg = msg) else: print "fz主題已存在,不重複發佈:" + subject # 發週報主題貼 fid_wh_week = 51 fid_fz_week = 73 msg = u"參照[url=http://bbs.cnfol.wh/forum.php?mod=viewthread&tid=20&extra=page%3D1]週報規範[/url]進行回覆。" if len(workday_range) > 1: subject = workday_range[0].strftime('%Y%m%d').encode('utf-8') + "-" + workday_range[1].strftime('%Y%m%d').encode('utf-8') result = robot.isSubExisted(fid = fid_wh_week, subject = subject) if result == False: print "wh_week主題貼發佈:" + subject robot.publish(fid=fid_wh_week, subject = subject, msg = msg) else: print "wh_week主題貼已存在,不重複發佈:" + subject result = robot.isSubExisted(fid = fid_fz_week, subject = subject) if result == False: print "fz_week主題貼發佈:" + subject robot.publish(fid=fid_fz_week, subject = subject, msg = msg) else: print "fz_week主題貼已存在,不重複發佈:" + subject
#! /usr/bin/env python # -*- coding: utf-8 -*- """ base by Conanca image upload by N3il """ import urllib2 import urllib import cookielib import random import string import re import time import sys import httplib import mimetools import mimetypes httplib.HTTPConnection.debuglevel = 1 class DiscuzAPI: def __init__(self, forumUrl, userName, password, proxy = None): ''' 初始化論壇url、用戶名、密碼和代理服務器 ''' self.forumUrl = forumUrl self.userName = userName self.password = password self.formhash = '' self.isLogon = False self.isSign = False self.xq = '' self.jar = cookielib.CookieJar() if not proxy: openner = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.jar)) else: openner = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.jar), urllib2.ProxyHandler({'http' : proxy})) urllib2.install_opener(openner) def login(self): ''' 登陸論壇 ''' url = self.forumUrl + "/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&inajax=1"; postData = urllib.urlencode({'username': self.userName, 'password': self.password, 'answer': '', 'cookietime': '2592000', 'handlekey': 'ls', 'questionid': '0', 'quickforward': 'yes', 'fastloginfield': 'username'}) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read() if self.userName.encode('utf-8') in content: self.isLogon = True #print 'logon success!' self.initFormhashXq() return 1 else: print 'logon faild!' return 0 def initFormhashXq(self): ''' 獲取formhash和心情 ''' content = urllib2.urlopen(self.forumUrl + '/plugin.php?id=dsu_paulsign:sign').read().decode('utf-8', 'ignore') rows = re.findall(r'<input type=\"hidden\" name=\"formhash\" value=\"(.*?)\" />', content) if len(rows)!=0: self.formhash = rows[0] #print 'formhash is: ' + self.formhash else: print 'none formhash!' rows = re.findall(r'<input id=.* type=\"radio\" name=\"qdxq\" value=\"(.*?)\" style=\"display:none\">', content) if len(rows)!=0: self.xq = rows[0] print 'xq is: ' + self.xq elif u'已經簽到' in content: self.isSign = True print 'signed before!' else: #print 'none xq!' pass def reply(self, tid, subject = u'',msg = u'支持~~~頂一下下~~嘻嘻'): ''' 回帖 ''' url = self.forumUrl + '/forum.php?mod=post&action=reply&fid=41&tid='+str(tid)+'&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1' postData = urllib.urlencode({'formhash': self.formhash, 'message': msg.encode('utf-8'), 'subject': subject.encode('gbk'), 'posttime':int(time.time()) }) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read().decode('utf-8', 'ignore') #print content if u'發佈成功' in content: print 'reply success!' else: print 'reply faild!' def publish(self, fid, subject, msg, typeid = 125, imgId = ""): ''' 發帖 ''' url = self.forumUrl + '/forum.php?mod=post&action=newthread&fid='+ str(fid) +'&extra=&topicsubmit=yes' """ formhash=d649673a&posttime=1367460177&wysiwyg=1&subject=test&unused%5B%5D=70554 &message=tset123214141&save=&attachnew%5B70555%5D%5Bdescription%5D=&usesig=1&allownoticeauthor=1 """ postData = urllib.urlencode( {'formhash': self.formhash, 'message': msg.encode('utf-8'), 'subject': subject.encode('utf-8'), 'posttime':int(time.time()), 'addfeed':'1', 'allownoticeauthor':'1', 'checkbox':'0', 'newalbum':'', 'readperm':'', 'rewardfloor':'', 'rushreplyfrom':'', 'rushreplyto':'', 'save':'', 'stopfloor':'', #'typeid':typeid, 'attachnew[%s][description]' % imgId: "", 'uploadalbum':'', 'usesig':'1', 'wysiwyg':'0' }) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read().decode('utf-8', 'ignore') if subject in content: print 'publish success!' return 1 else: print 'publish faild!' return 0 def sign(self,msg = u'哈哈,我來簽到了!'): ''' 簽到 ''' if self.isSign: return if self.isLogon and self.xq: url = self.forumUrl + '/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1' postData = urllib.urlencode({'fastreply': '1', 'formhash': self.formhash, 'qdmode': '1', 'qdxq': self.xq, 'todaysay':msg.encode('utf-8') }) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read().decode('utf-8', 'ignore') #print content if u'簽到成功' in content: self.isSign = True print 'sign success!' return print 'sign faild!' def speak(self,msg = u'hah,哈哈,測試一下!'): ''' 發表心情 ''' url = self.forumUrl + '/home.php?mod=spacecp&ac=doing&handlekey=doing&inajax=1' postData = urllib.urlencode({'addsubmit': '1', 'formhash': self.formhash, 'referer': 'home.php', 'spacenote': 'true', 'message':msg.encode('utf-8') }) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read().encode('utf-8') #print content if u'操做成功' in content: print 'speak success!' else: print 'speak faild!' def uploadImage(self, imageData, fid=21): imageId = None # get the uid and hash url = self.forumUrl + "/forum.php?mod=post&action=newthread&fid=%d&extra=" % fid data = urllib2.urlopen(url).read().decode('utf-8', 'ignore') hashReg = re.compile(r"<input type=\"hidden\" name=\"hash\" value=\"(.*?)\">", re.S) uidReg = re.compile(r"discuz_uid = '(.*?)'", re.S) hashRet = hashReg.search( data ).group(1) uid = uidReg.search( data ).group(1) # Upload the image uploadImageUrl = self.forumUrl + "/misc.php?mod=swfupload&operation=upload&simple=1&type=image" refer = self.forumUrl + "/forum.php?mod=post&action=newthread&fid=%d&extra=" % fid randomStr = "7dd" + ''.join( random.sample(string.ascii_lowercase + string.digits, 8) ) CRLF = '\r\n' #BOUNDARY = mimetools.choose_boundary() BOUNDARY = "---------------------------" + randomStr L = [] L.append('--' + BOUNDARY) L.append("Content-Disposition: form-data; name=\"uid\"" ) L.append("") L.append(uid) L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name=\"hash\"') L.append("") L.append(hashRet) L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name=\"Filedata\"; filename=\"testpic.jpg\"') L.append("Content-Type: image/pjpeg") L.append("") L.append( imageData ) L.append('--' + BOUNDARY + '--') L.append("") postData = CRLF.join(str(a) for a in L) #print postData req = urllib2.Request(uploadImageUrl, postData) req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % BOUNDARY ) req.add_header('Content-Length', len(postData) ) req.add_header('Referer', refer ) resp = urllib2.urlopen(req) body = resp.read().decode('utf-8') bodySp = body.split('|') if len(bodySp) == 0: return None if bodySp[0] == u'DISCUZUPLOAD' and bodySp[1] == u'0': imageId = bodySp[2] return imageId def isSubExisted(self, fid, subject): url = self.forumUrl + "/forum.php?mod=forumdisplay&fid=%d" % fid content = urllib2.urlopen(url).read() subject = ">" + subject + "<" if subject.encode('utf-8') in content: return True else: return False if __name__ == '__main__': url = "http://bbs.cnfol.wh" username = "zhangzhao" password = "zhangzhao" fid = 59 robot = DiscuzAPI(url, username, password) robot.login() print robot.isSubExisted(fid=fid, subject=u'20170505')