Python Tencent Weibo code sharing _python

Source: Internet
Author: User
Tags openid

Copy Code code as follows:

Import Urllib.parse,os.path,time,sys,re,urllib.request
From http.client import httpsconnection
From Pyqt5.qtcore Import *
From Pyqt5.qtgui Import *
From pyqt5.qtwidgets Import *
From pyqt5.qtwebkitwidgets Import *
From pyqt5.qtnetwork Import *

#path
OSPATH=SYS.PATH[0]
If Len (Ospath)!=3:
ospath+= ' \ \ '
Ospath=ospath.replace (' \ \ ', '/')

#api
Class Api:
def getopenid (Self,token):
Url= "https://graph.qq.com/oauth2.0/me?access_token=%s"% token
U=urllib.request.urlopen (URL)
Data=u.read ()
Try
Data=data.decode (' Utf-8 ')
Except
Data=data.decode (' GBK ')
Openid=re.findall (' OpenID ":" (. +?) "", data) [0]
Return OpenID
def QQ (Self,token,status,pic):
Fsize=os.path.getsize (pic)
boundary= "$-img-lufei-goodboy-$"
crlf= ' \ r \ n '
data=[
'--' +boundary,
' Content-disposition:form-data; Name= "Access_token",
'',
Token
'--' +boundary,
' Content-disposition:form-data; Name= "OpenID",
'',
Self.getopenid (token),
'--' +boundary,
' Content-disposition:form-data; Name= "Oauth_consumer_key",
'',
' 100451446 ',
#status
'--' +boundary,
' Content-disposition:form-data; Name= "Content" ',
'',
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 (' graph.qq.com ')
H.putrequest (' POST ', '/t/add_pic_t ')
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 ')
#webview
Class WebView (Qwebview):
Token=none
def __init__ (self):
Super (). __INIT__ ()
Self.resize (800,500)
Self.setwindowflags (qt.framelesswindowhint| Qt.x11bypasswindowmanagerhint| Qt.tool)
Self.cookiejar=qnetworkcookiejar ()
Self.page (). Networkaccessmanager (). Setcookiejar (Self.cookiejar)
Url= "Https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state =%s "% (' 100451446 ', ' http://lufei.fboat.net/api/qq.php ', '---I---love---you---')
Self.load (Qurl (URL))
#sigal
Self.loadProgress.connect (Self.judge)
Def judge (self):
Url=re.findall (R "' (. +?) '", str (Self.url ()) [0]
If url== ' http://lufei.fboat.net/':
For x in Self.cookieJar.allCookies ():
If x.domain () = = ' Lufei.fboat.net ' and x.name () = = ' token ':
Self.token=re.findall ("' (. +?) '", str (X.value ()) [0]
Self.close ()
#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,login.
Self.line=qlineedit ()
Brows=qpushbutton (' open ')
Brows.clicked.connect (Self.getfilename)
Submit=qpushbutton (' Publish ')
Submit.clicked.connect (Self.submit)
Login=qpushbutton (' login ')
Login.clicked.connect (Self.view)
#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,1)
Layout.addwidget (login,2,1,1,1)
#set
Self.setlayout (Layout)
def getfilename (self):
Filename=qfiledialog.getopenfilename ()
Self.line.setText (Filename[0])
def view (self):
Webview.show ()
def submit (self):
Status=self.editor.toplaintext ()
Pic=self.line.text ()
Self.editor.setText (API.QQ (webview.token,status,pic))
App=qapplication (SYS.ARGV)
Webview=webview ()
Api=api ()
Dialog=dialog ()
Dialog.show ()
APP.EXEC_ ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.