1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-16 13:17:30 +00:00

Automatically download test files when required

This commit is contained in:
Cadence Ember 2020-07-07 22:08:47 +12:00
parent c3455ec183
commit 642e543fd6
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412
2 changed files with 31 additions and 4 deletions

View File

@ -5,6 +5,7 @@ const {promisify} = require("util")
const crypto = require("crypto") const crypto = require("crypto")
const stream = require("stream") const stream = require("stream")
const constants = require("../../constants")
const db = require("../../db") const db = require("../../db")
const Saved = require("../request_backends/saved") const Saved = require("../request_backends/saved")
const NodeFetch = require("../request_backends/node-fetch") const NodeFetch = require("../request_backends/node-fetch")
@ -56,9 +57,32 @@ class SavedRequestManager {
request() { request() {
const row = db.prepare("SELECT * FROM SavedRequests WHERE url = ?").get(this.url) const row = db.prepare("SELECT * FROM SavedRequests WHERE url = ?").get(this.url)
if (row) { if (row) {
console.log("Found, using saved request for "+row.path)
const base = pj(folder, row.path) const base = pj(folder, row.path)
try {
fs.accessSync(base)
console.log("Found, using saved request for "+row.path)
return new Saved(base) return new Saved(base)
} catch (e) {
console.log("Known, but not downloaded for "+row.path)
function download(name) {
return new NodeFetch(constants.external.saved_requests_location+name).stream().then(readable => {
return promisify(stream.pipeline)(
readable,
fs.createWriteStream(pj(folder, name))
)
})
}
return new DelayedBackend(
Promise.all([
download(row.path),
download(row.path + ".meta.json")
]).then(() => {
return this.clone().request()
})
)
}
} else { } else {
const name = generateName() const name = generateName()
console.log("Not found, saving now as "+name) console.log("Not found, saving now as "+name)

View File

@ -18,12 +18,13 @@ const cookies = {
} }
} }
function generateSetup(pageName, url, sizeName, cookiesName) { function generateSetup(pageName, url, sizeName, cookiesName, scrolls) {
return { return {
url: url, url: url,
filename: `${pageName}-${sizeName}-${cookiesName}`, filename: `${pageName}-${sizeName}-${cookiesName}`,
size: sizes[sizeName], size: sizes[sizeName],
cookies: cookies[cookiesName] cookies: cookies[cookiesName],
scrolls: scrolls
} }
} }
@ -31,6 +32,8 @@ module.exports = [
generateSetup("home", "/", "laptop", "default"), generateSetup("home", "/", "laptop", "default"),
generateSetup("settings", "/settings", "laptop", "default"), generateSetup("settings", "/settings", "laptop", "default"),
generateSetup("home", "/", "phone", "default"), generateSetup("home", "/", "phone", "default"),
generateSetup("settings", "/settings", "phone", "default"),
generateSetup("home", "/", "laptop", "ptc"), generateSetup("home", "/", "laptop", "ptc"),
generateSetup("settings", "/settings", "laptop", "ptc"), generateSetup("settings", "/settings", "laptop", "ptc"),
generateSetup("home", "/", "phone", "ptc"), generateSetup("home", "/", "phone", "ptc"),