#!/usr/bin/python3
import os
import requests
from ruamel import yaml
from tqdm import tqdm

URL = "https://dler.cloud/link/8y2MrCU9eLYaLkCn?is_mu=1&dns=1&clash=1"
# 你的clash-config的文件夹位置，这里写了默认位置注意修改username
PATH = "/home/suun/.config/clash/"
NAME = "config.yml"  # 默认的config文件名

print("Welcome to Clash Auto-Updater!")

r = requests.get(url=URL, stream=True)
content_size = int(r.headers['Content-Length']) / 1024

with open(PATH + NAME, 'wb') as f:
    print("Size: ", content_size, 'k, Start Downloading...')
    for data in tqdm(iterable=r.iter_content(512), total=content_size, unit='k', desc=NAME):
        f.write(data)
    print(NAME + ": All Done!")

with open(PATH + NAME, 'r+') as f:
    config = yaml.load(f, Loader=yaml.RoundTripLoader)
    config['socks-port'] = 1080
    config['port'] = 12333
with open(PATH + NAME, 'w') as nf:
    yaml.dump(config, nf, Dumper=yaml.RoundTripDumper)

os.system('pm2 restart clash')

print("Finish!")
