forked from mito-systems/sol-mem-gen
Refactor out tweet text generation and regex logic
This commit is contained in:
parent
299e06f769
commit
bfda39830a
@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { processTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
|
||||
import { extractData } from '../../../utils/tweetMessage';
|
||||
|
||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||
try {
|
||||
@ -41,19 +42,3 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const extractData = (tweet: string | object) => {
|
||||
const tweetText = typeof tweet === 'string' ? tweet : JSON.stringify(tweet);
|
||||
|
||||
const decodedTweet = tweetText.replace(/'/g, "'").replace(/"/g, '"');
|
||||
|
||||
const urlMatch = decodedTweet.match(/<a href="(https:\/\/t.co\/[^"]+)">/);
|
||||
const txSignatureMatch = decodedTweet.match(/TX Hash: '([^']+)'/);
|
||||
const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/);
|
||||
|
||||
return {
|
||||
memeUrl: urlMatch ? urlMatch[1] : null,
|
||||
txSignature: txSignatureMatch ? txSignatureMatch[1].trim() : null,
|
||||
handle: handleMatch ? handleMatch[1] : null,
|
||||
};
|
||||
};
|
||||
|
@ -5,6 +5,8 @@ import BN from 'bn.js';
|
||||
import Big from 'big.js';
|
||||
import dynamic from 'next/dynamic'
|
||||
|
||||
import { generateTweetText } from '../utils/tweetMessage';
|
||||
|
||||
interface AIServiceCardProps {
|
||||
title: string
|
||||
description: string
|
||||
@ -95,7 +97,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
|
||||
const cid = imageUrl.split("/image/")[1];
|
||||
const memePageUrl = `${baseUrl}memes/${cid}`;
|
||||
|
||||
const tweetText = `Check out this meme that I generated! \n TX Hash: '${transactionSignature}' \n @${process.env.NEXT_PUBLIC_TWITTER_HANDLE} \n`;
|
||||
const tweetText = generateTweetText(transactionSignature, process.env.NEXT_PUBLIC_TWITTER_HANDLE)
|
||||
|
||||
return `https://twitter.com/intent/tweet?text=${encodeURIComponent(tweetText)}&url=${encodeURIComponent(memePageUrl)}`;
|
||||
};
|
||||
|
19
src/utils/tweetMessage.ts
Normal file
19
src/utils/tweetMessage.ts
Normal file
@ -0,0 +1,19 @@
|
||||
export const generateTweetText = (transactionSignature: string, handle: string | undefined) => {
|
||||
return `Check out this meme that I generated! \n TX Hash: '${transactionSignature}' \n @${handle} \n`;
|
||||
};
|
||||
|
||||
export const extractData = (tweet: string | object) => {
|
||||
const tweetText = typeof tweet === 'string' ? tweet : JSON.stringify(tweet);
|
||||
|
||||
const decodedTweet = tweetText.replace(/'/g, "'").replace(/"/g, '"');
|
||||
|
||||
const urlMatch = decodedTweet.match(/<a href="(https:\/\/t.co\/[^"]+)">/);
|
||||
const txSignatureMatch = decodedTweet.match(/TX Hash: '([^']+)'/);
|
||||
const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/);
|
||||
|
||||
return {
|
||||
memeUrl: urlMatch ? urlMatch[1] : null,
|
||||
txSignature: txSignatureMatch ? txSignatureMatch[1].trim() : null,
|
||||
handle: handleMatch ? handleMatch[1] : null,
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user