-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploader.py
More file actions
150 lines (123 loc) · 4.99 KB
/
Copy pathFileUploader.py
File metadata and controls
150 lines (123 loc) · 4.99 KB
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
141
142
143
144
145
146
147
148
149
150
import json
import os
import requests
import time
# 初始化全局的目录树结构
folder_structure = {}
def upload_directory_tree(root_dir, access_token, parent_file_id):
"""
递归上传目录树到云端。
:param root_dir: 本地目录的根路径
:param access_token: API访问令牌
:param parent_file_id: 父文件夹ID
"""
# 获取当前目录下的所有文件和子目录
entries = os.listdir(root_dir)
for entry in entries:
path = os.path.join(root_dir, entry)
if os.path.isdir(path):
# 如果是目录,则创建一个对应的云端子文件夹
folder_id = create_folder_in_cloud(entry, access_token, parent_file_id)
if folder_id is not None:
# 递归调用自身,上传子目录下的内容
upload_directory_tree(path, access_token, folder_id)
else:
# 如果已经在download_links.json中存在,则不上传
with open("download_links.json", 'r') as json_file:
links_dict = json.load(json_file)
if entry in links_dict:
print(f"File {entry} already exists in cloud. Skipping upload.")
return
upload_file_to_cloud(path, access_token, parent_file_id)
def create_folder_in_cloud(name, access_token, parent_file_id, save_path="folders_data.json"):
"""
在云端创建一个子文件夹,并更新本地JSON文件中的目录树结构。
:param name: 子文件夹的名字
:param access_token: API访问令牌
:param parent_file_id: 父文件夹ID
:param save_path: 保存文件夹数据的JSON文件路径,默认为'folders_data.json'
"""
global folder_structure
url = "https://api.kstore.space/api/v1/file/create"
headers = {
"content-type": "application/x-www-form-urlencoded"
}
payload = {
"access_token": access_token,
"fileId": parent_file_id,
"name": name,
}
response = requests.post(url, data=payload, headers=headers)
if response.status_code == 200:
data = response.json()
print(data)
folder_id = data['data']['id']
print(f"Folder {name} created successfully. ID: {folder_id}")
# 更新目录树结构
if parent_file_id not in folder_structure:
folder_structure[parent_file_id] = {'id': parent_file_id, 'subfolders': {}}
folder_structure[parent_file_id]['subfolders'][name] = {'id': folder_id, 'subfolders': {}}
# 将整个目录树结构保存到JSON文件
with open(save_path, 'w') as file:
json.dump(folder_structure, file, indent=4)
return folder_id
else:
print(f"Failed to create folder {name}. Status code: {response.status_code}")
return None
c = 0
def upload_file_to_cloud(file_path, access_token, file_id):
"""
上传文件到云端指定目录,并保存下载链接和密码。
:param file_path: 要上传的文件的本地路径
:param access_token: API访问令牌
:param file_id: 目标目录ID
"""
global c
if c == 100:
c = 0
time.sleep(3600)
c += 1
print(c)
url = f"https://upload.kstore.space/upload/{file_id}"
# 使用二进制模式打开文件以进行上传
with open(file_path, 'rb') as file:
files = {
"file": (file_path.split('\\')[-1], file) # 注意这里使用了文件名作为键的一部分
}
data = {
"access_token": access_token
}
response = requests.post(url, files=files, data=data)
if response.status_code == 200:
data = response.json()
print(data)
# 保存 downloadUrl 和 password
download_url = data['data']['downloadUrl']
password = data['data']['password']
# 拼接带有密码的完整下载链接
full_download_link = f"{download_url}?password={password}"
# 读取或创建 JSON 文件
try:
with open('download_links.json', 'r') as json_file:
links_dict = json.load(json_file)
except FileNotFoundError:
links_dict = {}
# 使用文件名作为键
file_name = file_path.split('\\')[-1]
links_dict[file_name] = {
"download_url": download_url,
"password": password,
"full_download_link": full_download_link
}
# 写入 JSON 文件
with open('download_links.json', 'w') as json_file:
json.dump(links_dict, json_file, indent=4)
print("File uploaded successfully.")
else:
print(f"Failed to upload file. Status code: {response.status_code}")
print(response.text)
# 使用示例
root_directory = r"./assets\cards_pic_classified"
access_token = "9516-9525dfa2a41d4596b77c848ee6ba5e01"
initial_parent_file_id = "0"
upload_directory_tree(root_directory, access_token, initial_parent_file_id)