File size: 4,205 Bytes
2d1ec2f
 
06567e1
 
 
 
 
2d1ec2f
 
 
 
06567e1
2d1ec2f
 
 
 
 
 
06567e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d1ec2f
06567e1
 
 
 
2d1ec2f
06567e1
2d1ec2f
 
 
 
 
 
06567e1
2d1ec2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06567e1
 
 
2d1ec2f
 
 
06567e1
2d1ec2f
06567e1
2d1ec2f
 
 
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
/* 
 *  This script exists to merge all .info files into one array.
 *  unfortunately, civitai is storing some of them as LFS objects
 *  so the script outputs a list of errors along with scripts to fix them (delfiles.sh, replacefiles.sh)
 *  others files are blank, so just ignore the errors the second time, they're files that aren't on civitai
 *  now we get tot he important part: it parses all files, and dumps all of them into an array in civitai.info.json for further processing
 *  Run once, run delfiles.bat or delfiles.sh to get rid of the bad ones, then run replacefiles.sh to fetch them from civitai.
 * 
 *  the reason I chose to output scripts (and use two of them) rather than doing it internally is two-fold:
 *  1. I can inspect delfiles.sh being deleted before running it
 *  2. I can rewrite the replacefiles.sh with a script to use a download manager (aria2c) when it's large. (this script is intended to be used in colab)
 */

// change this to true if you want the full contents of every .info file, otherwise we're deleting a bunch of stuff I don't care about
// keep in mind this changes the format of the file (I've replaced the image object with an array of urls that have no metadata)
// so any scripts I write won't work with it anymore. This reduced the size of the file by nearly 80%...The original data is still in the original files.
var fullFiles = false; 

var path = require('path'),
fs = require('fs');

function fromDir(startPath, filter, arr) {
    arr=arr||[];
    //console.log('Starting from dir '+startPath+'/');
    if (!fs.existsSync(startPath)) {
        console.log("no dir ", startPath);
        return;
    }

    var files = fs.readdirSync(startPath);
    for (var i = 0; i < files.length; i++) {
        var filename = path.join(startPath, files[i]);
        var stat = fs.lstatSync(filename);
        if (stat.isDirectory()) {
            fromDir(filename, filter,arr); //recurse
        } else if (filename.endsWith(filter)) {
            arr.push(filename);
            //console.log('-- found: ', filename);
        };
    };
    return arr;
};

var addons=[]; var errors=[]; errors2=[];
var push=function(file){
	try{
		var addon=JSON.parse(fs.readFileSync(file));
	} catch(err) {
		console.error(file + " is probably an LFS pointer. Will be added to delfiles and replacefiles scripts!");
		errors.push(file);
		return;
	}
	if(Object.keys(addon).length==0) {
		console.error(file + " is an empty file! Will be added to delfiles script! Assuming it's not a mistake, it will *not* be replaced!");
		errors2.push(file);
		return;
	}
	addon.mirror_path=file; // Add the location we found the file.

	// We're going to clean information idc about. Change `false` to `true` at the top if you want all data.
	if(fullFiles == true) {
		addons.push(addon);
		return;
	}
	delete addon.model.poi;
	'createdAt,updatedAt,earlyAccessTimeFrame,downloadUrl,description'.split(',').forEach(key=>{delete addon[key]});
	for(file=addon.files.length-1;file>=0;file--) {
		if(addon.files[file].type == 'Training Data' || addon.files[file].type == 'Config') {
			addon.files.splice(file,1);
		} else {
			'sizeKB,format,pickleScanResult,pickleScanMessage,virusScanResult,scannedAt,hashes,primary,downloadUrl'.split(',').forEach(key=>{
				delete addon.files[file][key];
			});
		}
	};
	var imgs=addon.images;
	addon.images=[];
	imgs.forEach(img=>{addon.images.push(img.url.replace('https://imagecache.civitai.com/',''))});
	addons.push(addon);
};

var files=fromDir('.', '.civitai.info');
files.forEach(file=>{push(file)});
//push(files[0]);
var errs=[...errors,...errors2].sort();
fs.writeFileSync('civitai.info.json',JSON.stringify(addons))
fs.writeFileSync('errors.json',JSON.stringify(errs))
//console.error("Error with the following files:",errors);
fs.writeFileSync('delfiles.bat','@echo off\n\n'+errs.map(e=>{return "del \""+ e + "\""}).join('\n'))
fs.writeFileSync('delfiles.sh',errs.map(e=>{return "rm \""+ e + "\""}).join('\n').split('\\').join('/'))
fs.writeFileSync('replacefiles.sh',errors.map(e=>{return "wget \"https://huggingface.co/anonderpling/civitai_mirror/resolve/main/"+(e.split(" ").join("%23")+"\" -O \""+e+"\"")}).join('\n').split('\\').join('\/'))