mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-11-22 08:07:30 +00:00
Add user import and export scripts
This commit is contained in:
parent
d95f6950c4
commit
6d7e089dae
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,6 +7,7 @@ node_modules
|
|||||||
|
|
||||||
# Database stuff
|
# Database stuff
|
||||||
db/**/*.db*
|
db/**/*.db*
|
||||||
|
users_export.json
|
||||||
|
|
||||||
# Test stuff
|
# Test stuff
|
||||||
/coverage
|
/coverage
|
||||||
|
11
scripts/export_users.js
Executable file
11
scripts/export_users.js
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
const fs = require("fs").promises
|
||||||
|
const pj = require("path").join
|
||||||
|
const db = require("../src/lib/db")
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
const users = db.prepare("SELECT * FROM Users").all()
|
||||||
|
const targetDir = process.argv.slice(2).includes("--publish") ? "../src/site/html" : ".."
|
||||||
|
const target = pj(__dirname, targetDir, "users_export.json")
|
||||||
|
fs.writeFile(target, JSON.stringify(users), {encoding: "utf8"})
|
||||||
|
console.log(`Users exported to ${target}`)
|
||||||
|
})()
|
33
scripts/import_users.js
Normal file
33
scripts/import_users.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
const fs = require("fs").promises
|
||||||
|
const pj = require("path").join
|
||||||
|
const db = require("../src/lib/db")
|
||||||
|
const {request} = require("../src/lib/utils/request")
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
const target = process.argv[2]
|
||||||
|
if (!target) {
|
||||||
|
console.log("Provide the file or URL to import from on the command line.")
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
if (target.match(/^https?:\/\//)) {
|
||||||
|
var usersString = await request(target).then(res => res.text())
|
||||||
|
} else {
|
||||||
|
var usersString = await fs.readFile(target, {encoding: "utf8"})
|
||||||
|
}
|
||||||
|
/** @type {{username: string, user_id: string, created: number, updated: number, updated_version: number, biography: string, post_count: number, following_count: number, followed_by_count: number, external_url: string, full_name: string, is_private: number, is_verified: number, profile_pic_url: string}[]} */
|
||||||
|
const users = JSON.parse(usersString)
|
||||||
|
let newCount = 0
|
||||||
|
let overwrittenCount = 0
|
||||||
|
db.transaction(() => {
|
||||||
|
for (const user of users) {
|
||||||
|
const existing = db.prepare("SELECT user_id FROM Users WHERE username = ?").get(user.username)
|
||||||
|
db.prepare(
|
||||||
|
"REPLACE INTO Users (username, user_id, created, updated, updated_version, biography, post_count, following_count, followed_by_count, external_url, full_name, is_private, is_verified, profile_pic_url) VALUES "
|
||||||
|
+"(@username, @user_id, @created, @updated, @updated_version, @biography, @post_count, @following_count, @followed_by_count, @external_url, @full_name, @is_private, @is_verified, @profile_pic_url)"
|
||||||
|
).run(user)
|
||||||
|
if (existing) overwrittenCount++
|
||||||
|
else newCount++
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
console.log(`Imported ${users.length} entries (${newCount} new, ${overwrittenCount} overwritten)`)
|
||||||
|
})()
|
Loading…
Reference in New Issue
Block a user