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 (' open ')
Brows.clicked.connect (Self.getfilename)
Submit=qpushbutton (' Publish ')
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_ ()