-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WRITE_INFO_FROM_URL.py
70 lines (53 loc) · 2.07 KB
/
WRITE_INFO_FROM_URL.py
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
import argparse
import configparser
from GET_INFO_FROM_URL import GET_APPTOKEN_FROM_URL, GET_TABLEID_FROM_URL, GET_VIEWID_FROM_URL
def WRITE_INFO_FROM_URL(url, config_file="feishu-config.ini"):
config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8')
try:
app_token = GET_APPTOKEN_FROM_URL(url)
table_id = GET_TABLEID_FROM_URL(url)
view_id = GET_VIEWID_FROM_URL(url)
if not app_token:
app_token = ''
if not table_id:
table_id = ''
if not view_id:
view_id = ''
except Exception:
return None
if 'TOKEN' not in config:
config.add_section('TOKEN')
config['TOKEN']['app_token'] = app_token
if 'ID' not in config:
config.add_section('ID')
config['ID']['table_id'] = table_id
config['ID']['view_id'] = view_id
try:
with open(config_file, 'w', encoding='utf-8') as configfile:
config.write(configfile)
except Exception:
return None
return app_token, table_id, view_id
def WRITE_INFO_FROM_URL_CMD():
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', default=None, help='用于提取令牌的URL')
parser.add_argument('-i', '--input', default=None, help='用于提取令牌的URL')
parser.add_argument('-c', '--config', default="feishu-config.ini", help='配置文件路径')
args = parser.parse_args()
url = args.url if args.url is not None else args.input
config_file = args.config
if url is None:
print("错误:未提供URL,请使用'-u/--url'或'-i/--input'参数提供URL。")
return
result = WRITE_INFO_FROM_URL(url, config_file=config_file)
if result is None:
print("发生错误,请检查您的输入URL并再试一次。")
else:
app_token, table_id, view_id = result
print(f"app_token: {app_token}")
print(f"table_id: {table_id}")
print(f"view_id: {view_id}")
print(f"成功写入 '{config_file}' 文件")
if __name__ == "__main__":
WRITE_INFO_FROM_URL_CMD()