简单的模拟登陆发帖例子

#!/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')

  

posted @ 2017-06-19 23:14  王芬老师  阅读(219)  评论(0编辑  收藏  举报