mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2024-11-22 08:07:30 +00:00
Add youtube/twitter rewrite (closes #22)
This commit is contained in:
parent
a7a9af8854
commit
81b2c77b98
@ -106,6 +106,11 @@ let constants = {
|
||||
collatedFiles: []
|
||||
},
|
||||
|
||||
default_user_settings: {
|
||||
rewrite_youtube: "invidio.us",
|
||||
rewrite_twitter: "nitter.net"
|
||||
},
|
||||
|
||||
user_settings: [
|
||||
{
|
||||
name: "language",
|
||||
@ -166,12 +171,12 @@ let constants = {
|
||||
name: "rewrite_youtube",
|
||||
default: "",
|
||||
boolean: false,
|
||||
replaceEmptyWithDefault: true
|
||||
replaceEmptyWithDefault: false
|
||||
},{
|
||||
name: "rewrite_twitter",
|
||||
default: "",
|
||||
boolean: false,
|
||||
replaceEmptyWithDefault: true
|
||||
replaceEmptyWithDefault: false
|
||||
}
|
||||
],
|
||||
|
||||
|
66
src/lib/structures/BaseUser.js
Normal file
66
src/lib/structures/BaseUser.js
Normal file
@ -0,0 +1,66 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
|
||||
const rewriters = {
|
||||
rewrite_youtube: ["youtube.com", "www.youtube.com", "youtu.be"],
|
||||
rewrite_twitter: ["twitter.com", "www.twitter.com", "twtr.cm"]
|
||||
}
|
||||
|
||||
class BaseUser {
|
||||
constructor() {
|
||||
/** @type {import("../types").GraphUser} */
|
||||
this.data
|
||||
/** @type {number} */
|
||||
this.cachedAt
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} settings
|
||||
* @param {string} settings.rewrite_youtube
|
||||
* @param {string} settings.rewrite_twitter
|
||||
*/
|
||||
getRewriteLink(settings) {
|
||||
if (!this.data.external_url) return null
|
||||
let url
|
||||
try {
|
||||
url = new URL(this.data.external_url)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
for (const key of Object.keys(rewriters)) { // for each thing we can rewrite
|
||||
if (key in settings) { // if the settings want to replace it
|
||||
if (rewriters[key].includes(url.host)) { // if the url matches this filter
|
||||
if (settings[key].includes("://")) {
|
||||
[url.protocol, url.host] = settings[key].split("//")
|
||||
} else {
|
||||
url.host = settings[key]
|
||||
url.protocol = "https:"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseUser
|
@ -1,11 +1,11 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
const Timeline = require("./Timeline")
|
||||
require("../testimports")(constants, Timeline)
|
||||
const BaseUser = require("./BaseUser")
|
||||
require("../testimports")(constants, Timeline, BaseUser)
|
||||
|
||||
class ReelUser {
|
||||
class ReelUser extends BaseUser {
|
||||
constructor(data) {
|
||||
super()
|
||||
/** @type {import("../types").GraphUser} */
|
||||
this.data = data
|
||||
this.fromReel = true
|
||||
@ -17,25 +17,6 @@ class ReelUser {
|
||||
this.cachedAt = Date.now()
|
||||
this.computeProxyProfilePic()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReelUser
|
||||
|
@ -1,14 +1,14 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
const Timeline = require("./Timeline")
|
||||
require("../testimports")(constants, Timeline)
|
||||
const BaseUser = require("./BaseUser")
|
||||
require("../testimports")(constants, Timeline, BaseUser)
|
||||
|
||||
class User {
|
||||
class User extends BaseUser {
|
||||
/**
|
||||
* @param {import("../types").GraphUser} data
|
||||
*/
|
||||
constructor(data) {
|
||||
super()
|
||||
this.data = data
|
||||
this.following = data.edge_follow.count
|
||||
this.followedBy = data.edge_followed_by.count
|
||||
@ -17,25 +17,6 @@ class User {
|
||||
this.cachedAt = Date.now()
|
||||
this.computeProxyProfilePic()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User
|
||||
|
@ -12,6 +12,8 @@ function addDefaults(input = {}) {
|
||||
} else {
|
||||
if (setting.boolean) {
|
||||
result[setting.name] = +(setting.default !== "")
|
||||
} else if (setting.name in constants.default_user_settings) {
|
||||
result[setting.name] = constants.default_user_settings[setting.name]
|
||||
} else {
|
||||
result[setting.name] = setting.default
|
||||
}
|
||||
|
@ -6,10 +6,14 @@ mixin fieldset(name)
|
||||
.fieldset-contents
|
||||
block
|
||||
|
||||
mixin input(id, description, placeholder, disabled)
|
||||
mixin input(id, description, placeholder, disabled, list)
|
||||
.field-row
|
||||
label.description(for=id)= description
|
||||
input(type="text" id=id name=id value=settings[id] placeholder=placeholder disabled=disabled)
|
||||
input(type="text" id=id name=id value=settings[id] placeholder=placeholder disabled=disabled list=`${id}-list`)
|
||||
if list
|
||||
datalist(id=`${id}-list`)
|
||||
each item in list
|
||||
option(value=item)
|
||||
|
||||
mixin checkbox(id, description, label, disabled)
|
||||
.field-row.checkbox-row
|
||||
@ -53,9 +57,26 @@ html
|
||||
{value: "full", text: "Full"}
|
||||
])
|
||||
|
||||
+input("rewrite_youtube", "Rewrite YouTube domain", "youtube.com", true)
|
||||
+input("rewrite_youtube", "Rewrite YouTube domain", constants.default_user_settings.rewrite_youtube, false, [
|
||||
"invidio.us",
|
||||
"invidious.snopyta.org",
|
||||
"invidious.13ad.de",
|
||||
"watch.nettohikari.com",
|
||||
"invidious.fdn.fr",
|
||||
"yewtu.be"
|
||||
])
|
||||
|
||||
+input("rewrite_twitter", "Rewrite Twitter domain", "twitter.com", true)
|
||||
+input("rewrite_twitter", "Rewrite Twitter domain", constants.default_user_settings.rewrite_twitter, false, [
|
||||
"nitter.net",
|
||||
"nitter.snopyta.org",
|
||||
"nitter.pussthecat.org",
|
||||
"nitter.42l.fr",
|
||||
"nitter.mastodont.cat",
|
||||
"nitter.tedomum.net",
|
||||
"nitter.cattube.org",
|
||||
"nitter.fdn.fr",
|
||||
"nitter.1d4.us"
|
||||
])
|
||||
|
||||
+checkbox("remove_trailing_hashtags", "Hide trailing hashtags", "Hide", false)
|
||||
|
||||
|
@ -54,9 +54,10 @@ html
|
||||
- const bio = user.getStructuredBio()
|
||||
if bio
|
||||
+display_structured(bio)
|
||||
if user.data.external_url
|
||||
- const userURL = user.getRewriteLink(settings)
|
||||
if userURL
|
||||
p.website
|
||||
a(href=user.data.external_url)= user.data.external_url
|
||||
a(href=userURL)= userURL
|
||||
if user.posts != undefined
|
||||
div.profile-counter #[span(data-numberformat=user.posts).count #{numberFormat(user.posts)}] posts
|
||||
if followerCountsAvailable
|
||||
|
Loading…
Reference in New Issue
Block a user