From 7acb21f613d319d544a7d9b9a813d383b87552f8 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 6 May 2022 16:05:37 +1200 Subject: [PATCH] User pages now count for quota, as it should be. --- src/lib/collectors.js | 4 +++- src/site/api/routes.js | 13 +++++++++++-- src/site/html/static/js/pagination.js | 8 ++++++-- src/site/pug/quota_reached.pug | 3 +++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/lib/collectors.js b/src/lib/collectors.js index e5245a2..f2f3f48 100644 --- a/src/lib/collectors.js +++ b/src/lib/collectors.js @@ -96,7 +96,9 @@ function fetchUserFromHTML(username) { } } } + let quotaUsed = 0 return userRequestCache.getOrFetch("user/"+username, false, true, () => { + quotaUsed++ return switcher.request("user_html", `https://www.instagram.com/${username}/feed/`, async res => { if (res.status === 301) throw constants.symbols.ENDPOINT_OVERRIDDEN if (res.status === 302) throw constants.symbols.INSTAGRAM_DEMANDS_LOGIN @@ -153,7 +155,7 @@ function fetchUserFromHTML(username) { } throw error }) - }).then(user => ({user, quotaUsed: 0})) + }).then(user => ({user, quotaUsed})) } /** diff --git a/src/site/api/routes.js b/src/site/api/routes.js index b7fdda4..c1d3f9e 100644 --- a/src/site/api/routes.js +++ b/src/site/api/routes.js @@ -13,6 +13,16 @@ function getPageTitle(post) { return (post.getCaptionIntroduction() || `Post from @${post.getBasicOwner().username}`) + " | Bibliogram" } +async function getUserAndQuota(req, username) { + if (quota.remaining(req) === 0) { + throw constants.symbols.QUOTA_REACHED + } + + const {user, quotaUsed} = await fetchUser(username) + const remaining = quota.add(req, quotaUsed) + return {user, remaining} +} + function getPostAndQuota(req, shortcode) { if (quota.remaining(req) === 0) { throw constants.symbols.QUOTA_REACHED @@ -101,8 +111,7 @@ module.exports = [ throw constants.symbols.QUOTA_REACHED } - const {user, quotaUsed} = await fetchUser(username) - let remaining = quota.add(req, quotaUsed) + let {user, remaining} = await getUserAndQuota(req, username) const selectedTimeline = user[type] let pageNumber = +params.get("page") diff --git a/src/site/html/static/js/pagination.js b/src/site/html/static/js/pagination.js index dc76b48..12302ed 100644 --- a/src/site/html/static/js/pagination.js +++ b/src/site/html/static/js/pagination.js @@ -79,8 +79,12 @@ class NextPage extends FreezeWidth { this.freeze(this.element.getAttribute("data-loading-text")) const type = this.element.getAttribute("data-type") - return fetch(`/fragment/user/${this.element.getAttribute("data-username")}/${this.nextPageNumber}?type=${type}`).then(res => res.text()).then(text => { - quota.change(-1) + return fetch(`/fragment/user/${this.element.getAttribute("data-username")}/${this.nextPageNumber}?type=${type}`).then(res => { + if (res.status === 200) { + quota.change(-1) + } + return res.text() + }).then(text => { q("#next-page-container").remove() this.observer.disconnect() q("#timeline").insertAdjacentHTML("beforeend", text) diff --git a/src/site/pug/quota_reached.pug b/src/site/pug/quota_reached.pug index 85b1e49..95a84b5 100644 --- a/src/site/pug/quota_reached.pug +++ b/src/site/pug/quota_reached.pug @@ -19,4 +19,7 @@ html | To prevent bot abuse, all users on proxy networks share the same request quota. | | + | If you are automatically collecting data from Bibliogram, please do not! + | #[a(href="https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Installing%20%28basic%29.md") You can easily run your own copy with no quota.] / #[a(href="https://cadence.moe/contact") Get in touch if you want help? <3] + | |