Conditions | 8 |
Total Lines | 66 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
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 | |||
129 |