The API runs, but we need to complete it

This commit is contained in:
dangered wolf 2022-07-25 13:08:03 -04:00
parent b1a0484377
commit fa1d6670e7
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
9 changed files with 45 additions and 25 deletions

View file

@ -1,7 +1,11 @@
import { fetchUsingGuest } from "./fetch";
import { translateTweet } from "./translate";
import { fetchUsingGuest } from './fetch';
import { translateTweet } from './translate';
export const statueAPI = async (event: FetchEvent, status: string, language: string): Promise<APIResponse> => {
export const statueAPI = async (
event: FetchEvent,
status: string,
language: string
): Promise<APIResponse> => {
const conversation = await fetchUsingGuest(status, event);
const tweet = conversation?.globalObjects?.tweets?.[status] || {};
/* With v2 conversation API we re-add the user object ot the tweet because
@ -32,7 +36,7 @@ export const statueAPI = async (event: FetchEvent, status: string, language: str
return { code: 500, message: 'API_FAIL' };
}
let response: APIResponse = {} as APIResponse;
let response: APIResponse = { code: 200, message: 'OK' } as APIResponse;
let apiTweet: APITweet = {} as APITweet;
const user = tweet.user;
@ -43,23 +47,27 @@ export const statueAPI = async (event: FetchEvent, status: string, language: str
apiTweet.author = {
name: name,
screen_name: screenName,
profile_picture_url: user?.profile_image_url_https || '',
profile_banner_url: user?.profile_banner_url || ''
}
avatar_url: user?.profile_image_url_https || '',
banner_url: user?.profile_banner_url || ''
};
apiTweet.replies = tweet.reply_count;
apiTweet.retweets = tweet.retweet_count;
apiTweet.likes = tweet.favorite_count;
/* If a language is specified, let's try translating it! */
if (typeof language === 'string' && language.length === 2 && language !== tweet.lang) {
let translateAPI = await translateTweet(tweet, conversation.guestToken || '', language || 'en');
let translateAPI = await translateTweet(
tweet,
conversation.guestToken || '',
language || 'en'
);
apiTweet.translation = {
translated_text: translateAPI?.translation || '',
source_language: tweet.lang,
target_language: language
}
};
}
response.tweet = apiTweet;
return response;
}
};

View file

@ -1,5 +1,5 @@
export const getAuthorText = (tweet: TweetPartial): string | null => {
/* Build out reply, retweet, like counts */
/* Build out reply, retweet, like counts */
if (tweet.favorite_count > 0 || tweet.retweet_count > 0 || tweet.reply_count > 0) {
let authorText = '';
if (tweet.reply_count > 0) {
@ -17,4 +17,4 @@ export const getAuthorText = (tweet: TweetPartial): string | null => {
}
return null;
}
};

View file

@ -5,6 +5,7 @@ export const Constants = {
BRANDING_NAME_DISCORD: BRANDING_NAME_DISCORD,
DIRECT_MEDIA_DOMAINS: DIRECT_MEDIA_DOMAINS.split(','),
MOSAIC_DOMAIN_LIST: MOSAIC_DOMAIN_LIST.split(','),
API_HOST: API_HOST,
HOST_URL: HOST_URL,
REDIRECT_URL: REDIRECT_URL,
TWITTER_ROOT: 'https://twitter.com',

1
src/env.d.ts vendored
View file

@ -4,3 +4,4 @@ declare const DIRECT_MEDIA_DOMAINS: string;
declare const HOST_URL: string;
declare const REDIRECT_URL: string;
declare const MOSAIC_DOMAIN_LIST: string;
declare const API_HOST: string;

View file

@ -20,13 +20,17 @@ const statusRequest = async (
if (
url.pathname.match(/\/status(es)?\/\d+\.(mp4|png|jpg)/g) !== null ||
Constants.DIRECT_MEDIA_DOMAINS.includes(url.hostname) ||
(prefix === 'dl' || prefix === 'dir')
prefix === 'dl' ||
prefix === 'dir'
) {
console.log('Direct media request by extension');
flags.direct = true;
}
if (url.pathname.match(/\/status(es)?\/\d+\.(json)/g) !== null) {
if (
url.pathname.match(/\/status(es)?\/\d+\.(json)/g) !== null ||
url.hostname === Constants.API_HOST
) {
console.log('JSON API request');
flags.api = true;
}

View file

@ -35,7 +35,16 @@ export const handleStatus = async (
let api = await statueAPI(event, status, language || 'en');
switch(api.code) {
if (flags?.api || true) {
return {
response: new Response(JSON.stringify(api), {
headers: { ...Constants.RESPONSE_HEADERS, 'content-type': 'application/json' },
status: api.code
})
};
}
switch (api.code) {
case 401:
return returnError(Strings.ERROR_PRIVATE);
case 404:
@ -50,9 +59,7 @@ export const handleStatus = async (
let engagementText = '';
if (api?.tweet?.translation) {
}
let mediaList = Array.from(
tweet.extended_entities?.media || tweet.entities?.media || []

View file

@ -42,7 +42,6 @@ export const translateTweet = async (
console.log(translationResults);
return translationResults;
} catch (e: any) {
console.error('Unknown error while fetching from Translation API');
return {} as TranslationPartial; // No work to do

11
src/types.d.ts vendored
View file

@ -32,13 +32,11 @@ interface APITranslate {
interface APIAuthor {
name?: string;
screen_name?: string;
profile_picture_url?: string;
profile_banner_url?: string;
avatar_url?: string;
banner_url?: string;
}
interface APIPoll {
}
interface APIPoll {}
interface APITweet {
id: string;
@ -55,5 +53,4 @@ interface APITweet {
author: APIAuthor;
thumbnail: string;
}
}

View file

@ -34,6 +34,9 @@ module.exports = {
}),
new webpack.DefinePlugin({
MOSAIC_DOMAIN_LIST: `'${process.env.MOSAIC_DOMAIN_LIST}'`
}),
new webpack.DefinePlugin({
API_HOST: `'${process.env.API_HOST}'`
})
],
optimization: {