File size: 5,707 Bytes
1fa9a79
 
 
98f37e9
1fa9a79
0079733
 
1fa9a79
2a6b17e
23776b9
 
 
 
 
1fa9a79
 
 
 
 
 
 
6383113
2f9ec38
 
 
 
b87bfea
6383113
 
 
 
2f9ec38
 
 
6383113
 
2f9ec38
6383113
b87bfea
6383113
 
2f9ec38
 
 
 
 
 
 
 
 
 
 
 
6383113
2f9ec38
 
 
 
6383113
 
 
 
 
acf0349
2f9ec38
 
acf0349
2f9ec38
 
 
869de46
acf0349
 
869de46
acf0349
 
 
2f9ec38
869de46
2f9ec38
6383113
2f9ec38
 
 
6383113
2f9ec38
 
 
 
b87bfea
6383113
2f9ec38
 
 
 
 
 
 
 
 
 
6383113
 
2f9ec38
 
 
 
 
 
 
 
 
 
869de46
 
2f9ec38
 
869de46
2f9ec38
 
6383113
 
 
 
2f9ec38
6383113
2f9ec38
 
 
 
6383113
 
b87bfea
1fa9a79
0079733
 
2bf30d8
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

def check_proxy(proxies):
    import requests
    proxies_https = proxies['https'] if proxies is not None else '无'
    try:
        response = requests.get("https://ipapi.co/json/",
                                proxies=proxies, timeout=4)
        data = response.json()
        print(f'查询代理的地理位置,返回的结果是{data}')
        if 'country_name' in data:
            country = data['country_name']
            result = f"代理配置 {proxies_https}, 代理所在地:{country}"
        elif 'error' in data:
            result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限"
        print(result)
        return result
    except:
        result = f"代理配置 {proxies_https}, 代理所在地查询超时,代理可能无效"
        print(result)
        return result


def backup_and_download(current_version, remote_version):
    """
    一键更新协议:备份和下载
    """
    from toolbox import get_conf
    import shutil
    import os
    import requests
    import zipfile
    os.makedirs(f'./history', exist_ok=True)
    backup_dir = f'./history/backup-{current_version}/'
    new_version_dir = f'./history/new-version-{remote_version}/'
    if os.path.exists(new_version_dir):
        return new_version_dir
    os.makedirs(new_version_dir)
    shutil.copytree('./', backup_dir, ignore=lambda x, y: ['history'])
    proxies, = get_conf('proxies')
    r = requests.get(
        'https://github.com/binary-husky/chatgpt_academic/archive/refs/heads/master.zip', proxies=proxies, stream=True)
    zip_file_path = backup_dir+'/master.zip'
    with open(zip_file_path, 'wb+') as f:
        f.write(r.content)
    dst_path = new_version_dir
    with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
        for zip_info in zip_ref.infolist():
            dst_file_path = os.path.join(dst_path, zip_info.filename)
            if os.path.exists(dst_file_path):
                os.remove(dst_file_path)
            zip_ref.extract(zip_info, dst_path)
    return new_version_dir


def patch_and_restart(path):
    """
    一键更新协议:覆盖和重启
    """
    import distutils
    import shutil
    import os
    import sys
    import time
    from colorful import print亮黄, print亮绿, print亮红
    # if not using config_private, move origin config.py as config_private.py
    if not os.path.exists('config_private.py'):
        print亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,',
              '另外您可以随时在history子文件夹下找回旧版的程序。')
        shutil.copyfile('config.py', 'config_private.py')
    distutils.dir_util.copy_tree(path+'/chatgpt_academic-master', './')
    import subprocess
    print亮绿('更新包依赖中……')
    for i in reversed(range(5)): time.sleep(1); print(i)
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
    print亮绿('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启')
    print亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')
    for i in reversed(range(5)): time.sleep(1); print(i)
    print(' ------------------------------ -----------------------------------')
    os.execl(sys.executable, sys.executable, *sys.argv)


def get_current_version():
    import json
    try:
        with open('./version', 'r', encoding='utf8') as f:
            current_version = json.loads(f.read())['version']
    except:
        current_version = ""
    return current_version


def auto_update():
    """
    一键更新协议:查询版本和用户意见
    """
    try:
        from toolbox import get_conf
        import requests
        import time
        import json
        proxies, = get_conf('proxies')
        response = requests.get(
            "https://raw.githubusercontent.com/binary-husky/chatgpt_academic/master/version", proxies=proxies, timeout=1)
        remote_json_data = json.loads(response.text)
        remote_version = remote_json_data['version']
        if remote_json_data["show_feature"]:
            new_feature = "新功能:" + remote_json_data["new_feature"]
        else:
            new_feature = ""
        with open('./version', 'r', encoding='utf8') as f:
            current_version = f.read()
            current_version = json.loads(current_version)['version']
        if (remote_version - current_version) >= 0.05:
            from colorful import print亮黄
            print亮黄(
                f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}{new_feature}')
            print('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
            user_instruction = input('(2)是否一键更新代码(Y+回车=确认,输入其他/无输入+回车=不更新)?')
            if user_instruction in ['Y', 'y']:
                path = backup_and_download(current_version, remote_version)
                try:
                    patch_and_restart(path)
                except:
                    print('更新失败。')
            else:
                print('自动更新程序:已禁用')
                return
        else:
            return
    except:
        print('自动更新程序:已禁用')


if __name__ == '__main__':
    import os
    os.environ['no_proxy'] = '*'  # 避免代理网络产生意外污染
    from toolbox import get_conf
    proxies, = get_conf('proxies')
    check_proxy(proxies)