Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
Added:support json in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjiaH committed Apr 23, 2022
1 parent 06f5582 commit 634e5c6
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 163 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ pip install -r requirements.txt
- 打开[Server酱](https://benjiah.gitee.io/redirect/serversauce)
- 申请一个`sendkey`,并记录下来。

### 4.2.填写[`config/config.ini`](config/config_example.ini)
### 4.2.填写[`config/config.json`](config/config_example.json)

- 重命名`config_example.ini`文件为`config.ini`
- 参照[`config.ini`文件](config/config_example.ini)内说明填写其余内容。
- 重命名`config_example.json`文件为`config.json`
- 参照[`config.json`文件](config/config_example.json)内说明填写其余内容。

### 4.3.填写[`config/account.csv`](config/account_example.csv)(可选)

Expand Down Expand Up @@ -125,15 +125,12 @@ chmod 777 scripts/run.sh
├─config
│ account.csv <---多账户管理文件
│ config.ini <---配置文件
│ config.json <---配置文件
│ email_tmpl.html <---Email模板文件
├─log
│ log.log <---日志文件
├─res
│ email_tmpl.html <---Email模板文件
│ error.json <---错误码及错误信息文件
└─scripts
run.bat <---Windows下运行文件
run.sh <---GNU/Linux下运行文件
Expand Down
5 changes: 3 additions & 2 deletions common/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import csv

from common.logger import logger
from common.config import global_config as gc


class Account:
Expand All @@ -18,7 +19,7 @@ def __init__(self, csv_file="../config/account.csv"):

@logger.catch
def _read_csv(self):
self._csv_file = csv.reader(open(self._path, encoding='UTF-8-sig'))
self._csv_file = csv.reader(open(self._path, encoding='utf-8-sig'))
for row in self._csv_file:
if row == [] or "#" in row[0]:
logger.debug(f"Del:{row}")
Expand Down Expand Up @@ -60,4 +61,4 @@ def row(self):
return len(self._raw)


global_account = Account("../config/account.csv")
global_account = Account(gc.config['config']['path']['account_file'])
28 changes: 11 additions & 17 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import os
import configparser
import json
from common.logger import logger


class Config:
def __init__(self, config_file=r"../config/config.ini"):
def __init__(self, config_file=r"../config/config.json"):
os.chdir(os.path.dirname(__file__))
self._path = os.path.abspath(config_file)
if not os.path.exists(self._path):
logger.error(f"No such file:{self._path}")
raise FileNotFoundError(f"No such file:{self._path}")
self._config = configparser.ConfigParser()
self._config.read(self._path, encoding='utf-8-sig')
self._configRaw = configparser.RawConfigParser()
self._configRaw.read(self._path, encoding='utf-8-sig')
self.config = {}
self._json_read()

@logger.catch
def _json_read(self):
with open(self._path, 'r', encoding='utf-8') as f:
self.config = json.load(f)
logger.debug(f"Loaded [{self._path}]")

@logger.catch
def refresh(self):
self._config.read(self._path, encoding='utf-8-sig')
self._configRaw.read(self._path, encoding='utf-8-sig')
self._json_read()
logger.debug(f"Refreshed [{self._path}]")

@logger.catch
def get(self, section, name):
return self._config.get(section, name)

@logger.catch
def getRaw(self, section, name):
return self._configRaw.get(section, name)


global_config = Config(r"../config/config.ini")
global_config = Config(r"../config/config.json")
6 changes: 3 additions & 3 deletions common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Logger:
def __init__(self, log_file: str):
os.chdir(os.path.dirname(__file__))
self._config_path = os.path.abspath(r"../config/config.ini")
self._config_path = os.path.abspath(r"../config/config.json")
self._log_fmt = "{time:YYYY-MM-DD HH:mm:ss.SSS} [<level>{level:<5}</level>] {file}.{line}: {message}"
self._debug_fmt = "{time:YYYY-MM-DD HH:mm:ss.SSS} [<level>{level:<5}</level>] {name}:{function}:{line}: {message}"
self._logger_conf(log_file)
Expand All @@ -28,13 +28,13 @@ def _logger_conf(self, log_file):
def _get_level(self):
level_raw = ""
try:
with open(self._config_path, "r", encoding="UTF-8") as f:
with open(self._config_path, "r", encoding="utf-8") as f:
lines = f.readlines()
for i in lines:
if "level" in i and ";" != i[0]:
level_raw = i
except:
level_raw = "level = INFO"
level_raw = '"level": "DEBUG",'
if "DEBUG" in level_raw:
self._level = "DEBUG"
else:
Expand Down
30 changes: 16 additions & 14 deletions common/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from urllib import parse
from datetime import datetime
from email.mime.text import MIMEText
from common.config import global_config
from common.config import global_config as gc
from common.logger import logger


class Email:
@logger.catch
def __init__(self, mail_user, mail_host, mail_pwd):
if global_config.getRaw('config', 'email_enable') == "off":
if gc.config['setting']['push']['email']['enable'] == "off":
logger.debug("Email is disabled")
return
logger.debug("Email is enabled")
Expand All @@ -25,17 +25,18 @@ def __init__(self, mail_user, mail_host, mail_pwd):
self._is_login = False
self.smtp = 0
self._mail_payload = ""
self._email_tmpl_path = gc.config['config']['path']['email_tmpl']

@logger.catch
def _load_tmpl(self):
os.chdir(os.path.dirname(__file__))
with open(r"../res/email_tmpl.html", "r", encoding="UTF-8") as f:
with open(self._email_tmpl_path, "r", encoding="utf-8") as f:
self._mail_payload = f.read()
logger.debug(f'Loaded [{os.path.abspath(r"../res/email_tmpl.html")}]')

@logger.catch
def login(self):
if global_config.getRaw('config', 'email_enable') == "off":
if gc.config['setting']['push']['email']['enable'] == "off":
return
self._load_tmpl()
try:
Expand Down Expand Up @@ -75,18 +76,19 @@ def send(self, uid, title, msg, receiver: list):
class Push:
@logger.catch
def __init__(self):
self._global_wechat = global_config.getRaw('config', 'wechat_enable')
self._global_email = global_config.getRaw('config', 'email_enable')
self._bot_email_user = global_config.getRaw('bot_email', 'email_user')
self._bot_email_host = global_config.getRaw('bot_email', 'email_host')
self._bot_email_pwd = global_config.getRaw('bot_email', 'email_pwd')
self._global_wechat = gc.config['setting']['push']['wechat']['enable']
self._global_email = gc.config['setting']['push']['email']['enable']
self._bot_email_user = gc.config['bot_email']['email_user']
self._bot_email_host = gc.config['bot_email']['email_host']
self._bot_email_pwd = gc.config['bot_email']['email_pwd']
self.bot_email = Email(self._bot_email_user, self._bot_email_host, self._bot_email_pwd)
self._errno_msg_path = r"../res/error.json"
self._errno_msg = self._load_errno()
self._errno_msg_path = gc.config['config']['path']['errno_msg']
# self._errno_msg = self._load_errno()
self._errno_msg = gc.config['config']['errmsg']

@logger.catch
def _load_errno(self):
with open(self._errno_msg_path, "r", encoding="UTF-8") as f:
with open(self._errno_msg_path, "r", encoding="utf-8") as f:
self._raw = f.read()
logger.debug(f'Loaded [{os.path.abspath(self._errno_msg_path)}]')
return json.loads(self._raw)
Expand All @@ -95,7 +97,7 @@ def _load_errno(self):
@logger.catch
def sct_wechat(uid, title, message, sendkey):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = f'https://sctapi.ftqq.com/{sendkey}.send'
url = f"{gc.config['config']['url']['sct']}/{sendkey}.send"
ps = ""
msg = f'{" " * 10}{title}\n\n{uid}:\n{" " * 4}{message}\n{ps}\n\n{now}'
payload = {
Expand Down Expand Up @@ -162,7 +164,7 @@ def push(self, result, uid, wechat_push, email_push, wechat_type, api, userid, s
title = "[ERROR]"
message = "ERROR!"
if errno != 0:
errmsg = [i["msg"] for i in self._errno_msg["content"] if errno == i["errno"]][0]
errmsg = [i["msg"] for i in self._errno_msg if errno == i["errno"]][0]
message = f'{message}[错误信息:"{errmsg}"]'
logger.debug(f"Title:{title}#Message:{message}#Error code:{errno}")
if self._global_wechat != "off":
Expand Down
28 changes: 17 additions & 11 deletions common/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bs4 import BeautifulSoup
from common import security
from common.logger import logger
from common.config import global_config as gc


class Report:
Expand All @@ -18,6 +19,8 @@ def __init__(self):
self._navigation_url = 0
self._date = ""
self._captcha_code = ""
self._success = gc.config['config']['response']['success']
self._existed = gc.config['config']['response']['existed']

@logger.catch
def update_date(self):
Expand All @@ -39,7 +42,7 @@ def _get_captcha_code(self):
logger.error(f"No available hosts.")
self._set_error(6, 1)
return
url = f"{self._host}/weblogin.asp"
url = f"{self._host}/{gc.config['config']['url']['login']}"
res = self._session.get(url=url, headers=self._headers)
logger.debug(f"URL:{url}. Status code:{res.status_code}")
res.encoding = "utf-8"
Expand All @@ -59,7 +62,7 @@ def _login(self, uid, password):
if self._error == 1:
logger.debug(f"The error flag: {self._error}. Exit the function.")
return
url = f"{self._host}/weblogin.asp"
url = f"{self._host}/{gc.config['config']['url']['login']}"
payload = {
"username": uid,
"userpwd": password,
Expand Down Expand Up @@ -88,7 +91,7 @@ def _get_navigation_url(self, target):
if self._error == 1:
logger.debug(f"The error flag: {self._error}. Exit the function.")
return
url = f"{self._host}/left.asp"
url = f"{self._host}/{gc.config['config']['url']['left']}"
res = self._session.get(url=url, headers=self._headers)
logger.debug(f"URL:{url}. Status code:{res.status_code}")
if res.status_code != 200:
Expand All @@ -110,7 +113,7 @@ def _report_default_method(self):
return ""
logger.info("Try to report in the default method.")
param = parse.parse_qs(parse.urlparse(str(self._navigation_url)).query)
url = f"{self._host}/projecthealth_addx.asp"
url = f"{self._host}/{gc.config['config']['url']['report_default']}"
payload = {
"id": param["id"][0],
"id2": self._date,
Expand All @@ -133,7 +136,7 @@ def _report(self, location):
logger.info("Try to report in the alternate method.")
[province, city, area] = location
param = parse.parse_qs(parse.urlparse(str(self._navigation_url)).query)
url = f"{self._host}/projecthealth_add.asp"
url = f"{self._host}/{gc.config['config']['url']['report']}"
payload = {
"id": param["id"][0],
"id2": self._date,
Expand Down Expand Up @@ -189,27 +192,30 @@ def main(self, uid, password):
self._error = 0
self._errno = 0
self._session = requests.Session()
self._host = f"https://xsswzx.cdu.edu.cn/{security.get_random_host()}/com_user"
_host_0 = gc.config['config']['url']['host_head']
_host_1 = security.get_random_host()
_host_2 = gc.config['config']['url']['host_foot']
self._host = f"{_host_0}/{_host_1}/{_host_2}"
self._headers = {
"User-Agent": security.get_random_useragent()
}
self._get_captcha_code()
self._login(uid, password)
self._get_navigation_url("健康日报登记")
self._get_navigation_url(gc.config['config']['url']['navigation'])
ret = self._report_default_method()
if "已存在" in ret:
if self._existed in ret:
logger.info(f"The report is already existed. ID:{uid}")
return 0, self._errno
elif "提交成功" in ret:
elif self._success in ret:
logger.info(f"Successful to report. ID:{uid}")
return 1, self._errno
else:
logger.error("Failed to report in the default method.")
ret = self._report(self._fetch_location())
if "已存在" in ret:
if self._existed in ret:
logger.info(f"The report is already existed. ID:{uid}")
return 0, self._errno
elif "提交成功" in ret:
elif self._success in ret:
logger.info(f"Successful to report. ID:{uid}")
return 1, self._errno
else:
Expand Down
22 changes: 7 additions & 15 deletions common/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@
import requests

from common.logger import logger
from common.config import global_config as gc
from fake_useragent import UserAgent

HOSTS = [
"ispstu", # 富强
"ispstu1-1", # 民主
"ispstu1-2", # 文明
"ispstu2", # 和谐
"ispstu2-1", # 自由
"ispstu2-2", # 平等
"ispstu3", # 公正
"ispstu3-1", # 法治
"ispstu3-2", # 爱国
"ispstu4", # 敬业
"ispstu4-1", # 诚信
"ispstu4-3" # 友善
]
HOSTS = list(gc.config['config']['all_hosts'].keys())
ua = UserAgent(verify_ssl=False)
available_host = []


@logger.catch
def check_host_status(host):
url = f"https://xsswzx.cdu.edu.cn/{host}/com_user/weblogin.asp"
url_0 = gc.config['config']['url']['host_head']
url_1 = host
url_2 = gc.config['config']['url']['host_foot']
url_3 = gc.config['config']['url']['login']
url = f"{url_0}/{url_1}/{url_2}/{url_3}"
try:
res = requests.get(url=url, timeout=10)
logger.debug(f"URL:{url}. Status code:{res.status_code}")
Expand Down
22 changes: 11 additions & 11 deletions common/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import sleep, time
from common import security
from common.logger import logger
from common.config import global_config
from common.config import global_config as gc
from common.account import global_account
from common.report import Report
from common.push import global_push
Expand All @@ -12,10 +12,10 @@ class ReportService:
@logger.catch
def __init__(self):
self._str_now_time = "0.1"
self._wechat_push = global_config.getRaw('config', 'wechat_enable')
self._email_push = global_config.getRaw('config', 'email_enable')
self._wechat_type = global_config.getRaw('config', 'wechat_type')
self._api = global_config.getRaw('config', 'api')
self._wechat_push = gc.config['setting']['push']['wechat']['enable']
self._wechat_type = gc.config['setting']['push']['wechat']['type']
self._api = gc.config['setting']['push']['wechat']['api']
self._email_push = gc.config['setting']['push']['email']['enable']
self._account_cnt = global_account.row
self._report = Report()

Expand Down Expand Up @@ -52,21 +52,21 @@ def _gen(self):

@logger.catch
def start(self):
if global_config.getRaw('config', 'timer_enable') == "off":
if gc.config['setting']['timer']['enable'] == "off":
logger.info("Timer is disabled.")
logger.info("Start to report.")
self._gen()
else:
logger.info("Timer is enabled.")
while True:
global_config.refresh()
str_set_time = global_config.getRaw('config', 'set_time')
gc.refresh()
str_set_time = str(gc.config['setting']['timer']['set_time'])
str_now_time = self._get_now_time()
logger.info(f"Now time:{str_now_time}. Set time:{str_set_time}.")
while True:
global_config.refresh()
if str_set_time != global_config.getRaw('config', 'set_time'):
str_set_time = global_config.getRaw('config', 'set_time')
gc.refresh()
if str_set_time != str(gc.config['setting']['timer']['set_time']):
str_set_time = str(gc.config['setting']['timer']['set_time'])
logger.info(f"New set time:{str_set_time}.")
str_now_time = self._get_now_time()
if str_now_time != str_set_time:
Expand Down
Loading

0 comments on commit 634e5c6

Please sign in to comment.