欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python发腾讯微博代码分享

程序员文章站 2022-05-21 08:44:36
复制代码 代码如下:import urllib.parse,os.path,time,sys,re,urllib.requestfrom http.client impor...

复制代码 代码如下:

import urllib.parse,os.path,time,sys,re,urllib.request
from http.client import httpsconnection
from pyqt5.qtcore import *
from pyqt5.qtgui import *
from pyqt5.qtwidgets import *
from pyqt5.qtwebkitwidgets import *
from pyqt5.qtnetwork import *

#path
ospath=sys.path[0]
if len(ospath)!=3:
    ospath+='\\'
ospath=ospath.replace('\\','/')

#api
class api:
    def getopenid(self,token):
        url="https://graph.qq.com/oauth2.0/me?access_token=%s" % token
        u=urllib.request.urlopen(url)
        data=u.read()
        try:
            data=data.decode('utf-8')
        except:
            data=data.decode('gbk')
        openid=re.findall('"openid":"(.+?)"',data)[0]
        return openid
    def qq(self,token,status,pic):
        fsize=os.path.getsize(pic)
        boundary="$-img-lufei-goodboy-$"
        crlf='\r\n'
        data=[
            '--'+boundary,
            'content-disposition: form-data; name="access_token"',
            '',
            token,
            '--'+boundary,
            'content-disposition: form-data; name="openid"',
            '',
            self.getopenid(token),
            '--'+boundary,
            'content-disposition: form-data; name="oauth_consumer_key"',
            '',
            '100451446',
            #status
            '--'+boundary,
            'content-disposition: form-data; name="content"',
            '',
            status,
            #pic
            '--'+boundary,
            'content-disposition: form-data; name="pic"; filename="q_17.jpg"',
            'content-type: image/jpeg',
            ''
        ]
        #utf-8
        data=(crlf.join(data)+crlf).encode('utf-8')
        closing='\r\n--'+boundary+'--\r\n'
        sumlen=len(data)+len(closing)+fsize
        #----------------------------------------
        h=httpsconnection('graph.qq.com')
        h.putrequest('post','/t/add_pic_t')
        h.putheader('content-type','multipart/form-data; boundary=%s' % boundary)
        h.putheader('content-length',sumlen)
        h.endheaders()
        h.send(data)
        f=open(pic,'rb')
        while true:
            data=f.read(12345)
            if not data:
                break
            h.send(data)
        f.close()
        h.send(closing.encode('utf-8'))
        r=h.getresponse()
        return r.read().decode('utf-8','ignore')
#webview
class webview(qwebview):
    token=none
    def __init__(self):
        super().__init__()
        self.resize(800,500)
        self.setwindowflags(qt.framelesswindowhint|qt.x11bypasswindowmanagerhint|qt.tool)
        self.cookiejar=qnetworkcookiejar()
        self.page().networkaccessmanager().setcookiejar(self.cookiejar)
        url="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s" % ('100451446','http://lufei.fboat.net/api/qq.php','---i---love---you---')
        self.load(qurl(url))
        #sigal
        self.loadprogress.connect(self.judge)
    def judge(self):
        url=re.findall(r"'(.+?)'",str(self.url()))[0]
        if url=='http://lufei.fboat.net/':
            for x in self.cookiejar.allcookies():
                if x.domain()=='lufei.fboat.net' and x.name()=='token':
                    self.token=re.findall("'(.+?)'",str(x.value()))[0]
            self.close()
#ui
class dialog(qdialog):
    def __init__(self):
        super().__init__()
        #icon,title
        self.setwindowicon(qicon(ospath+'weibo.ico'))
        self.setwindowtitle('weibo')
        #texteditor
        self.editor=qtextedit()
        #textline,filebutton,submit,login
        self.line=qlineedit()
        brows=qpushbutton('打开')
        brows.clicked.connect(self.getfilename)
        submit=qpushbutton('发表')
        submit.clicked.connect(self.submit)
        login=qpushbutton('登录')
        login.clicked.connect(self.view)
        #layout
        layout=qgridlayout()
        layout.setcontentsmargins(0,0,0,0)
        #addwidget
        layout.addwidget(self.editor,0,0,1,2)
        layout.addwidget(self.line,1,0,1,1)
        layout.addwidget(brows,1,1,1,1)
        layout.addwidget(submit,2,0,1,1)
        layout.addwidget(login,2,1,1,1)
        #set
        self.setlayout(layout)
    def getfilename(self):
        filename=qfiledialog.getopenfilename()
        self.line.settext(filename[0])
    def view(self):
        webview.show()
    def submit(self):
        status=self.editor.toplaintext()
        pic=self.line.text()
        self.editor.settext(api.qq(webview.token,status,pic))
app=qapplication(sys.argv)
webview=webview()
api=api()
dialog=dialog()
dialog.show()
app.exec_()