Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
taux1c
GitHub Repository: taux1c/onlyfans-scraper
Path: blob/main/onlyfans_scraper/utils/config.py
961 views
1
r"""
2
_ __
3
___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __
4
/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|
5
| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |
6
\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|
7
|___/ |_|
8
"""
9
10
import json
11
import pathlib
12
13
from .prompts import config_prompt
14
from ..constants import configPath, configFile, mainProfile
15
16
17
def read_config():
18
p = pathlib.Path.home() / configPath
19
if not p.is_dir():
20
p.mkdir(parents=True, exist_ok=True)
21
22
config = {}
23
while True:
24
try:
25
with open(p / configFile, 'r') as f:
26
config = json.load(f)
27
28
try:
29
if [*config['config']] != [*get_current_config_schema(config)['config']]:
30
config = auto_update_config(p, config)
31
except KeyError:
32
raise FileNotFoundError
33
34
break
35
except FileNotFoundError:
36
file_not_found_message = f"You don't seem to have a `config.json` file. One has been automatically created for you at: '{p / configFile}'"
37
38
make_config(p, config)
39
print(file_not_found_message)
40
return config
41
42
43
def get_current_config_schema(config: dict) -> dict:
44
config = config['config']
45
46
new_config = {
47
'config': {
48
mainProfile: config.get(mainProfile) or mainProfile,
49
'save_location': config.get('save_location') or '',
50
'file_size_limit': config.get('file_size_limit') or '',
51
}
52
}
53
return new_config
54
55
56
def make_config(path, config):
57
config = {
58
'config': {
59
mainProfile: mainProfile,
60
'save_location': '',
61
'file_size_limit': ''
62
}
63
}
64
65
with open(path / configFile, 'w') as f:
66
f.write(json.dumps(config, indent=4))
67
68
69
def update_config(field: str, value):
70
p = pathlib.Path.home() / configPath / configFile
71
72
with open(p, 'r') as f:
73
config = json.load(f)
74
75
config['config'].update({field: value})
76
77
with open(p, 'w') as f:
78
f.write(json.dumps(config, indent=4))
79
80
81
def auto_update_config(path, config: dict) -> dict:
82
print("Auto updating...")
83
new_config = get_current_config_schema(config)
84
85
with open(path / configFile, 'w') as f:
86
f.write(json.dumps(new_config, indent=4))
87
88
return new_config
89
90
91
def edit_config():
92
p = pathlib.Path.home() / configPath / configFile
93
94
with open(p, 'r') as f:
95
config = json.load(f)
96
97
updated_config = {
98
'config': config_prompt(config['config'])
99
}
100
101
with open(p, 'w') as f:
102
f.write(json.dumps(updated_config, indent=4))
103
104
print('`config.json` has been successfully edited.')
105
106