python发腾讯微博代码分享

时间:2022-05-27 04:45:00 


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_()

标签:腾讯微博代码
0
投稿

猜你喜欢

  • Fibonacci数,Θ(log n)

    2010-03-28 13:28:00
  • 8个js表单验证函数

    2007-10-28 19:19:00
  • ASP常见数学函数 Abs Atn Cos 等详解

    2008-05-28 12:33:00
  • SQLServer Execpt和not in 性能区别

    2012-01-29 17:53:24
  • Python3内置模块pprint让打印比print更美观详解

    2022-02-04 01:55:28
  • 用户体验中的五大要素

    2008-07-07 16:41:00
  • 提高JavaScript执行效率的23个实用技巧

    2023-08-15 18:38:12
  • 斐波那契数列的递归算法优化

    2010-01-23 11:37:00
  • Python爬取网站图片并保存的实现示例

    2023-06-05 18:01:29
  • Python解决多进程间访问效率低的方法总结

    2023-11-25 11:57:43
  • Python实现图像增强

    2022-07-08 10:50:50
  • python使用Qt界面以及逻辑实现方法

    2023-10-23 23:28:09
  • 使用Python3内置文档高效学习以及官方中文文档

    2022-06-13 08:14:45
  • c#中过滤html的正则表达式

    2023-07-04 04:03:24
  • 解决Dreamweaver不支持中文文件名方法

    2008-01-09 12:52:00
  • Excel数据导入到Access,Sql Server中示例代码

    2007-10-07 12:03:00
  • Python中字符串的常见操作技巧总结

    2021-06-26 18:35:45
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容

    2022-02-02 11:08:01
  • 如何使用表单发送电子邮件?

    2010-05-16 15:13:00
  • php 进阶:实现无限分类第1/4页

    2023-11-17 21:04:55
  • asp之家 网络编程 m.aspxhome.com