Copy codeThe Code is as follows:
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 "',
'',
'Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # Your 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 (1, 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. setw.wicon (QIcon (ospath + 'weibo. ico '))
Self. setWindowTitle ('weibo ')
# Texteditor
Self. editor = QTextEdit ()
# Textline, filebutton, submit
Self. line = QLineEdit ()
Brows = QPushButton ('Open ')
Brows. clicked. connect (self. getFileName)
Submit = QPushButton ('posting ')
Submit. clicked. connect (self. submit)
# Layout
Layout = QGridLayout ()
Layout. setContentsMargins (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)
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.exe c _()