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

时间:2021-10-13 02:15:06 


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='\r\n'
        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='\r\n--'+BOUNDARY+'--\r\n'
        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,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,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_()

标签:新浪微博api,上传图片
0
投稿

猜你喜欢

  • Python判断某个用户对某个文件的权限

    2023-12-15 01:25:27
  • Python字符串和其常用函数合集

    2023-07-12 15:29:22
  • 一篇文章带你入门python之推导式

    2022-02-05 08:29:35
  • 浅谈Golang内存逃逸

    2023-08-27 07:51:55
  • asp 实现的冒泡排序程序

    2011-03-25 11:13:00
  • 构建成功web应用的十项黄金法则

    2010-09-17 19:11:00
  • ASP.NET Core Authentication认证实现方法

    2023-07-21 12:21:31
  • Ajax+Servlet+jsp显示搜索效果

    2023-06-14 08:29:34
  • 在MySQL数据库中如何修改密码及访问限制

    2008-11-27 16:36:00
  • python库pydantic的简易入门教程

    2022-06-27 14:05:28
  • ASP 支持中文的len(),left(),right()的函数代码

    2011-03-03 10:59:00
  • HTML5中 b 和 i 标签将语义化

    2008-03-16 13:43:00
  • asp.net C#实现解压缩文件的方法

    2023-07-14 10:34:01
  • JS清空上传控件input(type="file")的值的代码第1/2页

    2023-08-13 07:22:28
  • golang实现的文件上传下载小工具

    2023-06-28 05:34:34
  • JS获取checkbox的个数简单实例

    2023-08-20 15:17:55
  • Http头 Range、Content-Range

    2010-06-25 19:19:00
  • Mootools 1.2教程(19)——Tooltips

    2008-12-25 13:26:00
  • Windows安装Anaconda并且配置国内镜像的详细教程

    2023-07-06 13:45:15
  • Python实现超快窗口截图功能详解

    2023-11-05 13:54:07
  • asp之家 网络编程 m.aspxhome.com