1
0
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:
Cadence Ember 2020-06-22 02:04:15 +12:00
parent a7a9af8854
commit 81b2c77b98
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412
7 changed files with 111 additions and 54 deletions

View File

@ -106,6 +106,11 @@ let constants = {
collatedFiles: [] collatedFiles: []
}, },
default_user_settings: {
rewrite_youtube: "invidio.us",
rewrite_twitter: "nitter.net"
},
user_settings: [ user_settings: [
{ {
name: "language", name: "language",
@ -166,12 +171,12 @@ let constants = {
name: "rewrite_youtube", name: "rewrite_youtube",
default: "", default: "",
boolean: false, boolean: false,
replaceEmptyWithDefault: true replaceEmptyWithDefault: false
},{ },{
name: "rewrite_twitter", name: "rewrite_twitter",
default: "", default: "",
boolean: false, boolean: false,
replaceEmptyWithDefault: true replaceEmptyWithDefault: false
} }
], ],

View 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

View File

@ -1,11 +1,11 @@
const constants = require("../constants") const constants = require("../constants")
const {proxyProfilePic} = require("../utils/proxyurl")
const {structure} = require("../utils/structuretext")
const Timeline = require("./Timeline") 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) { constructor(data) {
super()
/** @type {import("../types").GraphUser} */ /** @type {import("../types").GraphUser} */
this.data = data this.data = data
this.fromReel = true this.fromReel = true
@ -17,25 +17,6 @@ class ReelUser {
this.cachedAt = Date.now() this.cachedAt = Date.now()
this.computeProxyProfilePic() 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 module.exports = ReelUser

View File

@ -1,14 +1,14 @@
const constants = require("../constants") const constants = require("../constants")
const {proxyProfilePic} = require("../utils/proxyurl")
const {structure} = require("../utils/structuretext")
const Timeline = require("./Timeline") 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 * @param {import("../types").GraphUser} data
*/ */
constructor(data) { constructor(data) {
super()
this.data = data this.data = data
this.following = data.edge_follow.count this.following = data.edge_follow.count
this.followedBy = data.edge_followed_by.count this.followedBy = data.edge_followed_by.count
@ -17,25 +17,6 @@ class User {
this.cachedAt = Date.now() this.cachedAt = Date.now()
this.computeProxyProfilePic() 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 module.exports = User

View File

@ -12,6 +12,8 @@ function addDefaults(input = {}) {
} else { } else {
if (setting.boolean) { if (setting.boolean) {
result[setting.name] = +(setting.default !== "") result[setting.name] = +(setting.default !== "")
} else if (setting.name in constants.default_user_settings) {
result[setting.name] = constants.default_user_settings[setting.name]
} else { } else {
result[setting.name] = setting.default result[setting.name] = setting.default
} }

View File

@ -6,10 +6,14 @@ mixin fieldset(name)
.fieldset-contents .fieldset-contents
block block
mixin input(id, description, placeholder, disabled) mixin input(id, description, placeholder, disabled, list)
.field-row .field-row
label.description(for=id)= description 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) mixin checkbox(id, description, label, disabled)
.field-row.checkbox-row .field-row.checkbox-row
@ -53,9 +57,26 @@ html
{value: "full", text: "Full"} {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) +checkbox("remove_trailing_hashtags", "Hide trailing hashtags", "Hide", false)

View File

@ -54,9 +54,10 @@ html
- const bio = user.getStructuredBio() - const bio = user.getStructuredBio()
if bio if bio
+display_structured(bio) +display_structured(bio)
if user.data.external_url - const userURL = user.getRewriteLink(settings)
if userURL
p.website p.website
a(href=user.data.external_url)= user.data.external_url a(href=userURL)= userURL
if user.posts != undefined if user.posts != undefined
div.profile-counter #[span(data-numberformat=user.posts).count #{numberFormat(user.posts)}] posts div.profile-counter #[span(data-numberformat=user.posts).count #{numberFormat(user.posts)}] posts
if followerCountsAvailable if followerCountsAvailable