#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : zhibo.wang
# E-mail : gm.zhibo.wang@gmail.com
# Date :
# Desc :
import re
import os
import json
import time
import logging
import requests
from bs4 import BeautifulSoup
from logging import handlers
class Logger(object):
# 日志级别关系映射
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}
def __init__(self,
filename,
level='info',
when='midnight',
back_count=7,
fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
f_dir, f_name = os.path.split(filename)
os.makedirs(f_dir, exist_ok=True) # 当前目录新建log文件夹
self.logger = logging.getLogger(filename)
format_str = logging.Formatter(fmt) # 设置日志格式
self.logger.setLevel(self.level_relations.get(level)) # 设置日志
sh = logging.StreamHandler() # 往屏幕上输出
sh.setFormatter(format_str) # 设置屏幕上显示的格式
th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=back_count,
encoding='utf-8') # 往文件里写入指定间隔时间自动生成文件的Handler
# 实例化TimedRotatingFileHandler
# interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
# S 秒
# M 分
# H 小时
# D 天
# 'W0'-'W6' 每星期(interval=0时代表星期一:W0)
# midnight 每天凌晨
th.setFormatter(format_str) # 设置文件里写入的格式
self.logger.addHandler(sh) # 把对象加到logger里
self.logger.addHandler(th)
class RunLiuLi():
def __init__(self):
self.user_name = ""
self.user_pwd = ""
self.logger = Logger('./logs/liulifrp_daily_sign.log', 'debug').logger
self.time_sleep = 10
self.session = requests.session()
self.cookie = None
self.headers = {
'Origin': 'https://liulifrp.cn',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9'
}
def init_cookies(self):
status = False
try:
index_url = "https://liulifrp.cn/"
params = {
"page": "login"
}
index_resp = self.session.get(url=index_url, params=params, headers=self.headers)
if index_resp.status_code == 200:
cookie_items = index_resp.cookies.items()
if len(cookie_items) > 0:
self.cookie = "; ".join(["=".join(k_v) for k_v in cookie_items])
status = True
except:
pass
return status
def login(self):
time.sleep(self.time_sleep)
status = False
try:
login_url = "https://liulifrp.cn/"
params = {
"action": "login",
"page": "login"
}
payload = 'g-recaptcha-response=&email={0}&password={1}'.format(
self.user_name, self.user_pwd
)
headers = self.headers
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Referer"] = "https://liulifrp.cn/?page=login"
headers["Cookie"] = self.cookie
login_resp = requests.post(
url=login_url,
params=params,
data=payload,
headers=headers)
if login_resp.status_code == 200:
# data-cf-settings
status = True
except:
pass
return status
def get_csrf(self):
time.sleep(self.time_sleep)
csrf = None
try:
url = "https://liulifrp.cn/"
params = {
"page": "panel",
"module": "sign"
}
headers = self.headers
headers["Referer"] = "https://liulifrp.cn/?page=login"
headers["Cookie"] = self.cookie
csrf_resp = requests.get(url=url, params=params, headers=headers)
# print(csrf_resp.status_code, csrf_resp.text)
if csrf_resp.status_code == 200:
csrf_re = re.findall('csrf_token = "(.+?)";', csrf_resp.text)
if len(csrf_re) > 0:
csrf = csrf_re[0]
except:
pass
return csrf
def daily_sign(self, csrf):
time.sleep(self.time_sleep)
headers = self.headers
headers["Referer"] = "https://liulifrp.cn/?page=login"
headers["Cookie"] = self.cookie
url = "https://liulifrp.cn/?page=panel&module=sign&sign&csrf={}".format(csrf)
sign_resp = requests.get(url=url, headers=headers)
if sign_resp.status_code == 200:
end_text = sign_resp.text
self.logger.info("签到信息: {}".format(end_text))
else:
self.logger.info("签到信息失败")
def get_info(self):
info_text = None
# card-body p-0 table-responsive
try:
url = "https://liulifrp.cn/?page=panel&module=profile"
headers = self.headers
headers["Referer"] = "https://liulifrp.cn/?page=panel&module=nodeinfo"
headers["Cookie"] = self.cookie
info_resp = requests.get(url=url, headers=headers)
if info_resp.status_code == 200:
info_text = info_resp.text
soup = BeautifulSoup(info_text, "html.parser")
car_bodys = soup.find_all("div", attrs={"class": "card-body p-0 table-responsive"})
if len(car_bodys) > 0:
info_text = car_bodys[-1].get_text().strip()
# self.logger.info(info_text)
except:
pass
return info_text
def run(self):
init_status = self.init_cookies()
if init_status:
self.logger.info("初始化成功")
login_status = self.login()
if login_status:
self.logger.info("登录成功")
csrf_status = self.get_csrf()
if csrf_status:
self.logger.info("获取参数成功")
self.daily_sign(csrf_status)
info_text = self.get_info()
if info_text:
self.logger.info("账户内网穿透使用情况获取成功")
self.logger.info(info_text)
else:
self.logger.info("账户内网穿透使用情况获取失败")
else:
self.logger.info("获取参数失败")
else:
self.logger.info("登录失败")
else:
self.logger.info("初始化失败")
if __name__ == "__main__":
R = RunLiuLi()
R.run()
