Skip to content

Commit

Permalink
Merge pull request #2 from alt-0191/revert-1-code-quality
Browse files Browse the repository at this point in the history
Revert "Code quality merge"
  • Loading branch information
alt-0191 authored Apr 10, 2024
2 parents 6524c21 + c1cdd47 commit cf23556
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 127 deletions.
165 changes: 83 additions & 82 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,104 @@
import httpx
import time

# 假设 randomize 是一个自定义模块
from utils.randomize import generate
from utils.location import encrypted_latitude, encrypted_longitude
from utils.wechatPush import send_push_notification
from utils.data import account, password, school_id, province, city, address, address_name, pushPlus_set


class SigninManager:
def __init__(self):
self.token = None
self.cookies = None

def login_and_get_token(self):
token, uuid = generate()
print(f"初始随机token为:{token}")
def login_and_get_token():
token, uuid = generate()
print(f"初始随机token为:{token}")

url = f"https://api.xixunyun.com/login/api?token={token}&from=app&version=4.9.7&school_id={school_id}"
payload = {
'app_version': '4.9.7',
'uuid': uuid,
'request_source': 3,
'platform': 2,
'password': password,
'system': '14',
'school_id': school_id,
'account': account,
'app_id': 'cn.vanber.xixunyun.saas'
}
headers = {
'User-Agent': 'okhttp/3.8.1',
'content-type': 'application/x-www-form-urlencoded',
'Host': 'api.xixunyun.com',
'user-agent': 'okhttp/3.8.1'
}
url = f"https://api.xixunyun.com/login/api?token={token}&from=app&version=4.9.7&school_id={school_id}"
payload = {
'app_version': '4.9.7',
'uuid': uuid,
'request_source': 3,
'platform': 2,
'password': password,
'system': '14',
'school_id': school_id,
'account': account,
'app_id': 'cn.vanber.xixunyun.saas'
}
headers = {
'User-Agent': 'okhttp/3.8.1',
'content-type': 'application/x-www-form-urlencoded',
'Host': 'api.xixunyun.com',
'user-agent': 'okhttp/3.8.1'
}

try:
with httpx.Client() as client:
login_response = client.post(url, headers=headers, data=payload)
login_response_data = login_response.json()
if login_response_data.get('code') == 20000:
self.cookies = login_response.cookies
self.token = login_response.json()['data']['token']
print("登录成功")
print(f"获取cookies成功")
print(f"获取token成功")
else:
print(f"登录失败,信息:{login_response_data.get('message')}")
except httpx.RequestError as e:
print(f"登录时发生请求错误:{e}")
try:
with httpx.Client() as client:
login_response = client.post(url, headers=headers, data=payload)
login_response_data = login_response.json()
if login_response_data.get('code') == 20000:
cookies = login_response.cookies
token = login_response.json()['data']['token']
print("登录成功")
print(f"获取cookies成功")
print(f"获取token成功")
return token, cookies
else:
print(f"登录失败,信息:{login_response_data.get('message')}")
return None, None
except httpx.RequestError as e:
print(f"登录时发生请求错误:{e}")

def signin_with_token(self):
time.sleep(5) # 等待5秒
url = f"https://api.xixunyun.com/signin_rsa?token={self.token}&from=app&version=4.9.7&school_id={school_id}"

payload = {
'address': address,
'province': province,
'city': city,
'latitude': encrypted_latitude,
'longitude': encrypted_longitude,
'address_name': address_name
}
headers = {
'User-Agent': 'okhttp/3.8.1',
'Cookie': str(self.cookies),
'content-type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Host': 'api.xixunyun.com',
'Connection': 'keep-alive'
}
def signin_with_token(token, cookies):
time.sleep(5) # 等待5秒
url = f"https://api.xixunyun.com/signin_rsa?token={token}&from=app&version=4.9.7&school_id={school_id}"

try:
with httpx.Client() as client:
sign_response_data = client.post(url, headers=headers, data=payload).json()
sign_message = sign_response_data.get('message')
payload = {
'address': address,
'province': province,
'city': city,
'latitude': encrypted_latitude,
'longitude': encrypted_longitude,
'address_name': address_name
}
headers = {
'User-Agent': 'okhttp/3.8.1',
'Cookie': str(cookies),
'content-type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Host': 'api.xixunyun.com',
'Connection': 'keep-alive'
}

