Passed
Push — master ( 500da7...b00704 )
by Leon
01:59
created

ck_haxclock.HaxClock.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
cron: 27 8,22 * * *
4
new Env('Hax 续期提醒');
5
"""
6
7
import datetime
8
import re
9
10
import requests
11
12
from notify_mtr import send
13
from utils import get_data
14
15
16
class HaxClock:
17
    def __init__(self, check_items):
18
        self.check_items = check_items
19
20
    def check_vps_info(self, cookie):
21
        url = "https://hax.co.id/vps-info"
22
        headers = {
23
            "cookie": cookie,
24
            "referer": "https://hax.co.id/vps-info/",
25
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43",
26
        }
27
        datas = requests.get(url=url, headers=headers).text
28
        return datas
29
30
    def get_valid_until(self, cookie):
31
        html_text = self.check_vps_info(cookie)
32
        hostname = re.search("[0-9]+_hax", html_text).group(0)
33
        valid_until = re.search(
34
            "(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,\\ ((1[6-9]|[2-9]\\d)\\d{2}))",
35
            html_text,
36
        ).group(0)
37
        return hostname, valid_until
38
39
    def main(self):
40
        msg_all = ""
41
        for check_item in self.check_items:
42
            cookie = check_item.get("cookie")
43
            hostname, valid_until = self.get_valid_until(cookie)
44
            d1 = datetime.datetime.today()
45
            today = d1.strftime("%B %d, %Y")
46
            d2 = datetime.datetime.strptime(valid_until, "%B %d, %Y")
47
            tip = (
48
                "Please wait until at least three days before the expiry date to renew. / 请等到至少过期前三天再去续期。"
49
                if (d2 - d1).days > 3
50
                else "AVAILABLE for RENEWAL / 可以续期了"
51
            )
52
            msg = f"Hostname / 主机名:{hostname}\nToday / 本地日期:{today}\nValid until / 有效期至:{valid_until}\n{tip}"
53
            msg_all += msg + "\n\n"
54
        return msg_all
55
56
57
if __name__ == "__main__":
58
    data = get_data()
59
    _check_items = data.get("HAXCLOCK", [])
60
    res = HaxClock(check_items=_check_items).main()
61
    send("Hax 续期提醒", res)
62