-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (30 loc) · 1.24 KB
/
utils.py
File metadata and controls
43 lines (30 loc) · 1.24 KB
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
import os
import json
from urllib.parse import urlparse
def read_domain_data(domain: str):
file_name = f'{domain}.json'
script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, '..', 'output', file_name)
if not os.path.exists(file_path):
return None
with open(file_path, 'r') as f:
json_data = json.load(f)
return json_data
def write_domain_data(domain: str, new_data: dict):
file_name = f'{domain}.json'
script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, '..', 'output', file_name)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as f:
json.dump(new_data, f, indent=4)
def read_lines_from_settings_txt(file_name):
script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, '..', 'settings', file_name)
with open(file_path, "r", encoding="utf-8") as f:
return [line.strip() for line in f if line.strip()]
def clean_domain(url: str) -> str:
if "://" not in url:
url = "http://" + url # helps urlparse work properly
domain = urlparse(url).netloc
domain = domain.removeprefix("www.")
return domain