python发腾讯微博代码分享
发布时间:2022-05-27 04:45:00
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('打开')
brows.clicked.connect(self.getFileName)
submit=QPushButton('发表')
submit.clicked.connect(self.submit)
login=QPushButton('登录')
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_()


猜你喜欢
- 本文介绍了笔者通过python程序实现某OA系统自动考勤打卡功能及相关逻辑原理的解析。Github: https://github.com/
- sort 包源码解读前言我们的代码业务中很多地方需要我们自己进行排序操作,go 标准库中是提供了 sort 包是实现排序功能的,这里来看下生
- Git/GitHub/GitHub Desktop相关概念1、GitGit是一款免费的、开源的、最先进的分布式版本控制系统,可以有效、高速地
- 之前遇到过MySQL本地可以连接但是远程连不上的问题,没有记录,今天在云上新申请的服务器上又遇到这个问题,记录一下解决过程。1.排除网络或防
- var iframe = document.createElement("iframe"); iframe.id = &
- 我们可以利用windows提供的api函数来实现对系统键盘事件和鼠标事件的监听,主要利用的是SetWindowsHookEx函数,这个函数可
- Python socket网络编程初学 python,前段时间买了两本书《python 编程从入门到实践》《Python 核心编程第三版》,
- 背景:有一个爬虫服务,需要定时从公开网站上拉取一些数据,为了避免被识别为爬虫(防爬虫的识别需要根据很多特征,时间仅仅是其中一个维度),需要在
- 本文实现了PyQt5个各种弹出窗口:输入框、消息框、文件对话框、颜色对话框、字体对话框、自定义对话框其中,为了实现自定义对话框的返回值,使用
- 前言:创建进程池可以形象地理解为创建一个并行的流水线,只需创建一次流水线的消耗,处理接收到的任务的,不使用进程池。 ,浪费时间。中方本来没有
- php var_dump 函数作用是判断一个变量的类型与长度,并输出变量的数值,如果变量有值输的是变量的值并回返数据类型.来看看var_du
- 以titanic数据集为例。其中name列是字符串,现在想从其中提取title作为新的一列。例如:# create new Title co
- python解释器默认编码(python2与python3的区别一)python2 解释器默认编码:asciipython3 解释器默认编码
- python中的email模块可以方便的解析邮件,先上代码#-*- encoding: gb2312 -*-import osimport
- 先看要实现的饼图效果图方法一:使用seriesseries: [ { &
- 介绍可以针对分区表的每个分区指定各自的存储路径,对于innodb存储引擎的表只能指定数据路径,因为数据和索引是存储在一个文件当中,对于MYI
- 现在我将清除页面缓存的一些方法总结如下:1、在Asp页面首部加入<% Response.Buffer = True
- 为cd2sc.com网站功能而开发,代码为本人原创,生成速度一般。 (出于众所周知的原因,涉及到数据库的数据字段名称做了改动,并且为了代码明
- MYSQL官方提供了Installer方式安装MYSQL服务以及其他组件,使的Windows下安装,卸载,配置MYSQL变得特别简单。1.
- 当你在IE中点击一个Realplayer连接时,系统会自动启动Realplayer软件,不仅占用系统内存,而且在上网时Realplayer容