Add experimental assistant feature

This commit is contained in:
Cadence Ember
2020-04-07 18:30:00 +12:00
parent 160fa7d843
commit b22028aaa4
10 changed files with 236 additions and 3 deletions

View File

@@ -78,7 +78,7 @@ module.exports = [
message: "This user doesn't exist.",
withInstancesLink: false
})
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN) {
} else if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN || error === constants.symbols.RATE_LIMITED) {
return {
statusCode: 503,
contentType: "text/html",

40
src/site/assistant.js Normal file
View File

@@ -0,0 +1,40 @@
const {Pinski} = require("pinski")
const {subdirs} = require("node-dir")
const constants = require("../lib/constants")
const passthrough = require("./passthrough")
const pinski = new Pinski({
port: constants.port,
relativeRoot: __dirname
})
;(async (err, dirs) => {
if (err) throw err
// need to check for and run db upgrades before anything starts using it
await require("../lib/utils/upgradedb")()
if (constants.tor.enabled) {
await require("../lib/utils/tor") // make sure tor state is known before going further
}
pinski.addAPIDir("assistant_api")
pinski.startServer()
pinski.enableWS()
require("pinski/plugins").setInstance(pinski)
Object.assign(passthrough, pinski.getExports())
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")
}
})()

View File

@@ -0,0 +1,75 @@
const constants = require("../../lib/constants")
const collectors = require("../../lib/collectors")
const db = require("../../lib/db")
function reply(statusCode, content) {
return {
statusCode: statusCode,
contentType: "application/json",
content: JSON.stringify(content)
}
}
module.exports = [
{
route: `/api/user`, methods: ["GET"], code: () => {
return Promise.resolve(reply(200, {
status: "ok",
generatedAt: Date.now(),
availableVersions: ["1"]
}))
}
},
{
route: `/api/user/v1/(${constants.external.username_regex})`, methods: ["GET"], code: async ({fill, url}) => {
function replyWithUserData(userData, type) {
return reply(200, {
status: "ok",
version: "1.0",
generatedAt: Date.now(),
data: {
type,
allow_user_from_reel: constants.allow_user_from_reel,
user: userData
}
})
}
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"))
} else {
return collectors.fetchUser(username, false).then(user => {
const type = user.constructor.name === "User" ? "user" : "reel"
return replyWithUserData({
username: user.data.username,
user_id: user.data.id,
biography: user.data.biography,
post_count: user.posts,
following_count: user.following,
followed_by_count: user.followedBy,
external_url: user.data.external_url,
full_name: user.data.full_name,
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, {
status: "fail",
version: "1.0",
generatedAt: Date.now(),
message: "Rate limited by Instagram.",
identifier: "RATE_LIMITED"
})
} else {
throw error
}
})
}
}
}
]

View File

@@ -1,6 +1,7 @@
const {instance, pugCache, wss} = require("./passthrough")
const {userRequestCache, timelineEntryCache, history} = require("../lib/collectors")
const constants = require("../lib/constants")
const collectors = require("../lib/collectors")
const util = require("util")
const repl = require("repl")
const vm = require("vm")

View File

@@ -34,7 +34,6 @@ subdirs("pug", async (err, dirs) => {
pinski.addAPIDir("api")
pinski.startServer()
pinski.enableWS()
require("pinski/plugins").setInstance(pinski)