if sign_response_data.get('code') == 20000:
signin_result = "签到成功"
print(f"{signin_result},信息:{sign_message}")
try:
with httpx.Client() as client:
sign_response_data = client.post(url, headers=headers, data=payload).json()
sign_message = sign_response_data.get('message')

if pushPlus_set == 1:
send_push_notification(signin_result, sign_message)
elif pushPlus_set == 0:
print("推送未开启,请检查推送设置")
if sign_response_data.get('code') == 20000:
signin_result = "签到成功"
print(f"{signin_result},信息:{sign_message}")

else:
signin_result = "签到失败"
print(f"{signin_result},信息:{sign_message}")
if pushPlus_set != 0:
send_push_notification(signin_result, sign_message)
elif pushPlus_set == 0:
print("推送未开启,请检查推送设置")
except httpx.RequestError as e:
print(f"登录时发生请求错误:{e}")
if pushPlus_set == 1:
send_push_notification(signin_result, sign_message)
elif pushPlus_set == 0:
print("推送未开启,请检查推送设置")

else:
signin_result = "签到失败"
print(f"{signin_result},信息:{sign_message}")
if pushPlus_set != 0:
send_push_notification(signin_result, sign_message)
elif pushPlus_set == 0:
print("推送未开启,请检查推送设置")
return None, None
# 发送签到结果推送
except httpx.RequestError as e:
print(f"登录时发生请求错误:{e}")


if __name__ == "__main__":
manager = SigninManager()
manager.login_and_get_token()
if manager.token and manager.cookies:
manager.signin_with_token()
token, cookies = login_and_get_token()
if token and cookies:
signin_with_token(token, cookies)
87 changes: 42 additions & 45 deletions utils/data.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
import os
import urllib.parse
import os

class EnvironmentVariables:
def __init__(self):
self.variables = {
'school_id': None,
'account': None,
'password': None,
'longitude': None,
'latitude': None,
'address_name': None,
'address': None,
'province': None,
'city': None,
'pushPlus_set': None,
'pushPlus_token': None,
}

def read_environment_variables(self):
for key in self.variables.keys():
self.variables[key] = os.environ.get(key)

def check_variables_existence(self):
for key, value in self.variables.items():
if value is not None:
print(f"变量 {key} 存在")
else:
print(f"变量 {key} 不存在")

def quote_variables(self, to_quote):
for var in to_quote:
if self.variables[var] is not None:
self.variables[var] = urllib.parse.quote(self.variables[var])
print(f"已对 {var} 进行转义处理: {self.variables[var]}")
else:
print(f"无法对 {var} 进行转义处理,因为变量不存在")

# 创建环境变量管理对象
env_vars_manager = EnvironmentVariables()

# 读取环境变量
env_vars_manager.read_environment_variables()

# 检查并输出变量的存在状态
env_vars_manager.check_variables_existence()
# 读取 Secrets

school_id = os.environ.get('SCHOOL_ID')
account = os.environ.get('ACCOUNT')
password = os.environ.get('PASSWORD')
longitude = os.environ.get('LONGITUDE')
latitude = os.environ.get('LATITUDE')
address = os.environ.get('ADDRESS')
address_name = os.environ.get('ADDRESS_NAME')
province = os.environ.get('PROVINCE')
city = os.environ.get('CITY')
pushPlus_set = os.environ.get('PUSHPLUS_SET')
pushPlus_token = os.environ.get('PUSHPLUS_TOKEN')

# 检查并输出每个变量的存在状态
variables = {
'school_id': school_id,
'account': account,
'password': password,
'longitude': longitude,
'latitude': latitude,
'address_name': address_name,
'address': address,
'province': province,
'city': city,
'pushPlus_set': pushPlus_set,
'pushPlus_token': pushPlus_token,
}

for key, value in variables.items():
if value is not None:
print(f"变量 {key} 存在")
else:
print(f"变量 {key} 不存在")

# 需要转义的变量列表
to_quote = ['address_name', 'province', 'city', 'address']

# 对需要转义的变量进行转义处理
env_vars_manager.quote_variables(to_quote)
for var in to_quote:
if variables[var] is not None:
variables[var] = urllib.parse.quote(variables[var])
print(f"已对 {var} 进行转义处理: {variables[var]}")
else:
print(f"无法对 {var} 进行转义处理,因为变量不存在")

0 comments on commit cf23556

Please sign in to comment.