mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2025-12-15 19:05:09 +00:00
Hopefully the final assistants changes
This commit is contained in:
@@ -7,7 +7,7 @@ module.exports = [
|
||||
{route: `/u/(${constants.external.username_regex})/(rss|atom)\\.xml`, methods: ["GET"], code: ({fill}) => {
|
||||
if (constants.settings.rss_enabled) {
|
||||
const kind = fill[1]
|
||||
return fetchUser(fill[0], true).then(async user => {
|
||||
return fetchUser(fill[0], constants.symbols.fetch_context.RSS).then(async user => {
|
||||
const feed = await user.timeline.fetchFeed()
|
||||
if (kind === "rss") {
|
||||
var data = {
|
||||
|
||||
@@ -63,7 +63,7 @@ module.exports = [
|
||||
}
|
||||
|
||||
const params = url.searchParams
|
||||
return fetchUser(fill[0], false).then(async user => {
|
||||
return fetchUser(fill[0]).then(async user => {
|
||||
const page = +params.get("page")
|
||||
if (typeof page === "number" && !isNaN(page) && page >= 1) {
|
||||
await user.timeline.fetchUpToPage(page - 1)
|
||||
@@ -97,7 +97,7 @@ module.exports = [
|
||||
},
|
||||
{
|
||||
route: `/fragment/user/(${constants.external.username_regex})/(\\d+)`, methods: ["GET"], code: async ({url, fill}) => {
|
||||
return fetchUser(fill[0], false).then(async user => {
|
||||
return fetchUser(fill[0]).then(async user => {
|
||||
const pageNumber = +fill[1]
|
||||
const pageIndex = pageNumber - 1
|
||||
await user.timeline.fetchUpToPage(pageIndex)
|
||||
|
||||
@@ -29,11 +29,6 @@ const pinski = new Pinski({
|
||||
|
||||
console.log("Assistant started")
|
||||
|
||||
if (constants.allow_user_from_reel !== "never") {
|
||||
constants.allow_user_from_reel = "never"
|
||||
console.log(`[!] You are running the assistant, so \`constants.allow_user_from_reel\` has been set to "never" for this session.`)
|
||||
}
|
||||
|
||||
if (process.stdin.isTTY || process.argv.includes("--enable-repl")) {
|
||||
require("./repl")
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const crypto = require("crypto")
|
||||
const constants = require("../../lib/constants")
|
||||
const collectors = require("../../lib/collectors")
|
||||
const db = require("../../lib/db")
|
||||
@@ -22,27 +23,49 @@ module.exports = [
|
||||
},
|
||||
{
|
||||
route: `/api/user/v1/(${constants.external.username_regex})`, methods: ["GET"], code: async ({fill, url}) => {
|
||||
function replyWithUserData(userData, type) {
|
||||
function replyWithUserData(userData) {
|
||||
return reply(200, {
|
||||
status: "ok",
|
||||
version: "1.0",
|
||||
generatedAt: Date.now(),
|
||||
data: {
|
||||
type,
|
||||
allow_user_from_reel: constants.allow_user_from_reel,
|
||||
user: userData
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (constants.as_assistant.require_key) {
|
||||
if (url.searchParams.has("key")) {
|
||||
const inputKey = url.searchParams.get("key")
|
||||
if (!constants.as_assistant.keys.some(key => inputKey === key)) {
|
||||
return reply(401, {
|
||||
status: "fail",
|
||||
version: "1.0",
|
||||
generatedAt: Date.now(),
|
||||
message: "The authentication key provided is not in the list of allowed keys.",
|
||||
fields: ["q:key"],
|
||||
identifier: "NOT_AUTHENTICATED"
|
||||
})
|
||||
}
|
||||
} else {
|
||||
return reply(401, {
|
||||
status: "fail",
|
||||
version: "1.0",
|
||||
generatedAt: Date.now(),
|
||||
message: "This endpoint requires authentication. If you have a key, specify it with the `key` query parameter.",
|
||||
fields: ["q:key"],
|
||||
identifier: "NOT_AUTHENTICATED"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const username = fill[0]
|
||||
const saved = db.prepare("SELECT username, user_id, updated_version, biography, post_count, following_count, followed_by_count, external_url, full_name, is_private, is_verified, profile_pic_url FROM Users WHERE username = ?").get(username)
|
||||
if (saved && saved.updated_version >= 2) { // suitable data is already saved
|
||||
delete saved.updated_version
|
||||
return Promise.resolve(replyWithUserData(saved, "user"))
|
||||
return Promise.resolve(replyWithUserData(saved))
|
||||
} else {
|
||||
return collectors.fetchUser(username, false).then(user => {
|
||||
const type = user.constructor.name === "User" ? "user" : "reel"
|
||||
return collectors.fetchUser(username, constants.symbols.fetch_context.ASSISTANT).then(user => {
|
||||
return replyWithUserData({
|
||||
username: user.data.username,
|
||||
user_id: user.data.id,
|
||||
@@ -55,7 +78,7 @@ module.exports = [
|
||||
is_private: user.data.is_private,
|
||||
is_verified: user.data.is_verified,
|
||||
profile_pic_url: user.data.profile_pic_url
|
||||
}, type)
|
||||
})
|
||||
}).catch(error => {
|
||||
if (error === constants.symbols.RATE_LIMITED || error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN) {
|
||||
return reply(503, {
|
||||
|
||||
@@ -33,6 +33,12 @@ subdirs("pug", async (err, dirs) => {
|
||||
}
|
||||
|
||||
pinski.addAPIDir("api")
|
||||
|
||||
if (constants.as_assistant.enabled) {
|
||||
console.log("Assistant API enabled")
|
||||
pinski.addAPIDir("assistant_api")
|
||||
}
|
||||
|
||||
pinski.startServer()
|
||||
|
||||
require("pinski/plugins").setInstance(pinski)
|
||||
|
||||
Reference in New Issue
Block a user