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
投稿

猜你喜欢

  • 检查access数据库中是否存在某个名字的表的asp代码

    2011-04-02 11:20:00
  • 利用C#远程存取Access数据库

    2024-01-27 01:58:32
  • 有关Python的22个编程技巧

    2021-04-05 22:58:05
  • MySQL存储过程中使用动态行转列

    2024-01-16 22:03:16
  • Python Base64编码和解码操作

    2022-10-20 06:26:05
  • JavaScript门道之标准库

    2024-06-05 09:53:21
  • Python可视化神器pyecharts绘制仪表盘

    2021-12-29 15:57:53
  • pytorch 实现情感分类问题小结

    2022-11-06 12:21:00
  • python读取excel进行遍历/xlrd模块操作

    2022-11-09 18:44:51
  • 解决os.path.isdir() 判断文件夹却返回false的问题

    2022-11-07 18:11:37
  • JavaScript实现解析INI文件内容的方法

    2024-04-25 13:10:23
  • PHP实现页面静态化的超简单方法

    2023-11-18 17:13:24
  • PyQt5打开文件对话框QFileDialog实例代码

    2022-08-10 03:38:47
  • python执行等待程序直到第二天零点的方法

    2023-08-27 11:49:21
  • Python+MediaPipe实现检测人脸功能详解

    2021-05-05 15:34:17
  • mysql仿oracle的decode效果查询

    2024-01-12 22:04:00
  • python单线程文件传输的实例(C/S)

    2023-04-07 22:45:48
  • 400多行Python代码实现了一个FTP服务器

    2023-12-10 19:31:31
  • 在mac下查找python包存放路径site-packages的实现方法

    2023-06-12 21:12:36
  • python flask实现分页的示例代码

    2021-04-01 09:55:23
  • asp之家 网络编程 m.aspxhome.com