-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
65 lines (52 loc) · 1.89 KB
/
worker.js
File metadata and controls
65 lines (52 loc) · 1.89 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
const path = require('path')
const fs = require('fs')
const schedule = require('node-schedule')
const shell = require('shelljs')
const mongoose = require('mongoose')
const config = require('./config')
const { libraryPath, nhentaiDir, ehentaiDir, wnacgDir, mongodbUrl } = config
mongoose.Promise = global.Promise
mongoose.connect(mongodbUrl, { useNewUrlParser: true })
let sqlite
const ComicbookCalibre = mongoose.model('ComicbookCalibre', {
storeInCalibre: Boolean,
domain: String,
id: String,
filepath: String
})
const add_calibre_library = async (libraryPath, addDir) => {
if (!fs.existsSync(addDir)) return
const files = fs.readdirSync(addDir)
for (const file of files) {
const filepath = path.join(addDir, file)
const stat = fs.lstatSync(filepath)
if (!stat.isFile()) continue
if (!file.includes('.epub')) continue
const { stdout } = shell.exec(`calibredb add --with-library=${libraryPath} ${filepath}`)
const found = stdout.match(/id[s]?\: (\d+)/)
if (!found) continue
const bookId = found[1]
if (!sqlite) sqlite = require('better-sqlite3')(path.join(libraryPath, 'metadata.db'))
const row = sqlite.prepare('select B.path, D.name from books B left join data D on (B.id = D.id) where B.id = ?').get(bookId)
if (!row) continue
const calibreBookPath = path.join(row.path, `${row.name}.epub`)
const splited = file.replace('.epub', '').split('@')
const [domain, id] = splited
const cc = await ComicbookCalibre.findOne({
domain, id,
})
await cc.updateOne({
$set: {
storeInCalibre: true,
filepath: calibreBookPath,
}
})
shell.rm(filepath)
}
}
add_calibre_library(libraryPath, nhentaiDir)
schedule.scheduleJob('* * * * *', async () => {
await add_calibre_library(libraryPath, nhentaiDir)
await add_calibre_library(libraryPath, ehentaiDir)
await add_calibre_library(libraryPath, wnacgDir)
})