File size: 11,569 Bytes
394128f
 
 
 
 
 
 
74aa496
 
394128f
 
 
 
74aa496
394128f
 
 
 
 
 
 
 
 
74aa496
 
 
 
394128f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34e52d
394128f
 
 
 
 
 
 
 
 
 
 
74aa496
 
 
 
 
d34e52d
74aa496
d34e52d
74aa496
 
394128f
df86cf2
74aa496
df86cf2
 
74aa496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34e52d
74aa496
 
394128f
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import json
import re
import subprocess
import shutil
import os
from concurrent.futures import ThreadPoolExecutor
import sys
import traceback
from urllib.parse import urlparse



params = {}
cookie=''
for arg in sys.argv[1:]:
    if arg.startswith('--'):
        key_value = arg[len('--'):].split('=')
        if len(key_value) == 2:
            key, value = key_value
            params[key] = value

check_dir = f"{params['dir']}/models/Stable-diffusion"
lora_dir = f"{params['dir']}/models/Lora"

with open(f'/content/cookie.txt', 'r') as readFile:
    cookie=readFile.read()

def swap(arr, l, r):
    temp = arr[l]
    arr[l] = arr[r]
    arr[r] = temp


def checkRelateMod(oldMod, newMod):
    try:
        return oldMod and newMod and oldMod['downloadLink'] == newMod['downloadLink']
    except Exception as e:
        print(e)


def sanitize_filename(filename):
    # 定义不兼容字符的正则表达式模式
    pattern = r'[\\/:*?"<>|]'
    # 使用下划线替换不兼容字符
    sanitized_filename = re.sub(pattern, '_', filename)
    return sanitized_filename


def compatibility(preContent):
    for item in preContent:
        if bool(item['path']):
            item['path'] = re.sub(r'^(/[^/]+/[^/]+)', params['dir'], item['path'])

def dealRelateMod(oldRenameMod, newRenameMod):
    old_file_path = f"{oldRenameMod['path']}/{oldRenameMod['name']}"
    # 新模型的定义不带后缀名,所以需要加上处理,且在预处理情况下不知道路径那么只能沿用老模型的路径和名字
    new_file_dir = (
        newRenameMod['path']
        if newRenameMod.get('path') and newRenameMod['path'] != ""
        else oldRenameMod['path']
    )
    new_file_name = (
        sanitize_filename(newRenameMod['name']) + os.path.splitext(oldRenameMod['name'])[1]
        if newRenameMod['name'] and newRenameMod['name'] != "未命名"
        else oldRenameMod['name']
    )
    new_file_path = f"{new_file_dir}/{new_file_name}"
    if old_file_path != new_file_path:
        try:
            if os.path.exists(old_file_path):
                os.rename(old_file_path, new_file_path)
                oldRenameMod['path'] = new_file_dir
                oldRenameMod['name'] = new_file_name
                print(f"{old_file_path}更改为{new_file_path}成功")
            else:
                print(f"{old_file_path}不存在,重命名失败")
        except OSError as e:
            print(f"更改{old_file_path}时发生错误: {e}")
    else:
        print(f"{newRenameMod['name']}已存在且无任何改变,将忽视")


def deleteMod(oldDeMod):
    file_path = f"{oldDeMod['path']}/{oldDeMod['name']}"
    try:
        if os.path.exists(file_path):
            os.remove(file_path)
            # oldCo的行也要进行同步删除
            print(f"{oldDeMod['name']}删除成功")
        else:
            print(f"{oldDeMod['name']}不存在,删除失败")
    except OSError as e:
        print(f"删除{oldDeMod['name']}时发生错误: {e}")


