下面是 Python 实现 T00ls 自动签到脚本代码的完整攻略。
1. 为什么需要自动签到
对于 T00ls(T00ls.net)这个网站,每天都需要签到一次才能获得贡献值,获得更好的体验和权限。如果你忘记了签到或者没有时间,那么就会影响你在 T00ls 上的使用体验。因此,我们可以使用 Python 编写自动签到脚本,在固定的时间自动完成签到,让你的使用更加便捷。
2. 实现自动签到脚本
以下是实现自动签到脚本的步骤:
2.1 安装必要的库
- requests:用于发送 POST 请求进行签到
- configparser:用于读取配置文件中的信息
- smtplib:用于发送邮件
- json:用于处理 JSON 数据
- time:用于处理时间
可以使用以下命令进行安装:
pip install requests configparser smtplib json time
2.2 编写代码
下面是代码实现的过程:
import requests
import configparser
import smtplib
from email.mime.text import MIMEText
import json
import time
# 读取配置文件
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
# 设置请求头
headers = {
'User-Agent': config.get('info', 'user_agent'),
'Cookie': config.get('info', 'cookie')
}
# 签到相关信息
username = config.get('info', 'username')
email_address = config.get('info', 'email_address')
dd_webhook = config.get('info', 'dd_webhook')
# 发送 POST 请求进行签到
url = 'https://www.t00ls.net/ajax-sign.json'
data = {'formhash': config.get('info', 'formhash')}
response = requests.post(url, headers=headers, data=data)
# 解析返回数据
result = json.loads(response.text)
# 邮件通知
subject = 'T00ls自动签到通知'
content = '{},你好:\n\n签到结果:{}\n签到信息:{}'.format(username, result['msg'], result['content'])
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = email_address
msg['To'] = email_address
smtp_server = config.get('info', 'smtp_server')
smtp_port = config.get('info', 'smtp_port')
server = smtplib.SMTP(smtp_server, smtp_port)
server.login(email_address, config.get('info', 'email_passwd'))
server.sendmail(msg['From'], [msg['To']], msg.as_string())
server.quit()
# 钉钉通知
if dd_webhook:
timestamp = str(round(time.time() * 1000))
secret = config.get('info', 'dd_secret')
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
from hashlib import sha256
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = '{}×tamp={}&sign={}'.format(dd_webhook, timestamp, sign)
headers = {'Content-Type': 'application/json;charset=utf-8'}
data = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"isAtAll": False
}
}
requests.post(url, headers=headers, json=data)
2.3 配置文件的设置
在程序运行时,需要读取config.ini
中的信息,下面是config.ini
文件的示例:
[info]
user_agent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
cookie = 在这里填写你的 Cookie 信息
username = 在这里填写你的用户名
formhash = 在这里填写你的 formhash
email_address = 在这里填写你的邮箱地址
email_passwd = 在这里填写你的邮箱密码
smtp_server = 在这里填写你的 SMTP 服务器地址
smtp_port = 在这里填写你的 SMTP 服务器端口
dd_webhook = 在这里填写你的钉钉 Webhook 地址
dd_secret = 在这里填写你的钉钉 Secret
2.4 示例说明
下面是两条示例说明:
2.4.1 示例 1
用户小明在机器上设置一个定时任务,每天定时运行自动签到脚本,然后在邮箱邮箱里查收签到结果。如果签到失败,会显示失败原因。
2.4.2 示例 2
用户小红在机器上设置一个定时任务,每天定时运行自动签到脚本,并通过钉钉机器人通知签到结果。如果签到成功,则不会有通知,如果签到失败,则会有通知。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python 实现 T00ls 自动签到脚本代码(邮件+钉钉通知) - Python技术站