-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,5 @@ qcapi | |
claude.json | ||
bard.json | ||
/*yaml | ||
!/docker-compose.yaml | ||
!/docker-compose.yaml | ||
res/instance_id.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import os | ||
import uuid | ||
import json | ||
import time | ||
|
||
|
||
identifier = { | ||
'host_id': '', | ||
'instance_id': '', | ||
'host_create_ts': 0, | ||
'instance_create_ts': 0, | ||
} | ||
|
||
HOST_ID_FILE = os.path.expanduser('~/.qchatgpt/host_id.json') | ||
INSTANCE_ID_FILE = 'res/instance_id.json' | ||
|
||
def init(): | ||
global identifier | ||
|
||
if not os.path.exists(os.path.expanduser('~/.qchatgpt')): | ||
os.mkdir(os.path.expanduser('~/.qchatgpt')) | ||
|
||
if not os.path.exists(HOST_ID_FILE): | ||
new_host_id = 'host_'+str(uuid.uuid4()) | ||
new_host_create_ts = int(time.time()) | ||
|
||
with open(HOST_ID_FILE, 'w') as f: | ||
json.dump({ | ||
'host_id': new_host_id, | ||
'host_create_ts': new_host_create_ts | ||
}, f) | ||
|
||
identifier['host_id'] = new_host_id | ||
identifier['host_create_ts'] = new_host_create_ts | ||
else: | ||
loaded_host_id = '' | ||
loaded_host_create_ts = 0 | ||
|
||
with open(HOST_ID_FILE, 'r') as f: | ||
file_content = json.load(f) | ||
loaded_host_id = file_content['host_id'] | ||
loaded_host_create_ts = file_content['host_create_ts'] | ||
|
||
identifier['host_id'] = loaded_host_id | ||
identifier['host_create_ts'] = loaded_host_create_ts | ||
|
||
# 检查实例 id | ||
if os.path.exists(INSTANCE_ID_FILE): | ||
instance_id = {} | ||
with open(INSTANCE_ID_FILE, 'r') as f: | ||
instance_id = json.load(f) | ||
|
||
if instance_id['host_id'] != identifier['host_id']: # 如果实例 id 不是当前主机的,删除 | ||
os.remove(INSTANCE_ID_FILE) | ||
|
||
if not os.path.exists(INSTANCE_ID_FILE): | ||
new_instance_id = 'instance_'+str(uuid.uuid4()) | ||
new_instance_create_ts = int(time.time()) | ||
|
||
with open(INSTANCE_ID_FILE, 'w') as f: | ||
json.dump({ | ||
'host_id': identifier['host_id'], | ||
'instance_id': new_instance_id, | ||
'instance_create_ts': new_instance_create_ts | ||
}, f) | ||
|
||
identifier['instance_id'] = new_instance_id | ||
identifier['instance_create_ts'] = new_instance_create_ts | ||
else: | ||
loaded_instance_id = '' | ||
loaded_instance_create_ts = 0 | ||
|
||
with open(INSTANCE_ID_FILE, 'r') as f: | ||
file_content = json.load(f) | ||
loaded_instance_id = file_content['instance_id'] | ||
loaded_instance_create_ts = file_content['instance_create_ts'] | ||
|
||
identifier['instance_id'] = loaded_instance_id | ||
identifier['instance_create_ts'] = loaded_instance_create_ts | ||
|
||
def print_out(): | ||
global identifier | ||
print(identifier) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import os | ||
import uuid | ||
import json | ||
|
||
# 向 ~/.qchatgpt 写入一个 标识符 | ||
|
||
if not os.path.exists(os.path.expanduser('~/.qchatgpt')): | ||
os.mkdir(os.path.expanduser('~/.qchatgpt')) | ||
|
||
identifier = { | ||
"host_id": "host_"+str(uuid.uuid4()), | ||
} | ||
|
||
if not os.path.exists(os.path.expanduser('~/.qchatgpt/host.json')): | ||
print('create ~/.qchatgpt/host.json') | ||
with open(os.path.expanduser('~/.qchatgpt/host.json'), 'w') as f: | ||
json.dump(identifier, f) | ||
else: | ||
print('load ~/.qchatgpt/host.json') | ||
with open(os.path.expanduser('~/.qchatgpt/host.json'), 'r') as f: | ||
identifier = json.load(f) | ||
|
||
print(identifier) | ||
|
||
instance_id = { | ||
"host_id": identifier['host_id'], | ||
"instance_id": "instance_"+str(uuid.uuid4()), | ||
} | ||
|
||
# 实例 id | ||
if os.path.exists("res/instance_id.json"): | ||
with open("res/instance_id.json", 'r') as f: | ||
instance_id = json.load(f) | ||
|
||
if instance_id['host_id'] != identifier['host_id']: | ||
os.remove("res/instance_id.json") | ||
|
||
if not os.path.exists("res/instance_id.json"): | ||
print('create res/instance_id.json') | ||
with open("res/instance_id.json", 'w') as f: | ||
json.dump(instance_id, f) | ||
|
||
print(instance_id) |