def LCS(oldCo, newCo):
    oldStartIdx = 0  # 旧前指针
    newStartIdx = 0  # 新前指针
    oldEndIdx = len(oldCo) - 1  # 旧后指针
    newEndIdx = len(newCo) - 1  # 新后指针
    oldStartMod = oldCo[oldStartIdx]  # 旧前Mod
    oldEndMod = oldCo[oldEndIdx]  # 旧后Mod
    newStartMod = newCo[newStartIdx]  # 新前Mod
    newEndMod = newCo[newEndIdx]  # 新后Mod
    linkMap = {}
    while oldStartIdx <= oldEndIdx and newStartIdx <= newEndIdx:
        # 新前旧前
        if checkRelateMod(oldStartMod, newStartMod):
            dealRelateMod(oldStartMod, newStartMod)
            oldStartIdx += 1
            newStartIdx += 1
            if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
                break
            oldStartMod = oldCo[oldStartIdx]
            newStartMod = newCo[newStartIdx]
        # 新后旧后
        elif checkRelateMod(oldEndMod, newEndMod):
            dealRelateMod(oldEndMod, newEndMod)
            oldEndIdx -= 1
            newEndIdx -= 1
            if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
                break
            oldEndMod = oldCo[oldEndIdx]
            newEndMod = newCo[newEndIdx]
        # 新后旧前
        elif checkRelateMod(oldStartMod, newEndMod):
            dealRelateMod(oldStartMod, newEndMod)
            oldStartIdx += 1
            newEndIdx -= 1
            if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
                break
            oldStartMod = oldCo[oldStartIdx]
            newEndMod = newCo[newEndIdx]
        # 新前旧后
        elif checkRelateMod(oldEndMod, newStartMod):
            dealRelateMod(oldEndMod, newStartMod)
            oldEndIdx -= 1
            newStartIdx += 1
            if newStartIdx > newEndIdx or oldStartIdx > oldEndIdx:
                break
            oldEndMod = oldCo[oldEndIdx]
            newStartMod = newCo[newStartIdx]
        # 四种均未找到
        else:
            # if not linkMap:
            linkMap = {}
            # 从 oldStartIdx 开始,到oldEndIdx结束,创建linkMap映射对象
            for i in range(oldStartIdx, oldEndIdx + 1):
                downloadLink = oldCo[i]['downloadLink']
                if downloadLink is not None:
                    linkMap[downloadLink] = i
            idxInOld = linkMap.get(newStartMod['downloadLink'], None)
            if idxInOld is None:
                print(f"{newStartMod['name']}不存在于旧mod,{newStartMod['downloadLink']}将添加到下载任务队列")
                content.append(newStartMod)

            else:
                dealRelateMod(oldCo[idxInOld], newStartMod)
                swap(oldCo, idxInOld, oldEndIdx)
                oldEndIdx -= 1
            # 指针下移,移动新的头
            newStartIdx += 1
            if newStartIdx > newEndIdx:
                break
            newStartMod = newCo[newStartIdx]
            oldEndMod = oldCo[oldEndIdx]
            # oldStartIdx += 1
    print('new', newStartIdx, newEndIdx)
    print('old', oldStartIdx, oldEndIdx)
    # new这里还有剩余Mod没有处理
    if newStartIdx <= newEndIdx:
        for i in range(newStartIdx, newEndIdx + 1):
            print(f"{newCo[i]['name']}为新且不重复mod,将添加到下载任务队列")
            content.append(newCo[i])

    elif oldStartIdx <= oldEndIdx:
        i = oldStartIdx
        while i <= oldEndIdx:
            if i < len(oldCo):
                print(f"{oldCo[i]['name']}为旧不重复mod,将删除")
                deleteMod(oldCo[i])
                del oldCo[i]
                oldEndIdx -= 1
            else:
                break


# LCS(oldArr,oldArr)
with open(params["json_dir"], 'r') as modelFile:
    preContent = json.loads(modelFile.read())
    compatibility(preContent)

oldCo = json.loads(os.environ["oldCo"])

# 更新阶段
if 'oldCo' and len(oldCo) > 0:
    content = []
    print(oldCo)
    print(preContent)
    LCS(oldCo, preContent)
    print(content)
# 初始化阶段
else:
    oldCo = []
    content = preContent

# 部分下载的情况
selected_mods = [x.strip() for x in params["name"].split('与') if x.strip()]


