Passed
Push — master ( 0ed109...1ec83f )
by Leon
02:19
created

ck_freenom.FreeNom.main()   C

Complexity

Conditions 8

Size

Total Lines 66
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 48
nop 1
dl 0
loc 66
rs 6.8351
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
"""
3
cron: 25 7 */10 * *
4
new Env('FreeNom');
5
"""
6
7
import re
8
9
import requests
10
11
from notify_mtr import send
12
from utils import get_data
13
14
# 登录地址
15
LOGIN_URL = "https://my.freenom.com/dologin.php"
16
17
# 域名状态地址
18
DOMAIN_STATUS_URL = "https://my.freenom.com/domains.php?a=renewals"
19
20
# 域名续期地址
21
RENEW_DOMAIN_URL = "https://my.freenom.com/domains.php?submitrenewals=true"
22
23
# token 正则
24
token_ptn = re.compile('name="token" value="(.*?)"', re.I)
25
26
# 域名信息正则
27
domain_info_ptn = re.compile(
28
    r'<tr><td>(.*?)</td><td>[^<]+</td><td>[^<]+<span class="[^<]+>(\d+?).Days</span>[^&]+&domain=(\d+?)">.*?</tr>',
29
    re.I,
30
)
31
32
# 登录状态正则
33
login_status_ptn = re.compile('<a href="logout.php">Logout</a>', re.I)
34
35
36
class FreeNom:
37
    def __init__(self, check_items: dict):
38
        self.check_items = check_items
39
        self._s = requests.Session()
40
        self._s.headers.update(
41
            {
42
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/79.0.3945.130 Safari/537.36"
43
            }
44
        )
45
46
    def _login(self, usr: str, pwd: str) -> bool:
47
        self._s.headers.update(
48
            {
49
                "content-type": "application/x-www-form-urlencoded",
50
                "referer": "https://my.freenom.com/clientarea.php",
51
            }
52
        )
53
        r = self._s.post(LOGIN_URL, data={"username": usr, "password": pwd})
54
        return r.status_code == 200
55
56
    def main(self) -> str:
57
        msg_all = ""
58
        i = 1
59
60
        for check_item in self.check_items:
61
            username = check_item.get("username")
62
            password = check_item.get("password")
63
64
            # login
65
            ok = self._login(usr=username, pwd=password)
66
            if not ok:
67
                msg_all += f"account{i} login failed\n\n"
68
                i += 1
69
                continue
70
71
            # check domain status
72
            self._s.headers.update({"referer": "https://my.freenom.com/clientarea.php"})
73
            r = self._s.get(DOMAIN_STATUS_URL)
74
75
            # login status check
76
            if not re.search(login_status_ptn, r.text):
77
                msg_all += f"account{i} get login status failed\n\n"
78
                i += 1
79
                continue
80
81
            # page token
82
            match = re.search(token_ptn, r.text)
83
            if not match:
84
                msg_all += f"account{i} get page token failed\n\n"
85
                i += 1
86
                continue
87
            token = match.group(1)
88
89
            # domains
90
            domains = re.findall(domain_info_ptn, r.text)
91
92
            # renew domains
93
            result = ""
94
            for domain, days, renewal_id in domains:
95
                days = int(days)
96
                if days < 14:
97
                    self._s.headers.update(
98
                        {
99
                            "referer": f"https://my.freenom.com/domains.php?a=renewdomain&domain={renewal_id}",
100
                            "content-type": "application/x-www-form-urlencoded",
101
                        }
102
                    )
103
                    r = self._s.post(
104
                        RENEW_DOMAIN_URL,
105
                        data={
106
                            "token": token,
107
                            "renewalid": renewal_id,
108
                            f"renewalperiod[{renewal_id}]": "12M",
109
                            "paymentmethod": "credit",
110
                        },
111
                    )
112
                    result += (
113
                        f"{domain} 续期成功\n"
114
                        if r.text.find("Order Confirmation") != -1
115
                        else f"{domain} 续期失败"
116
                    )
117
                result += f"{domain} 还有 {days} 天续期\n"
118
                msg = f"账号{i}\n" + result
119
            i += 1
120
            msg_all += msg + "\n"
121
        return msg_all
122
123
124
if __name__ == "__main__":
125
    data = get_data()
126
    _check_items = data.get("FREENOM", [])
127
    res = FreeNom(check_items=_check_items).main()
128
    send("FreeNom", res)
129