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
|
window.displayIsReady = false;
let testMode = false;
const path = require('path');
// Source: https://stackoverflow.com/a/45130990
const { promisify } = require('util');
const { resolve } = require('path');
const fs = require('fs');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
async function getFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFiles(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
function extToMime(ext) {
if (ext.startsWith(".")) ext = ext.substring(1);
switch (ext) {
case "jpg":
case "jpe":
case "jpeg":
case "jfif":
case "jif":
case "jfi":
return "image/jpeg";
case "png":
return "image/png";
case "webp":
return "image/webp";
case "gif":
return "image/gif";
case "swf":
return "application/x-shockwave-flash";
case "webm":
return "video/webm";
case "mp4":
case "m4v":
case "mp4v":
return "video/mp4";
}
}
// ------------------------------------
async function slideshowMain() {
console.log("Slideshow: Flushing queue");
window.displayQueue = [];
console.log("Slideshow: Displaying loader");
initiateLoad();
let pageDerpibooru = 1;
let pageLocal = 1;
let seed = Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString("hex");
console.log(config);
async function loadNextPageDerpibooru() {
if (!config.modules.derpibooru || !(config._sessionPreferences.sources === 0 || config._sessionPreferences.sources === 2)) return;
console.log("Slideshow: Loading page " + pageDerpibooru + " from Derpibooru");
let res = await fetch("https://derpibooru.org/api/v1/json/search/images/?q=" + (testMode ? "suggestive" : "explicit") + (
config._sessionPreferences.content === 0 ?
",%20mime_type:image/*"
: (config._sessionPreferences.content === 1 ?
",%20mime_type:video/*"
:
""
)
) + ",%20faved_by:" + encodeURIComponent(config.modules.derpibooru.source) + "&per_page=" + Math.round(50 * config.modules.derpibooru.proportion) + "&filter_id=56027&sf=random:" + seed + "&page=" + pageDerpibooru);
let json = await res.json();
if (json["images"].length === 0 && pageDerpibooru !== 1) {
console.log("Slideshow: Reached the end of Derpibooru, wrapping around");
pageDerpibooru = 1;
await loadNextPageDerpibooru();
return;
}
json["images"] = json["images"].map(i => {
i["_source"] = "https://derpibooru.org/images/" + i["id"];
return i;
});
window.displayQueue.push(...json["images"].map(i => {
if (!config.modules.derpibooru['faunerie_cache']) return i;
if (!fs.existsSync(config.modules.derpibooru['faunerie_cache'] + "/images/" + i['sha512_hash'].substring(0, 1) + "/" + i['sha512_hash'].substring(0, 2) + "/" + i['sha512_hash'].substring(0, 3) + "/10" + i['id'] + ".bin")) return i;
i['view_url'] = "pbip://" + config.modules.derpibooru['faunerie_cache'] + "/images/" + i['sha512_hash'].substring(0, 1) + "/" + i['sha512_hash'].substring(0, 2) + "/" + i['sha512_hash'].substring(0, 3) + "/10" + i['id'] + ".bin";
return i;
}));
window.displayQueue = window.displayQueue.sort(() => Math.random() - Math.random());
pageDerpibooru++;
}
let fullList;
if (config.modules.local && (config._sessionPreferences.sources === 1 || config._sessionPreferences.sources === 2)) {
fullList = (await getFiles(config.modules.local.source)).filter(i => !path.basename(i).startsWith(".") && extToMime(path.extname(i)));
}
async function loadNextPageLocal() {
if (!config.modules.derpibooru || !(config._sessionPreferences.sources === 1 || config._sessionPreferences.sources === 2)) return;
console.log("Slideshow: Loading page " + pageLocal + " from local filesystem");
let res = fullList.slice((pageLocal - 1) * Math.round(50 * config.modules.derpibooru.proportion), pageLocal * Math.round(50 * config.modules.derpibooru.proportion));
let json = {
images: res.map(i => {
return {
_file: i,
id: path.basename(i),
mime_type: extToMime(path.extname(i)),
view_url: "file://" + encodeURI(i.replaceAll("\\", "/"))
}
})
};
if (json["images"].length === 0 && pageLocal !== 1) {
console.log("Slideshow: Reached the end of local filesystem, wrapping around");
pageLocal = 1;
await loadNextPageLocal();
return;
}
json["images"] = json["images"].map(i => {
i["_source"] = i["_file"];
return i;
});
window.displayQueue.push(...json["images"]);
window.displayQueue = window.displayQueue.sort(() => Math.random() - Math.random()).filter(i => {
if (config._sessionPreferences.content === 0) {
return i.mime_type.startsWith("image/");
} else if (config._sessionPreferences.content === 1) {
return i.mime_type.startsWith("video/");
} else {
return true;
}
});
pageLocal++;
}
console.log("Slideshow: Loading first page");
await loadNextPageDerpibooru();
await loadNextPageLocal();
console.log("Slideshow: Starting slideshow");
await startSlideshow(false, async () => {
await loadNextPageDerpibooru();
await loadNextPageLocal();
});
console.log("Slideshow: Removing loader");
completeLoad();
window.displayIsReady = true;
}
window.addEventListener("load", () => {
setTimeout(async () => {
window.config = await require('electron/renderer').ipcRenderer.invoke("getConfig");
await slideshowMain();
}, 1000);
});
|