-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinternal_hdd.py
124 lines (101 loc) · 3.46 KB
/
internal_hdd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import re
from datetime import datetime
from pathlib import Path
from bs4.element import Tag
from jinja2 import Template
from utilits import get_index_soup, translator
class InternalHdd:
def __init__(self):
self.title = "傳統內接硬碟HDD"
self.tag = self.get_tag()
self.optgroups = self.get_optgroups()
self.options = self.get_options()
def get_tag(self):
soup = get_index_soup(is_coolpc_having_fucking_garbage_html=True)
title_tag = soup.find(text=self.title)
tag: Tag = title_tag.parent
return tag
def get_optgroups(self):
optgroups = self.tag.find_all("optgroup")
return optgroups
def get_options(self):
options = list()
for group in self.optgroups:
subtitle = group["label"]
for option_tag in group.find_all("option"):
if option_tag.has_attr("disabled"):
continue
a_option = InternalHddOption(self.title, subtitle, option_tag.text)
options.append(a_option)
return options
class InternalHddOption:
def __init__(self, title: str, subtitle: str, describe: str):
self.title = title
self.subtitle = subtitle
self.describe = describe
self.brand = self.get_brand()
self.size = self.get_size()
self.series = self.get_series()
self.memory = self.get_memory()
self.model = self.get_model()
self.rpm = self.get_rpm()
self.warranty = self.get_warranty()
self.price = self.get_price()
self.cp_value = self.get_cp_value()
def get_brand(self):
brand = self.describe.split()[0]
return brand
def get_size(self):
match = re.search(r"(\d+)(TB|G)", self.describe)
size = match.group(1)
size = int(size)
unit = match.group(2)
if "G" == unit:
size /= 1000
return size
def get_series(self):
match = re.search(r"(?<=【)[\w ]+(?=】)", self.describe)
if not match:
return None
series = match.group()
return series
def get_memory(self):
match = re.search(r"(?<=\()?\d+(?=MB?)", self.describe)
if not match:
return None
memory = match.group()
return memory
def get_model(self):
match = re.search(r"(?<=\()(\w|^/)+(?=\))", self.describe)
if not match:
return None
model = match.group()
return model
def get_rpm(self):
match = re.search(r"(?<=/)\d+(?=轉/)", self.describe)
if not match:
return None
rpm = match.group()
return int(rpm)
def get_warranty(self):
match = re.search(r"(?<=/)\w(?=年)", self.describe)
if not match:
return None
warranty = match.group()
return translator[warranty]
def get_price(self):
match = re.search(r"(?<=\$)(\d+)", self.describe)
price = match.group()
return int(price)
def get_cp_value(self):
return self.size * 1_000_000 / self.price
def save_to_html(options: list):
template_path = Path("../templates/internal-hdd.jinja2")
content = template_path.read_text()
template = Template(content)
html = template.render(items=options, update_time=datetime.now())
path = Path("../res/html/internal-hdd.html")
path.write_text(html)
if __name__ == '__main__':
x = InternalHdd()
save_to_html(x.options)