From 046383f56bb88652cbd83c78bf9caff14cb875a5 Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Mon, 25 Jul 2022 19:41:50 -0400 Subject: [PATCH] Re-add polls --- src/card.ts | 51 ++++------------------------------------------- src/pollHelper.ts | 42 ++++++++++++++++++++++++++++++++++++++ src/status.ts | 36 ++++++++++++++++++++++++++------- src/types.d.ts | 1 + 4 files changed, 76 insertions(+), 54 deletions(-) create mode 100644 src/pollHelper.ts diff --git a/src/card.ts b/src/card.ts index 25e04df..d79a016 100644 --- a/src/card.ts +++ b/src/card.ts @@ -1,47 +1,4 @@ -import { Strings } from './strings'; - -let barLength = 36; - -export const calculateTimeLeft = (date: Date) => { - const now = new Date(); - const diff = date.getTime() - now.getTime(); - const days = Math.floor(diff / (1000 * 60 * 60 * 24)); - const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); - const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); - const seconds = Math.floor((diff % (1000 * 60)) / 1000); - return { days, hours, minutes, seconds }; -}; - -export const calculateTimeLeftString = (date: Date) => { - const { days, hours, minutes, seconds } = calculateTimeLeft(date); - const daysString = - days > 0 - ? `${days} ${days === 1 ? Strings.SINGULAR_DAY_LEFT : Strings.PLURAL_DAYS_LEFT}` - : ''; - const hoursString = - hours > 0 - ? `${hours} ${hours === 1 ? Strings.SINGULAR_HOUR_LEFT : Strings.PLURAL_HOURS_LEFT}` - : ''; - const minutesString = - minutes > 0 - ? `${minutes} ${ - minutes === 1 ? Strings.SINGULAR_MINUTE_LEFT : Strings.PLURAL_MINUTES_LEFT - }` - : ''; - const secondsString = - seconds > 0 - ? `${seconds} ${ - seconds === 1 ? Strings.SINGULAR_SECOND_LEFT : Strings.PLURAL_SECONDS_LEFT - }` - : ''; - return ( - daysString || - hoursString || - minutesString || - secondsString || - Strings.FINAL_POLL_RESULTS - ); -}; +import { calculateTimeLeftString } from './pollHelper'; export const renderCard = async ( card: TweetCard @@ -54,7 +11,6 @@ export const renderCard = async ( // Telegram's bars need to be a lot smaller to fit its bubbles let choices: { [label: string]: number } = {}; let totalVotes = 0; - let timeLeft = ''; if (typeof values !== 'undefined') { /* TODO: make poll code cleaner */ @@ -63,11 +19,12 @@ export const renderCard = async ( typeof values.choice2_count !== 'undefined' ) { let poll = {} as APIPoll; - poll.ends_at = values.end_datetime_utc?.string_value || ''; if (typeof values.end_datetime_utc !== 'undefined') { + poll.ends_at = values.end_datetime_utc.string_value || ''; + const date = new Date(values.end_datetime_utc.string_value); - timeLeft = calculateTimeLeftString(date); + poll.time_left_en = calculateTimeLeftString(date); } choices[values.choice1_label?.string_value || ''] = parseInt( values.choice1_count.string_value diff --git a/src/pollHelper.ts b/src/pollHelper.ts new file mode 100644 index 0000000..898a945 --- /dev/null +++ b/src/pollHelper.ts @@ -0,0 +1,42 @@ +import { Strings } from './strings'; + +export const calculateTimeLeft = (date: Date) => { + const now = new Date(); + const diff = date.getTime() - now.getTime(); + const days = Math.floor(diff / (1000 * 60 * 60 * 24)); + const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((diff % (1000 * 60)) / 1000); + return { days, hours, minutes, seconds }; +}; + +export const calculateTimeLeftString = (date: Date) => { + const { days, hours, minutes, seconds } = calculateTimeLeft(date); + const daysString = + days > 0 + ? `${days} ${days === 1 ? Strings.SINGULAR_DAY_LEFT : Strings.PLURAL_DAYS_LEFT}` + : ''; + const hoursString = + hours > 0 + ? `${hours} ${hours === 1 ? Strings.SINGULAR_HOUR_LEFT : Strings.PLURAL_HOURS_LEFT}` + : ''; + const minutesString = + minutes > 0 + ? `${minutes} ${ + minutes === 1 ? Strings.SINGULAR_MINUTE_LEFT : Strings.PLURAL_MINUTES_LEFT + }` + : ''; + const secondsString = + seconds > 0 + ? `${seconds} ${ + seconds === 1 ? Strings.SINGULAR_SECOND_LEFT : Strings.PLURAL_SECONDS_LEFT + }` + : ''; + return ( + daysString || + hoursString || + minutesString || + secondsString || + Strings.FINAL_POLL_RESULTS + ); +}; diff --git a/src/status.ts b/src/status.ts index be9bcd4..712a402 100644 --- a/src/status.ts +++ b/src/status.ts @@ -1,13 +1,10 @@ import { Constants } from './constants'; -import { linkFixer } from './linkFixer'; -import { colorFromPalette } from './palette'; -import { renderCard } from './card'; import { handleQuote } from './quote'; import { sanitizeText } from './utils'; import { Strings } from './strings'; -import { handleMosaic } from './mosaic'; import { getAuthorText } from './author'; import { statusAPI } from './api'; +import { calculateTimeLeftString } from './pollHelper'; export const returnError = (error: string): StatusResponse => { return { @@ -104,7 +101,11 @@ export const handleStatus = async ( const { photos } = tweet.media; let photo = photos[mediaNumber || 0]; - if (typeof mediaNumber !== 'number' && tweet.media.mosaic) { + if ( + typeof mediaNumber !== 'number' && + tweet.media.mosaic && + userAgent?.indexOf('Telegram') === -1 + ) { photo = { url: userAgent?.indexOf('Telegram') !== -1 @@ -159,6 +160,29 @@ export const handleStatus = async ( } let siteName = Constants.BRANDING_NAME; + let newText = tweet.text; + + if (tweet.poll) { + const { poll } = tweet; + let barLength = 34; + let str = ''; + + if (userAgent?.indexOf('Telegram') !== -1) { + barLength = 24; + } + + tweet.poll.choices.forEach(choice => { + // render bar + const bar = 'โ–ˆ'.repeat((choice.percentage / 100) * barLength); + str += `${bar} +${choice.label}โ€€โ€€(${choice.percentage}%) +`; + }); + + str += `\n${poll.total_votes} votes ยท ${poll.time_left_en}`; + + newText += `\n\n${str}`; + } if (!tweet.media?.video && !tweet.media?.photos) { headers.push( @@ -171,8 +195,6 @@ export const handleStatus = async ( ); } - let newText = tweet.text; - if (api.tweet?.translation) { const { translation } = api.tweet; diff --git a/src/types.d.ts b/src/types.d.ts index 46d23f4..6be8250 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -54,6 +54,7 @@ interface APIPoll { choices: APIPollChoice[]; total_votes: number; ends_at: string; + time_left_en: string; } interface APIPhoto {