网络资源
全部免费下载

149行代码打造微信机器人实现下载百度文库

149行代码打造微信机器人实现下载百度文库
使用方法:
1、到python官网下载对应系统的安装程序,下载并安装,(如果自己有请忽略这条)
2、安装完成后打开命令提示符CMD,输入pip install requests回车,然后输入pip install itchat回车。(如果有也请忽略)
3、打开IDLE (Python 3.7 32-bit或其他版本)——左上角file——new file——在弹出窗口粘贴txt文件里的源码——替换自己的token——ctrl+s选择文件夹和命名保存——F5运行
(初步运行就这样,长时间运行的话,最好打个包)

请注意源码需要在第110行替换自己的短链接token,
访问”https://dwz.cn/console/userinfo”登录百度账号,即可免费获取令牌

源码:

import requests
import json
import itchat
import re

def qun(): #多群名输入管理
qun = []
qun_input = input("你要管理的群昵称:")
while qun_input != "":
qun.append(qun_input)
qun_input = input("你要管理的群昵称(结束请直接回车):")
print("您一共输入了以下群名:", end="")
for i in range(len(qun)):
print(qun[i], end="、")
return qun

qunlist=qun()

def get_cookie():#获取域名访问所需要的__cfduid值
header = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language":"zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3",
"Upgrade-Insecure-Requests": "1",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763",
"Accept-Encoding": "gzip, deflate",
"Host":"www.blpack.com",
"Connection": "Keep-Alive"
}
res = requests.get("http://www.blpack.com/", headers=header)
cookie = res.headers["Set-Cookie"]
cookie = re.findall("uid=(.*?);", cookie)
return cookie[0]

def GET_SHORTURL(firsturl):
try:
def get_long_url():
usrname_list = ["901961495", "824756833", "942115816", "369069317"]
usrpwd_list =["559448", "928041", "440742", "588874"]
for i in range(len(usrname_list)):
usrname = usrname_list[i]
usrpwd = usrpwd_list[i]
url1 = "http://www.blpack.com/post.php"# 以下用到了两个链接,一个是查询文档ID的,另一个是下载的
url3 = "http://www.blpack.com/downdoc.php"
# 将传入的文档链接进行转化
downloadurl = firsturl.replace("/", "%2F").replace(":", "%3A")
__cfduid = get_cookie()

def query(): #查询文档ID
head1 = {"POST": "http://www.blpack.com/post.php HTTP/1.1",
"Host": "www.blpack.com",
"Content-Length": "145",
"Accept": "*/*",
"Origin": "http://www.blpack.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": "http://www.blpack.com/",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cookie": "__cfduid={}; usrname={}; usrpwd={}".format(__cfduid, usrname, usrpwd)
}
data1 = 'usrname={}&usrpass={}&docinfo={}&taskid=up_down_doc1'.format(usrname, usrpwd, downloadurl)
respons = requests.post(url1, data=data1, headers=head1).json()
id = respons['url']
id = id[36:] #将response的链接截取id值
return id

def down(): # 获取下载链接
id = query()
Referer = "http://www.blpack.com/nocode.php?id={docid}"
head3 = {"POST": "http://www.blpack.com/downdoc.php HTTP/1.1",
"Host": "www.blpack.com",
"Connection": "keep-alive",
"Content-Length": "54",
"Accept": "*/*",
"Origin": "http://www.blpack.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": Referer.format(docid=id),
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cookie": "__cfduid={}; usrname={}; usrpwd={}".format(__cfduid, usrname, usrpwd)
}
data3 = 'vid={docid}&taskid=directDown'.format(docid=id)
response = requests.post(url3, data=data3, headers=head3).json()
if response["result"]=="down_succ":
downurl = response["dlink"] #获取下载长链接
return downurl
else:
downurl = response["msg"]
print(downurl)

downurl = down()
if downurl[0:5]=="https": #判断账号是否可用,可用进行下一步
return downurl
elif i == len(usrname_list)-1:
back = "所有账号均不可用"
return back
else:
i += 1

def short_url(long_url): #将长链接变成短链接
host = 'https://dwz.cn'
path = '/admin/v2/create'
url = host + path
url1 = long_url
content_type = 'application/json'
# TODO: 设置Token
token = '请填写自己的token值' #访问"https://dwz.cn/console/userinfo"登录百度账号,即可免费获取令牌
# TODO:设置待创建的长网址
bodys = {'url': url1}
# 配置headers
headers = {'Content-Type': content_type, 'Token': token}
# 发起请求
response = requests.post(url=url, data=json.dumps(bodys), headers=headers).json()
# 读取响应
if response['Code'] == 0:
return response["ShortUrl"]
else:
return response.text

long_url = get_long_url()
if long_url[0:5] == "https": #判断是否获得长链接,是则下一步
res = short_url(long_url)
return res
else:
return long_url
except:
r = "获取下载链接失败,请检查是否是付费文档,或者地址是否正确"
return r

def login_req():
@itchat.msg_register([itchat.content.TEXT], isGroupChat=True) # 群消息的处理
def print_content(msg):
if msg.User['NickName'] in qunlist:#判断获取消息的群名是否在qun列表中
if str(msg['Content'][0:5])=="https": #如果消息开头为"https"判断为网址输入
backurl= GET_SHORTURL(str(msg['Content']))
print(msg.User['NickName'] + ":" + msg['Text'] ) # 打印哪个群给你发了什么消息
print(backurl) # 打印机器人回复的消息
itchat.send(u'@%s\u2005 下载链接:%s' % (msg['ActualNickName'],backurl), msg['FromUserName'])
else:# 不是链接直接忽略
print(msg['Text'])
else:#不是相应群直接忽略
pass

itchat.auto_login(hotReload=True) #登录微信,并保持热加载,短时间内再次登录无需扫码
login_req()
itchat.run()

赞(3) 打赏
未经允许不得转载:福利年 » 149行代码打造微信机器人实现下载百度文库

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