Conditions | 8 |
Total Lines | 60 |
Code Lines | 42 |
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 = "" |
||
58 | msg_all = "" |
||
59 | |||
60 | for i, check_item in enumerate(self.check_items, start=1): |
||
61 | username = check_item.get("username") |
||
62 | password = check_item.get("password") |
||
63 | |||
64 | # login |
||
65 | if not self._login(usr=username, pwd=password): |
||
66 | msg_all += f"account{i} login failed\n\n" |
||
67 | continue |
||
68 | |||
69 | # check domain status |
||
70 | self._s.headers.update({"referer": "https://my.freenom.com/clientarea.php"}) |
||
71 | r = self._s.get(DOMAIN_STATUS_URL) |
||
72 | |||
73 | # login status check |
||
74 | if not re.search(login_status_ptn, r.text): |
||
75 | msg_all += f"account{i} get login status failed\n\n" |
||
76 | continue |
||
77 | |||
78 | # page token |
||
79 | match = re.search(token_ptn, r.text) |
||
80 | if not match: |
||
81 | msg_all += f"account{i} get page token failed\n\n" |
||
82 | continue |
||
83 | token = match[1] |
||
84 | |||
85 | # domains |
||
86 | domains = re.findall(domain_info_ptn, r.text) |
||
87 | |||
88 | # renew domains |
||
89 | res = "" |
||
90 | for domain, days, renewal_id in domains: |
||
91 | if int(days) < 14: |
||
92 | self._s.headers.update( |
||
93 | { |
||
94 | "referer": f"https://my.freenom.com/domains.php?a=renewdomain&domain={renewal_id}", |
||
95 | "content-type": "application/x-www-form-urlencoded", |
||
96 | } |
||
97 | ) |
||
98 | r = self._s.post( |
||
99 | RENEW_DOMAIN_URL, |
||
100 | data={ |
||
101 | "token": token, |
||
102 | "renewalid": renewal_id, |
||
103 | f"renewalperiod[{renewal_id}]": "12M", |
||
104 | "paymentmethod": "credit", |
||
105 | }, |
||
106 | ) |
||
107 | res += ( |
||
108 | f"{domain} 续期成功\n" |
||
109 | if r.text.find("Order Confirmation") != -1 |
||
110 | else f"{domain} 续期失败" |
||
111 | ) |
||
112 | res += f"{domain} 还有 {days} 天续期\n" |
||
113 | msg = f"账号{i}\n{res}" |
||
114 | msg_all += msg + "\n" |
||
115 | return msg_all |
||
116 | |||
123 |