-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-scan.js
62 lines (54 loc) · 1.84 KB
/
update-scan.js
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
'use strict'
module.exports = updateScan
const url = require('url')
const { Site } = require('@fanfic/parser')
const deeplyEquivalent = require('./deeply-equivalent.js')
function ficEqual (ficA, ficB) {
if (typeof ficA !== 'object') return false
if (typeof ficB !== 'object') return false
return deeplyEquivalent(ficA.toJSON(), ficB.toJSON())
}
async function updateScan (fetch, activeScan) {
const site = Site.create(activeScan.conf.engine || activeScan.conf.link)
let lastSeen = await activeScan.data.lastSeen() || 0
let nextPage = activeScan.conf.link
let pageId = url.parse(activeScan.conf.link).hash
let newerThan = lastSeen
while (nextPage) {
const res = await fetch(site.fetchLink(nextPage))
const scan = site.parseScan(nextPage, await res.buffer(), pageId)
const existingItems = {}
const existingFics = await activeScan.data.getByIds(scan.fics.map(_ => _.siteId))
existingFics.forEach(existing => {
if (existing == null) return
existingItems[existing.siteId] = existing
})
nextPage = scan.nextPage
let sawAnyNewer
for (let fic of scan.fics) {
const updated = fic.updated
if (updated > newerThan) {
sawAnyNewer = true
}
if (updated > lastSeen) {
lastSeen = updated
}
if (!fic.siteId) {
//console.error('Skipping, no id', fic.link)
continue
}
const existing = existingItems[fic.siteId]
// no changes, skip
if (ficEqual(fic, existing)) continue
const tagMatch = fic.tagMatch(activeScan.conf.filterTags)
const entryMatch = fic.entryMatch(activeScan.conf.filterEntry)
if (existing || tagMatch || entryMatch) {
await activeScan.data.replace(fic)
}
}
if (newerThan && !sawAnyNewer) break
}
if (lastSeen > newerThan) {
await activeScan.data.setLastSeen(lastSeen)
}
}