Skip to content

Commit

Permalink
Ran Prettier again
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeredwolf committed Jul 14, 2022
1 parent 071d9bb commit 824e44d
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
@@ -1,7 +1,7 @@
{
"singleQuote": true,
"semi": true,
"trailingComma": "es5",
"trailingComma": "none",
"tabWidth": 2,
"printWidth": 90,
"arrowParens": "avoid",
Expand Down
6 changes: 3 additions & 3 deletions src/constants.ts
Expand Up @@ -18,7 +18,7 @@ export const Constants = {
'include_ext_media_color=true',
'include_ext_media_availability=true',
'include_ext_sensitive_media_warning=true',
'simple_quoted_tweet=true',
'simple_quoted_tweet=true'
].join('&'),
BASE_HEADERS: {
'sec-ch-ua': `".Not/A)Brand";v="99", "Google Chrome";v="${fakeChromeVersion}", "Chromium";v="${fakeChromeVersion}"`,
Expand All @@ -36,11 +36,11 @@ export const Constants = {
'Sec-Fetch-Dest': `empty`,
'Referer': `https://twitter.com/`,
'Accept-Encoding': `gzip, deflate, br`,
'Accept-Language': `en`,
'Accept-Language': `en`
},
RESPONSE_HEADERS: {
'content-type': 'text/html;charset=UTF-8',
"x-powered-by": 'Black Magic',
'x-powered-by': 'Black Magic'
// 'cache-control': 'max-age=1'
},
DEFAULT_COLOR: '#10A3FF'
Expand Down
6 changes: 3 additions & 3 deletions src/fetch.ts
Expand Up @@ -5,7 +5,7 @@ export const fetchUsingGuest = async (status: string): Promise<TimelineBlobParti

let headers: { [header: string]: string } = {
Authorization: Constants.GUEST_BEARER_TOKEN,
...Constants.BASE_HEADERS,
...Constants.BASE_HEADERS
};

/* If all goes according to plan, we have a guest token we can use to call API
Expand All @@ -16,7 +16,7 @@ export const fetchUsingGuest = async (status: string): Promise<TimelineBlobParti
const activate = await fetch(`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`, {
method: 'POST',
headers: headers,
body: '',
body: ''
});

/* Let's grab that guest_token so we can use it */
Expand All @@ -42,7 +42,7 @@ export const fetchUsingGuest = async (status: string): Promise<TimelineBlobParti
`${Constants.TWITTER_ROOT}/i/api/2/timeline/conversation/${status}.json?${Constants.GUEST_FETCH_PARAMETERS}`,
{
method: 'GET',
headers: headers,
headers: headers
}
)
).json()) as TimelineBlobPartial;
Expand Down
2 changes: 1 addition & 1 deletion src/html.ts
Expand Up @@ -15,5 +15,5 @@ export const Html = {
███ The best way to embed tweets.
███ A work in progress by @dangeredwolf
-->
<head>{headers}</head>`,
<head>{headers}</head>`
};
17 changes: 7 additions & 10 deletions src/palette.ts
@@ -1,16 +1,13 @@


import { Constants } from "./constants";
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)}`;
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++) {
Expand All @@ -25,4 +22,4 @@ export const colorFromPalette = (palette: MediaPlaceholderColor[]) => {
}

return Constants.DEFAULT_COLOR;
}
};
43 changes: 29 additions & 14 deletions src/poll.ts
Expand Up @@ -8,16 +8,19 @@ export const calculateTimeLeft = (date: Date) => {
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 ? 'day left' : 'days left'}` : '';
const hoursString = hours > 0 ? `${hours} ${hours === 1 ? 'hour left' : 'hours left'}` : '';
const minutesString = minutes > 0 ? `${minutes} ${minutes === 1 ? 'minute left' : 'minutes left'}` : '';
const secondsString = seconds > 0 ? `${seconds} ${seconds === 1 ? 'second left' : 'seconds left'}` : '';
const hoursString =
hours > 0 ? `${hours} ${hours === 1 ? 'hour left' : 'hours left'}` : '';
const minutesString =
minutes > 0 ? `${minutes} ${minutes === 1 ? 'minute left' : 'minutes left'}` : '';
const secondsString =
seconds > 0 ? `${seconds} ${seconds === 1 ? 'second left' : 'seconds left'}` : '';
return daysString || hoursString || minutesString || secondsString || 'Final results';
}
};

