Skip to content

Commit

Permalink
🐎 Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreomeow committed Oct 14, 2022
1 parent 67948d9 commit 9e259c0
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 122 deletions.
33 changes: 15 additions & 18 deletions api_leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
new Env('LeetCode 每日一题');
"""

import json

import requests

from notify_mtr import send
Expand All @@ -19,7 +17,7 @@ def main():
base_url = "https://leetcode-cn.com"

# 获取今日每日一题的题名(英文)
response = requests.post(
res = requests.post(
f"{base_url}/graphql",
json={
"operationName": "questionOfToday",
Expand All @@ -29,22 +27,21 @@ def main():
"lastSubmission { id __typename } "
"date userStatus __typename } }",
},
)
leetcode_title = (
json.loads(response.text)
.get("data")
).json()
title_slug = (
res.get("data")
.get("todayRecord")[0]
.get("question")
.get("questionTitleSlug")
)

# 获取今日每日一题的所有信息
url = f"{base_url}/problems/{leetcode_title}"
response = requests.post(
url = f"{base_url}/problems/{title_slug}"
res = requests.post(
f"{base_url}/graphql",
json={
"operationName": "questionData",
"variables": {"titleSlug": leetcode_title},
"variables": {"titleSlug": title_slug},
"query": "query questionData($titleSlug: String!) "
"{ question(titleSlug: $titleSlug) { questionId questionFrontendId "
"boundTopicId title titleSlug content translatedTitle translatedContent"
Expand All @@ -61,15 +58,15 @@ def main():
"isSubscribed isDailyQuestion dailyRecordStatus "
"editorType ugcQuestionId style __typename } }",
},
)
).json()

# 转化成 json 格式
json_text = json.loads(response.text).get("data").get("question")
# 题目题号
num = json_text.get("questionFrontendId")
# 题名(中文)
leetcode_title = json_text.get("translatedTitle")
return f'<a href="{url}">{num}. {leetcode_title}</a>'
# 题目
question = res.get("data").get("question")
# 题号
num = question.get("questionFrontendId")
# 题名(中文)
zh_title = question.get("translatedTitle")
return f'<a href="{url}">{num}. {zh_title}</a>'


if __name__ == "__main__":
Expand Down
8 changes: 2 additions & 6 deletions ck_bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,7 @@ def main(self) -> str:
)

# GET 导航栏用户信息 1/2
uname, uid, is_login, coin, vip_type, current_exp = self.get_nav(
session=session
)
uname, uid, is_login, coin, _, _ = self.get_nav(session=session)
if not is_login:
msg_all += "非登录状态,请检查\n\n"
continue
Expand Down Expand Up @@ -475,9 +473,7 @@ def main(self) -> str:
live_stats = self.get_live_status(session=session)

# GET 导航栏用户信息 2/2
uname, uid, is_login, new_coin, vip_type, new_current_exp = self.get_nav(
session=session
)
uname, uid, is_login, _, _, new_current_exp = self.get_nav(session=session)

# 今日获得经验
today_exp = sum(
Expand Down
210 changes: 115 additions & 95 deletions ck_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Site:
def __init__(self, check_items):
self.check_items = check_items
self.error_tip = "cookie 已过期或网站类型不对"
self.url = ""

@staticmethod
def generate_headers(url):
Expand All @@ -49,106 +48,127 @@ def cookie_parse(cookie_str):
def signin(self, session, url):
# hdarea 签到
if url == "https://www.hdarea.co":
attendance_url = f"{url}/sign_in.php"
data = {"action": "sign_in"}
with session.post(attendance_url, data) as response:
r = re.compile(r"获得了\d+魔力值")
r1 = re.compile(r"重复")
log(response.text)
if r.search(response.text):
tip = "签到成功"
elif r1.search(response.text):
tip = "重复签到"
else:
tip = self.url
print(f"{url} {tip}")
log(f"{url} {tip}")
self.hdarea(session, url)
elif url == "https://pterclub.com":
attendance_url = f"{url}/attendance-ajax.php"
with session.get(attendance_url) as response:
try:
msg = json.loads(
response.text.encode("utf-8").decode("unicode-escape")
).get("message")
except JSONDecodeError:
msg = response.text
if "连续签到" in msg:
pattern = re.compile(r"</?.>")
tip = f"签到成功, {re.sub(pattern, '', msg)}"
elif "重复刷新" in msg:
tip = "重复签到"
else:
tip = self.url
print(f"{url} {tip}")
log(f"{url} {tip}")
self.pterclub(session, url)
elif url == "https://www.haidan.video":
attendance_url = f"{url}/signin.php"
with session.get(attendance_url) as response:
r = re.compile(r"已经打卡")
r1 = re.compile(r"退出")
if r.search(response.text):
tip = "签到成功"
elif r1.search(response.text):
tip = "重复签到"
else:
tip = "cookie 已过期或网站类型不对!"
print(f"{url} {tip}")
log(f"{url} {tip}")
self.haidan(session, url)
elif url == "https://pt.btschool.club":
attendance_url = f"{url}/index.php?action=addbonus"
with session.get(attendance_url) as response:
r = re.compile(r"今天签到您获得\d+点魔力值")
r1 = re.compile(r"退出")
location = r.search(response.text)
if location:
tip = location.group()
elif r1.search(response.text):
tip = "重复签到"
else:
tip = "cookie已过期"
print(f"{url} {tip}")
log(f"{url} {tip}")
self.btschool(session, url)
elif url == "https://lemonhd.org":
attendance_url = f"{url}/attendance.php"
with session.get(attendance_url) as response:
r = re.compile(r"已签到")
r1 = re.compile(r"请勿重复刷新")
# log(response.text)
if r.search(response.text):
tip = "签到成功"
elif r1.search(response.text):
tip = "重复签到"
else:
tip = self.url
print(f"{url} {tip}")
log(f"{url} {tip}")
self.lemonhd(session, url)
elif url in ["https://hdtime.org", "https://www.pttime.org"]:
attendance_url = f"{url}/attendance.php"
with session.get(attendance_url) as response:
r = re.compile(r"签到成功")
r1 = re.compile(r"请勿重复刷新")
if r.search(response.text):
tip = "签到成功"
elif r1.search(response.text):
tip = "重复签到"
else:
tip = "cookie已过期"
print(f"{url} {tip}")
log(f"{url} {tip}")
self.hdtime(session, url)
else:
attendance_url = f"{url}/attendance.php"
with session.get(attendance_url) as response:
r = re.compile(r"请勿重复刷新")
r1 = re.compile(r"签到已得\s*\d+")
location = r1.search(response.text).span()
if r.search(response.text):
tip = "重复签到"
elif location:
tip = response.text[location[0], location[1]]
else:
tip = self.url
print(f"{url} {tip}")
log(f"{url} {tip}")
self.common(session, url)

@staticmethod
def signin_base(session, url, data=None, **kwargs):
attendance_url = kwargs.get("attendance_url")
success = kwargs.get("success", [])
repeat = kwargs.get("repeat", [])
failure = kwargs.get("failure", "")
with session.get(attendance_url, data) as response:
r1 = re.compile(success[0])
r2 = re.compile(repeat[0])
location = r1.search(response.text)
if location:
tip = success[1]
elif r2.search(response.text):
tip = repeat[1]
else:
tip = failure
print(f"{url} {tip}")
if tip == "btschool":
tip = location.group()
log(f"{url} {tip}")

def hdarea(self, session, url):
self.signin_base(
session,
url,
attendance_url=f"{url}/sign_in.php",
data={"action": "sign_in"},
success=[r"获得了\d+魔力值", "签到成功"],
repeat=[r"重复", "重复签到"],
failure="签到失败",
)

@staticmethod
def pterclub(session, url):
attendance_url = f"{url}/attendance-ajax.php"
with session.get(attendance_url) as response:
try:
msg = json.loads(
response.text.encode("utf-8").decode("unicode-escape")
).get("message")
except JSONDecodeError:
msg = response.text
if "连续签到" in msg:
pattern = re.compile(r"</?.>")
tip = f"签到成功, {re.sub(pattern, '', msg)}"
elif "重复刷新" in msg:
tip = "重复签到"
else:
tip = "签到失败"
print(f"{url} {tip}")
log(f"{url} {tip}")

def haidan(self, session, url):
self.signin_base(
session,
url,
attendance_url=f"{url}/signin.php",
success=[r"已经打卡", "签到成功"],
repeat=[r"退出", "重复签到"],
failure="cookie 已过期或网站类型不对!",
)

def btschool(self, session, url):
self.signin_base(
session,
url,
attendance_url=f"{url}/index.php?action=addbonus",
success=[r"今天签到您获得\d+点魔力值", "btschool"],
repeat=[r"退出", "重复签到"],
failure="cookie 已过期",
)

def lemonhd(self, session, url):
self.signin_base(
session,
url,
attendance_url=f"{url}/attendance.php",
success=[r"已签到", "签到成功"],
repeat=[r"请勿重复刷新", "重复签到"],
failure="签到失败",
)

def hdtime(self, session, url):
self.signin_base(
session,
url,
attendance_url=f"{url}/attendance.php",
success=[r"签到成功", "签到成功"],
repeat=[r"请勿重复刷新", "重复签到"],
failure="cookie 已过期",
)

@staticmethod
def common(session, url):
attendance_url = f"{url}/attendance.php"
with session.get(attendance_url) as response:
r = re.compile(r"请勿重复刷新")
r1 = re.compile(r"签到已得\s*\d+")
location = r1.search(response.text).span()
if r.search(response.text):
tip = "重复签到"
elif location:
tip = response.text[location[0], location[1]]
else:
tip = "cookie 已过期"
print(f"{url} {tip}")
log(f"{url} {tip}")

@staticmethod
def signin_discuz_dsu(session, url):
Expand Down
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from utils_ver import print_ver

# 缓存全局的环境
DATA = {}
DATA: dict = {}


def get_data() -> dict:
Expand Down
2 changes: 1 addition & 1 deletion utils_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

from peewee import *
from peewee import CharField, DateField, Model, SmallIntegerField, SqliteDatabase

from utils_env import get_file_path

Expand Down
2 changes: 1 addition & 1 deletion utils_ver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import requests

__version__ = "20221014-4-011"
__version__ = "20221014-4-021"
ONLINE_VERSION = ""
ver_re = re.compile("__version__ = .(\\d+-\\d+-...).")

Expand Down

0 comments on commit 9e259c0

Please sign in to comment.