Skip to content

Commit

Permalink
⚡ Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreomeow committed Oct 16, 2022
1 parent cd10705 commit 5fcd6aa
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
30 changes: 20 additions & 10 deletions FN_extend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,31 @@
)


def main():
print("配置信息")
print(
[MAIL_TO, MAIL_PORT, MAIL_HOST, MAIL_ADDRESS, MAIL_PW, MAIL_USER, FN_ID, FN_PW]
def mask_data(data: str) -> str:
return "".join(
["*" if i > 2 and i < len(data) - 3 else data[i] for i in range(len(data))]
)
if not all(
[MAIL_TO, MAIL_PORT, MAIL_HOST, MAIL_ADDRESS, MAIL_PW, MAIL_USER, FN_ID, FN_PW]
):
raise CustomException("参数缺失")

to = [MAIL_TO]

def main():
envs = [
FN_ID,
FN_PW,
MAIL_USER,
MAIL_ADDRESS,
MAIL_PW,
MAIL_HOST,
MAIL_PORT,
MAIL_TO,
]
print("配置信息(脱敏后):")
print([mask_data(env) if isinstance(env, str) else env for env in envs])
if not all(envs):
raise CustomException("参数缺失")

body = {
"subject": "FreeNom 自动续期",
"to": to,
"to": [MAIL_TO],
}
try:
results = FreeNom().run()
Expand Down
19 changes: 8 additions & 11 deletions FNplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def qlnotify(desp):
cur_path = os.path.abspath(os.path.dirname(__file__))
if os.path.exists(f"{cur_path}/notify.py"):
try:
from notify import send
from notify import send # type: ignore
except Exception:
print("加载通知服务失败~")
else:
Expand All @@ -46,7 +46,7 @@ class FreeNom:
def __init__(self, username: str, password: str):
self._u = username
self._p = password
self._s = requests.Session()
self._s = requests.session()
self._s.headers.update(
{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/79.0.3945.130 Safari/537.36"
Expand All @@ -63,15 +63,13 @@ def _login(self) -> bool:
r = self._s.post(LOGIN_URL, data={"username": self._u, "password": self._p})
return r.status_code == 200

def renew(self):
global msg
def renew(self) -> str:
msg = ""
# login
ok = self._login()
if not ok:
if not self._login():
msg = "login failed"
print(msg)
return
return msg

# check domain status
self._s.headers.update({"referer": "https://my.freenom.com/clientarea.php"})
Expand All @@ -81,23 +79,22 @@ def renew(self):
if not re.search(login_status_ptn, r.text):
msg = "get login status failed"
print(msg)
return
return msg

# page token
match = re.search(token_ptn, r.text)
if not match:
msg = "get page token failed"
print(msg)
return
return msg
token = match[1]

# domains
domains = re.findall(domain_info_ptn, r.text)

# renew domains
for domain, days, renewal_id in domains:
days = int(days)
if days < 14:
if int(days) < 14:
self._s.headers.update(
{
"referer": f"https://my.freenom.com/domains.php?a=renewdomain&domain={renewal_id}",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TASK -> 添加单个任务 -> 修改名称、时间、任务 -> JSMANAGE -> stor

名称: Freenom 续期

时间:cron定时 `25 7 */10 * *`
时间: cron 定时 `25 7 */10 * *`

任务:

Expand All @@ -104,7 +104,7 @@ TASK -> 添加单个任务 -> 修改名称、时间、任务 -> JSMANAGE -> stor

名称: Freenom 续期

时间:cron定时 `25 7 */10 * *`
时间: cron 定时 `25 7 */10 * *`

任务:

Expand Down
4 changes: 2 additions & 2 deletions utils/exception.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CustomException(Exception):
def __init__(self, message):
def __init__(self, message: str):
super().__init__(self)
self.message = message
self.message: str = message

def __str__(self):
return self.message
4 changes: 2 additions & 2 deletions utils/freenom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from utils.exception import CustomException


class FreeNom(object):
class FreeNom:
"""FreeNom api 请求"""

# 登录
Expand Down Expand Up @@ -96,7 +96,7 @@ def get_domains(self) -> str:
print("FreeNom check domain status failed")
raise CustomException("调用获取域名信息接口失败")

def renew_domain(self, token, renewalid) -> str:
def renew_domain(self, token, renewalid: str) -> str:
headers = {
**self.headers,
"Referer": "https://my.freenom.com/domains.php?a=renewdomain&domain="
Expand Down
10 changes: 5 additions & 5 deletions utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, FileSystemLoader, Template

from utils import settings


class EmailPoster(object):
class EmailPoster:
"""邮件发送基础类"""

@staticmethod
def get_template():
def get_template() -> Template:
loader = FileSystemLoader("templates")
env = Environment(autoescape=True, loader=loader)
return env.get_template("default.html")
Expand Down Expand Up @@ -45,5 +45,5 @@ def _send(content: str, subject: str, mail_from: str, mail_to: list):
smtp.login(settings.MAIL_USER, settings.MAIL_PW)
smtp.sendmail(settings.MAIL_ADDRESS, mail_to, msg_root.as_string())
smtp.quit()
except Exception as e:
print(traceback.format_exc(e))
except Exception:
print(traceback.format_exc())
14 changes: 7 additions & 7 deletions utils/settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os

# qq mail
MAIL_ADDRESS = os.getenv("MAIL_ADDRESS", "")
MAIL_HOST = os.getenv("SMTP_HOST", "smtp.qq.com")
MAIL_PW = os.getenv("MAIL_PW", "")
MAIL_ADDRESS = str(os.getenv("MAIL_ADDRESS", ""))
MAIL_HOST = str(os.getenv("SMTP_HOST", "smtp.qq.com"))
MAIL_PW = str(os.getenv("MAIL_PW", ""))
MAIL_PORT = int(os.getenv("SMTP_PORT", 465))
MAIL_TO = os.getenv("MAIL_TO", "")
MAIL_USER = os.getenv("MAIL_USER", "")
MAIL_TO = str(os.getenv("MAIL_TO", ""))
MAIL_USER = str(os.getenv("MAIL_USER", ""))


# free nom
FN_ID = os.getenv("FN_ID", "")
FN_PW = os.getenv("FN_PW", "")
FN_ID = str(os.getenv("FN_ID", ""))
FN_PW = str(os.getenv("FN_PW", ""))

0 comments on commit 5fcd6aa

Please sign in to comment.