def get_file_size(file_path):
    try:
        file_size_bytes = os.path.getsize(file_path)
        for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
            if file_size_bytes < 1024.0:
                break
            file_size_bytes /= 1024.0
        return f"{file_size_bytes:.2f} {unit}"
    except OSError as e:
        print(f"Error: {e}")
        return None


def get_civitai_file(str):
    match = re.search(r'[^/]+$', str.decode())
    if match:
        return match.group(0).split('\n')[0]
    else:
        print("无法从输出中提取文件名")
        return ''


def move_model(source_path, target_path):
    try:
        shutil.move(source_path, target_path)
    except OSError as e:
        print(f"Error: {e}")


def download_file(item):
    # for item in content:
    try:
      if selected_mods and item['name'] not in selected_mods:
          return
      download_url = item['downloadLink']
      # huggingface
      file_name = urlparse(download_url).path.split("/")[-1]
      # 开始下载
      if '.' not in file_name:
        if len(cookie)>0:
          cmd = ['aria2c', '--header', f'Cookie:{cookie}', '--console-log-level=error', '-c', '-x', '16', '-s', '16', '-k', '1M', download_url, '-d', lora_dir]
        else:
          cmd = ['aria2c',  '--console-log-level=error', '-c', '-x', '16', '-s', '16', '-k', '1M', download_url, '-d', lora_dir]
      else:
        cmd = ['aria2c',  '--console-log-level=error', '-c', '-x', '16', '-s', '16', '-k', '1M', download_url, '-d', lora_dir ,"-o",file_name]
      result = subprocess.run(cmd ,stderr=subprocess. PIPE, stdout=subprocess.PIPE)
      if result.returncode == 0:
          # 判断是civitai还是hugging face还是自定义
          # civitai
          if '.' not in file_name:
              file_name = get_civitai_file(result.stdout)
          # 如果定义了名字则重命名
          if item['name'] != '未命名':
              # 需要进行文件名的兼容处理
              temp_name = sanitize_filename(item['name']) + os.path.splitext(file_name)[1]
              os.rename(f"{lora_dir}/{file_name}", f"{lora_dir}/{temp_name}")
              final_name = temp_name
          else:
              final_name = file_name
          source_path = f"{lora_dir}/{final_name}"
          file_size = get_file_size(source_path)
          # 如果path定义了则进行移动
          if item.get('path') and item['path'] != "":
              target_path = item['path']
              move_model(source_path, target_path)
              item['path'] = target_path
              print(file_name + '已下载,重命名为:' + final_name)
              print('移动--', final_name, f'到{target_path}')
          # 如果为checkpoint则进行移动
          elif file_size and 'GB' in file_size:
              move_model(source_path, check_dir)
              item['path'] = check_dir
              print(file_name + '已下载,重命名为:' + final_name)
              print('移动checkpoint--', final_name, f'到{check_dir}文件夹')
          # 如果为lora则直接调用source_path
          else:
              item['path'] = lora_dir
              print(file_name + '已下载,重命名为:' + final_name)
          # 改变item['name']用于最小化更新的重命名判断
          item['name'] = final_name
          oldCo.append(item)
      else:
          if "login" in get_civitai_file(result.stdout):
            print("===============================================================================\n   ")
            print(f"{item['name']}下载失败,需要登录civitai进行下载,请按照如下步骤获取cookie填入")
            print("1.ctrl+t打开空白页,按f12选择\"网络\"")
            print(f"2.粘贴{item['downloadLink']}到链接输入栏进行跳转同时观察包含http s://civitai.com/api/download 的这个请求")
            print(f"3.将请求头的cookie复制下来在cookie的输入框中填入,重新执行2.2进行下载\n   ")
            print("===============================================================================")
          print(f"\033[91m{item['name']}下载失败,请检查{item['downloadLink']}\033[0m")
    except Exception as e:
      traceback.print_exc()


# download_file(item)
# 使用线程池进行下载
with ThreadPoolExecutor(max_workers=5) as executor:
    for item in content:
        executor.submit(download_file, item)