加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python使用新浪微博api上传图片到微博示例

发布时间:2020-12-16 20:07:42 所属栏目:Python 来源:网络整理
导读:复制代码 代码如下: import urllib.parse,os.path,time,sys from http.client import HTTPSConnection from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * #path ospath=sys.path[0] if len(ospath)!=3: ospath+='' osp

复制代码 代码如下:

import urllib.parse,os.path,time,sys
from http.client import HTTPSConnection
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

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

#api
class Api:
    def sina(self,status,pic):
        fSize=os.path.getsize(pic)
        BOUNDARY="$-img-lufei-goodboy-$"
        CRLF='rn'
        data=[
            #token
            '--'+BOUNDARY,
            'Content-disposition: form-data; name="access_token"',
            '',
            'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',#你的access_token
            #status
            '--'+BOUNDARY,
            'Content-disposition: form-data; name="status"',
            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='rn--'+BOUNDARY+'--rn'
        sumlen=len(data)+len(closing)+fSize
        #----------------------------------------
        h=HTTPSConnection('upload.api.weibo.com')
        h.putrequest('POST','/2/statuses/upload.json')
        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')
api=Api()
#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
        self.line=QLineEdit()
        brows=QPushButton('打开')
        brows.clicked.connect(self.getFileName)
        submit=QPushButton('发表')
        submit.clicked.connect(self.submit)
        #layout
        layout=QGridLayout()
        layout.setContentsMargins(0,0)
        #addwidget
        layout.addWidget(self.editor,1,2)
        layout.addWidget(self.line,1)
        layout.addWidget(brows,1)
        layout.addWidget(submit,2,2)
        #set
        self.setLayout(layout)
    def getFileName(self):
        fileName=QFileDialog.getOpenFileName()
        self.line.setText(fileName[0])
    def submit(self):
        status=self.editor.toPlainText()
        pic=self.line.text()
        self.editor.setText(api.sina(status,pic))
app=QApplication(sys.argv)
dialog=Dialog()
dialog.show()
app.exec_()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读