export const renderPoll = async (card: TweetCard): Promise<string> => {
let str = '\n\n';
Expand All @@ -29,22 +32,34 @@ export const renderPoll = async (card: TweetCard): Promise<string> => {
let totalVotes = 0;
let timeLeft = '';

if (typeof values !== "undefined" && typeof values.end_datetime_utc !== "undefined") {
if (typeof values !== 'undefined' && typeof values.end_datetime_utc !== 'undefined') {
const date = new Date(values.end_datetime_utc.string_value);
timeLeft = calculateTimeLeftString(date);
}

if (typeof values !== "undefined" && typeof values.choice1_count !== "undefined" && typeof values.choice2_count !== "undefined") {
choices[values.choice1_label?.string_value || ''] = parseInt(values.choice1_count.string_value);
if (
typeof values !== 'undefined' &&
typeof values.choice1_count !== 'undefined' &&
typeof values.choice2_count !== 'undefined'
) {
choices[values.choice1_label?.string_value || ''] = parseInt(
values.choice1_count.string_value
);
totalVotes += parseInt(values.choice1_count.string_value);
choices[values.choice2_label?.string_value || ''] = parseInt(values.choice2_count.string_value);
choices[values.choice2_label?.string_value || ''] = parseInt(
values.choice2_count.string_value
);
totalVotes += parseInt(values.choice2_count.string_value);
if (typeof values.choice3_count !== "undefined") {
choices[values.choice3_label?.string_value || ''] = parseInt(values.choice3_count.string_value);
if (typeof values.choice3_count !== 'undefined') {
choices[values.choice3_label?.string_value || ''] = parseInt(
values.choice3_count.string_value
);
totalVotes += parseInt(values.choice3_count.string_value);
}
if (typeof values.choice4_count !== "undefined") {
choices[values.choice4_label?.string_value || ''] = parseInt(values.choice4_count.string_value);
if (typeof values.choice4_count !== 'undefined') {
choices[values.choice4_label?.string_value || ''] = parseInt(
values.choice4_count.string_value
);
totalVotes += parseInt(values.choice4_count.string_value);
}
} else {
Expand All @@ -65,4 +80,4 @@ ${label}  (${Math.round((votes / totalVotes || 0) * 100)}%)

console.log(str);
return str;
}
};
2 changes: 1 addition & 1 deletion src/quote.ts
@@ -1,3 +1,3 @@
export const handleQuote = (quote: TweetPartial): string | null => {
return null;
}
};
34 changes: 18 additions & 16 deletions src/server.ts
Expand Up @@ -28,17 +28,17 @@ const statusRequest = async (request: any) => {
const url = new URL(request.url);
const userAgent = request.headers.get('User-Agent');

if (userAgent.match(/bot/ig) !== null) {
if (userAgent.match(/bot/gi) !== null) {
return new Response(await handleStatus(handle, id, parseInt(mediaNumber || 1)), {
headers: {
'content-type': 'text/html;charset=UTF-8',
'content-type': 'text/html;charset=UTF-8'
},
status: 200
});
} else {
return Response.redirect(`${Constants.TWITTER_ROOT}${url.pathname}`, 302);
}
}
};

router.get('/:handle/status/:id', statusRequest);
router.get('/:handle/status/:id/photo/:mediaNumber', statusRequest);
Expand All @@ -48,29 +48,31 @@ router.get('/:handle/statuses/:id/photo/:mediaNumber', statusRequest);
router.get('/:handle/statuses/:id/video/:mediaNumber', statusRequest);

router.get('/owoembed', async (request: any) => {
console.log("THE OWOEMBED HAS BEEN ACCESSED!!!!!!!!!");
const { searchParams } = new URL(request.url)
console.log('THE OWOEMBED HAS BEEN ACCESSED!!!!!!!!!');
const { searchParams } = new URL(request.url);

let text = searchParams.get('text') || 'Twitter';
let author = searchParams.get('author') || 'dangeredwolf';
let status = searchParams.get('status') || '1547514042146865153';

const test = {
"author_name": decodeURIComponent(text),
"author_url": `https://twitter.com/${encodeURIComponent(author)}/status/${encodeURIComponent(status)}`,
"provider_name": Constants.BRANDING_NAME,
"provider_url": Constants.REDIRECT_URL,
"title": "Twitter",
"type": "link",
"version": "1.0"
}
author_name: decodeURIComponent(text),
author_url: `https://twitter.com/${encodeURIComponent(
author
)}/status/${encodeURIComponent(status)}`,
provider_name: Constants.BRANDING_NAME,
provider_url: Constants.REDIRECT_URL,
title: 'Twitter',
type: 'link',
version: '1.0'
};
return new Response(JSON.stringify(test), {
headers: {
'content-type': 'application/json',
'content-type': 'application/json'
},
status: 200
});
})
});

router.all('*', async request => {
return Response.redirect(Constants.REDIRECT_URL, 307);
Expand Down

0 comments on commit 824e44d

Please sign in to comment.