Path: blob/main/onlyfans_scraper/utils/config.py
961 views
r"""1_ __2___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __3/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|4| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |5\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|6|___/ |_|7"""89import json10import pathlib1112from .prompts import config_prompt13from ..constants import configPath, configFile, mainProfile141516def read_config():17p = pathlib.Path.home() / configPath18if not p.is_dir():19p.mkdir(parents=True, exist_ok=True)2021config = {}22while True:23try:24with open(p / configFile, 'r') as f:25config = json.load(f)2627try:28if [*config['config']] != [*get_current_config_schema(config)['config']]:29config = auto_update_config(p, config)30except KeyError:31raise FileNotFoundError3233break34except FileNotFoundError:35file_not_found_message = f"You don't seem to have a `config.json` file. One has been automatically created for you at: '{p / configFile}'"3637make_config(p, config)38print(file_not_found_message)39return config404142def get_current_config_schema(config: dict) -> dict:43config = config['config']4445new_config = {46'config': {47mainProfile: config.get(mainProfile) or mainProfile,48'save_location': config.get('save_location') or '',49'file_size_limit': config.get('file_size_limit') or '',50}51}52return new_config535455def make_config(path, config):56config = {57'config': {58mainProfile: mainProfile,59'save_location': '',60'file_size_limit': ''61}62}6364with open(path / configFile, 'w') as f:65f.write(json.dumps(config, indent=4))666768def update_config(field: str, value):69p = pathlib.Path.home() / configPath / configFile7071with open(p, 'r') as f:72config = json.load(f)7374config['config'].update({field: value})7576with open(p, 'w') as f:77f.write(json.dumps(config, indent=4))787980def auto_update_config(path, config: dict) -> dict:81print("Auto updating...")82new_config = get_current_config_schema(config)8384with open(path / configFile, 'w') as f:85f.write(json.dumps(new_config, indent=4))8687return new_config888990def edit_config():91p = pathlib.Path.home() / configPath / configFile9293with open(p, 'r') as f:94config = json.load(f)9596updated_config = {97'config': config_prompt(config['config'])98}99100with open(p, 'w') as f:101f.write(json.dumps(updated_config, indent=4))102103print('`config.json` has been successfully edited.')104105106