api_hax.Hax.get_server_info()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 19
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nop 2
dl 0
loc 19
rs 9.5
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
cron: 21 */6 * * *
4
new Env('Hax');
5
"""
6
7
import re
8
9
import requests
10
from bs4 import BeautifulSoup
11
12
from notify_mtr import send
13
from utils import get_data
14
15
16
class Hax:
17
    @staticmethod
18
    def check(url):
19
        headers = {
20
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
21
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
22
            "Content-type": "application/json",
23
        }
24
        return requests.get(url, headers=headers).text
25
26
    def get_server_info(self, url):
27
        html_text = self.check(url)
28
        soup = BeautifulSoup(html_text, "html.parser")
29
        zone_list = [x.text for x in soup("h5", class_="card-title mb-4")]
30
        sum_list = [x.text for x in soup("h1", class_="card-text")]
31
        vps_list = []
32
        vps_dict = {}
33
        for k, v in zip(zone_list, sum_list):
34
            zone = k.split("-", 1)[0].lstrip("./")
35
            sum_ = (
36
                k.split("-", 1)[1] + "(" + v.rstrip(" VPS") + "♝)"
37
                if len(k.split("-", 1)) > 1
38
                else v
39
            )
40
            vps_list.append((zone, sum_))
41
        for k_v in vps_list:
42
            k, v = k_v
43
            vps_dict.setdefault(k, []).append(v)
44
        return "".join(f">>{k}-" + ", ".join(v) + "\n" for k, v in vps_dict.items())
45
46
    def get_data_center(self, url, vir=False):
47
        html_text = self.check(url)
48
        soup = BeautifulSoup(html_text, "html.parser")
49
        ctr_list = [x.text for x in soup("option", value=re.compile(r"^[A-Z]{2,}-"))]
50
        ctr_str = "\n".join(ctr_list)
51
        if vir:
52
            ctr_list = [
53
                (c.split(" (")[1].rstrip(")"), c.split(" (")[0]) for c in ctr_list
54
            ]
55
            ctr_dict = {}
56
            for k_v in ctr_list:
57
                k, v = k_v
58
                ctr_dict.setdefault(k, []).append(v)
59
            ctr_str = "".join(
60
                f"★{k}★ " + ", ".join(v) + "\n" for k, v in ctr_dict.items()
61
            )
62
        return ctr_str
63
64
    def main(self):
65
        hax_str = self.get_server_info("https://hax.co.id/data-center")
66
        hax_stat = f"[🛰Hax Stats / Hax 开通数据]\n{hax_str}\n"
67
        vir_str = self.get_data_center("https://hax.co.id/create-vps", True)
68
        woiden_str = self.get_data_center("https://woiden.id/create-vps")
69
        data_center = (
70
            f"[🚩Available Centers / 可开通区域]\n"
71
            f'---------- <a href="https://hax.co.id/create-vps">Hax</a> ----------\n'
72
            f"{vir_str}"
73
            f'---------- <a href="https://woiden.id/create-vps">Woiden</a> ----------\n'
74
            f"{woiden_str}\n"
75
        )
76
        return hax_stat + data_center
77
78
79
if __name__ == "__main__":
80
    _data = get_data()
81
    hax = _data.get("HAX")
82
    if hax:
83
        result = Hax().main()
84
        send("Hax", result)
85