1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2025-12-15 19:05:09 +00:00

Rewrite feeds

This commit is contained in:
Cadence Fish
2020-02-18 13:39:20 +13:00
parent b10432aa38
commit 5201a6612b
16 changed files with 118 additions and 65 deletions

View File

@@ -13,6 +13,10 @@ const userRequestCache = new UserRequestCache(constants.caching.resource_cache_t
const timelineEntryCache = new TtlCache(constants.caching.resource_cache_time)
const history = new RequestHistory(["user", "timeline", "post", "reel"])
/**
* @param {string} username
* @param {boolean} isRSS
*/
async function fetchUser(username, isRSS) {
let mode = constants.allow_user_from_reel
if (mode === "preferForRSS") {
@@ -38,6 +42,10 @@ async function fetchUser(username, isRSS) {
}
}
/**
* @param {string} username
* @returns {Promise<import("./structures/User")>}
*/
function fetchUserFromHTML(username) {
return userRequestCache.getOrFetch("user/"+username, false, true, () => {
return switcher.request("user_html", `https://www.instagram.com/${username}/`, async res => {
@@ -72,6 +80,11 @@ function fetchUserFromHTML(username) {
})
}
/**
* @param {string} userID
* @param {string} username
* @returns {Promise<import("./structures/ReelUser")>}
*/
function fetchUserFromCombined(userID, username) {
// Fetch basic user information
const p = new URLSearchParams()

View File

@@ -6,7 +6,7 @@
let constants = {
// Things that server owners _should_ change!
website_origin: "http://localhost:10407",
website_origin: "http://localhost:10407", // Protocol and domain that this instance is hosted on. Do NOT include a trailing slash.
has_privacy_policy: false, // You MUST read /src/site/pug/privacy.pug.template before changing this!
// Things that server owners _could_ change if they want to.

View File

@@ -13,11 +13,16 @@ class ReelUser {
this.following = 0
this.followedBy = 0
this.posts = 0
/** @type {import("./Timeline")} */
this.timeline = new Timeline(this)
this.cachedAt = Date.now()
this.proxyProfilePicture = proxyImage(this.data.profile_pic_url)
}
getStructuredBio() {
return null
}
getTtl(scale = 1) {
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
const ttl = expiresAt - Date.now()

View File

@@ -1,4 +1,4 @@
const RSS = require("rss")
const {Feed} = require("feed")
const constants = require("../constants")
const config = require("../../../config")
const TimelineEntry = require("./TimelineEntry")
@@ -54,18 +54,27 @@ class Timeline {
}
async fetchFeed() {
const feed = new RSS({
title: `@${this.user.data.username}`,
feed_url: `${config.website_origin}/u/${this.user.data.username}/rss.xml`,
site_url: config.website_origin,
// we likely cannot use full_name here - reel fallback would make the feed title inconsistent, leading to confusing experience
const usedName = `@${this.user.data.username}`
const feed = new Feed({
title: usedName,
description: this.user.data.biography,
image_url: config.website_origin+this.user.proxyProfilePicture,
pubDate: new Date(this.user.cachedAt),
ttl: this.user.getTtl(1000*60) // scale to minute
id: `bibliogram:user/${this.user.data.username}`,
link: `${constants.website_origin}/u/${this.user.data.username}`,
feedLinks: {
rss: `${constants.website_origin}/u/${this.user.data.username}/rss.xml`,
atom: `${constants.website_origin}/u/${this.user.data.username}/atom.xml`
},
image: constants.website_origin+this.user.proxyProfilePicture,
updated: new Date(this.user.cachedAt),
author: {
name: usedName,
link: `${constants.website_origin}/u/${this.user.data.username}`
}
})
const page = this.pages[0] // only get posts from first page
await Promise.all(page.map(item =>
item.fetchFeedData().then(feedData => feed.item(feedData))
item.fetchFeedData().then(feedData => feed.addItem(feedData))
))
return feed
}

View File

@@ -217,6 +217,9 @@ class TimelineEntry extends TimelineBaseMethods {
else return this.update().then(() => this.getVideoUrlP())
}
/**
* @returns {Promise<import("feed/src/typings/index").Item>}
*/
async fetchFeedData() {
const children = await this.fetchChildren()
return {
@@ -230,10 +233,10 @@ class TimelineEntry extends TimelineBaseMethods {
height: child.data.dimensions.height
}))
}),
author: this.data.owner.username,
url: `${constants.website_origin}/p/${this.data.shortcode}`,
guid: `${constants.website_origin}/p/${this.data.shortcode}`, // Is it wise to keep the origin in here? The same post would have a different ID from different servers.
date: new Date(this.data.taken_at_timestamp*1000)
link: `${constants.website_origin}/p/${this.data.shortcode}`,
id: `bibliogram:post/${this.data.shortcode}`, // Is it wise to keep the origin in here? The same post would have a different ID from different servers.
published: new Date(this.data.taken_at_timestamp*1000), // first published date
date: new Date(this.data.taken_at_timestamp*1000) // last modified date
/*
Readers should display the description as HTML rather than using the media enclosure.
enclosure: {

View File

@@ -3,7 +3,7 @@ const fetch = require("node-fetch").default
function request(url, options = {}, settings = {}) {
if (settings.statusLine === undefined) settings.statusLine = "OUT"
if (settings.log === undefined) settings.log = true
if (settings.log) console.log(`-> [${settings.statusLine}] ${url}`) // todo: make more like pinski?
if (settings.log) console.log(` -> [${settings.statusLine}] ${url}`) // todo: make more like pinski?
// @ts-ignore
return fetch(url, Object.assign({
headers: {