improved lang, move palette code

This commit is contained in:
dangered wolf 2022-07-14 12:46:59 -04:00
parent 4f1d659a3d
commit afde8f2f2e
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
2 changed files with 54 additions and 25 deletions

28
src/palette.ts Normal file
View file

@ -0,0 +1,28 @@
import { Constants } from "./constants";
// https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
const componentToHex = (component: number) => {
let hex = component.toString(16);
return hex.length === 1 ? "0" + hex : hex;
}
const rgbToHex = (r: number, g: number, b: number) =>
`#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
export const colorFromPalette = (palette: MediaPlaceholderColor[]) => {
for (let i = 0; i < palette.length; i++) {
const rgb = palette[i].rgb;
// We need vibrant colors, grey backgrounds won't do!
if (rgb.red + rgb.green + rgb.blue < 120) {
continue;
}
return rgbToHex(rgb.red, rgb.green, rgb.blue);
}
return Constants.DEFAULT_COLOR;
}

View file

@ -1,29 +1,15 @@
import { Constants } from "./constants"; import { Constants } from "./constants";
import { fetchUsingGuest } from "./fetch"; import { fetchUsingGuest } from "./fetch";
import { Html } from "./html"; import { Html } from "./html";
import { colorFromPalette } from "./palette";
import { renderPoll } from "./poll"; import { renderPoll } from "./poll";
import { rgbToHex } from "./utils";
const colorFromPalette = (palette: MediaPlaceholderColor[]) => {
for (let i = 0; i < palette.length; i++) {
const rgb = palette[i].rgb;
// We need vibrant colors, grey backgrounds won't do!
if (rgb.red + rgb.green + rgb.blue < 120) {
continue;
}
return rgbToHex(rgb.red, rgb.green, rgb.blue);
}
return Constants.DEFAULT_COLOR;
}
export const handleStatus = async (handle: string, id: string, mediaNumber?: number): Promise<string> => { export const handleStatus = async (handle: string, id: string, mediaNumber?: number): Promise<string> => {
const tweet = await fetchUsingGuest(id); const tweet = await fetchUsingGuest(id);
console.log(tweet); console.log(tweet);
// Try to deep link to mobile apps, just like Twitter does /* Try to deep link to mobile apps, just like Twitter does.
No idea if this actually works.*/
let headers: string[] = [ let headers: string[] = [
`<meta property="og:site_name" content="Twitter"/>`, `<meta property="og:site_name" content="Twitter"/>`,
`<meta property="fb:app_id" content="2231777543"/>`, `<meta property="fb:app_id" content="2231777543"/>`,
@ -54,6 +40,8 @@ export const handleStatus = async (handle: string, id: string, mediaNumber?: num
const screenName = user?.screen_name || ''; const screenName = user?.screen_name || '';
const name = user?.name || ''; const name = user?.name || '';
const mediaList = tweet.extended_entities?.media || tweet.entities?.media || [];
let authorText = 'Twitter'; let authorText = 'Twitter';
// This is used to chop off the end if it's like pic.twitter.com or something // This is used to chop off the end if it's like pic.twitter.com or something
@ -94,9 +82,7 @@ export const handleStatus = async (handle: string, id: string, mediaNumber?: num
`<meta content="${text}" property="og:description"/>` `<meta content="${text}" property="og:description"/>`
); );
} else { } else {
let media = tweet.extended_entities?.media || tweet.entities?.media || []; let firstMedia = mediaList[0];
let firstMedia = media[0];
let palette = firstMedia?.ext_media_color?.palette; let palette = firstMedia?.ext_media_color?.palette;
let colorOverride: string = Constants.DEFAULT_COLOR; let colorOverride: string = Constants.DEFAULT_COLOR;
@ -145,31 +131,46 @@ export const handleStatus = async (handle: string, id: string, mediaNumber?: num
} }
} }
// You can specify a specific photo in the URL let actualMediaNumber = 1;
if (typeof mediaNumber === "number" && media[mediaNumber]) {
processMedia(media[mediaNumber]); /* You can specify a specific photo in the URL and we'll pull the correct one,
otherwise it falls back to first */
if (typeof mediaNumber === "number" && mediaList[mediaNumber]) {
actualMediaNumber = mediaNumber;
processMedia(mediaList[mediaNumber]);
} else { } else {
// I wish Telegram respected multiple photos in a tweet /* I wish Telegram respected multiple photos in a tweet,
and that Discord could do the same for 3rd party providers like us */
// media.forEach(media => processMedia(media)); // media.forEach(media => processMedia(media));
processMedia(firstMedia); processMedia(firstMedia);
} }
if (mediaList.length > 1) {
authorText = `Photo ${actualMediaNumber} of ${mediaList.length}`;
}
headers.push( headers.push(
`<meta content="${name} (@${screenName})" property="og:title"/>`, `<meta content="${name} (@${screenName})" property="og:title"/>`,
`<meta content="${text}" property="og:description"/>` `<meta content="${text}" property="og:description"/>`
); );
} }
/* */
if (tweet.in_reply_to_screen_name) { if (tweet.in_reply_to_screen_name) {
authorText = `↪ Replying to @${tweet.in_reply_to_screen_name}`; authorText = `↪ Replying to @${tweet.in_reply_to_screen_name}`;
} }
/* The additional oembed is pulled by Discord to enable improved embeds.
Telegram does not use this. */
headers.push(`<link rel="alternate" href="https://pxtwitter.com/owoembed?text=${encodeURIComponent(authorText)}&status=${encodeURIComponent(id)}&author=${encodeURIComponent(user?.screen_name || '')}" type="application/json+oembed" title="${name}">`) headers.push(`<link rel="alternate" href="https://pxtwitter.com/owoembed?text=${encodeURIComponent(authorText)}&status=${encodeURIComponent(id)}&author=${encodeURIComponent(user?.screen_name || '')}" type="application/json+oembed" title="${name}">`)
console.log(JSON.stringify(tweet)) console.log(JSON.stringify(tweet))
/* When dealing with a Tweet of unknown lang, fall back to en */
let lang = tweet.lang === 'unk' ? 'en' : tweet.lang || 'en';
return Html.BASE_HTML.format({ return Html.BASE_HTML.format({
lang: `lang="${tweet.lang || 'en'}"`, lang: `lang="${lang}"`,
headers: headers.join('') headers: headers.join('')
}); });
}; };