aboutsummaryrefslogtreecommitdiff
path: root/apps/cf-ai-backend/src/queueConsumer/helpers/processTweet.ts
blob: 8d83f2dc44a34920dc3c9380f4e78d8e94310d08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Tweet } from "react-tweet/api";
import { Result, Ok, Err, isErr } from "../../errors/results";
import { BaseError } from "../../errors/baseError";
import { getMetaData, Metadata } from "../utils/get-metadata";
import { tweetToMd } from "@repo/shared-types/utils"; // can I do this?
import { Env } from "../../types";

class ProcessTweetError extends BaseError {
	constructor(message?: string, source?: string) {
		super("[Tweet Proceessing Error]", message, source);
	}
}

type GetTweetResult = Tweet;

export const getTweetData = async (
	tweetID: string,
): Promise<Result<GetTweetResult, ProcessTweetError>> => {
	try {
		console.log("is fetch defined here?");
		const url = `https://cdn.syndication.twimg.com/tweet-result?id=${tweetID}&lang=en&features=tfw_timeline_list%3A%3Btfw_follower_count_sunset%3Atrue%3Btfw_tweet_edit_backend%3Aon%3Btfw_refsrc_session%3Aon%3Btfw_fosnr_soft_interventions_enabled%3Aon%3Btfw_show_birdwatch_pivots_enabled%3Aon%3Btfw_show_business_verified_badge%3Aon%3Btfw_duplicate_scribes_to_settings%3Aon%3Btfw_use_profile_image_shape_enabled%3Aon%3Btfw_show_blue_verified_badge%3Aon%3Btfw_legacy_timeline_sunset%3Atrue%3Btfw_show_gov_verified_badge%3Aon%3Btfw_show_business_affiliate_badge%3Aon%3Btfw_tweet_edit_frontend%3Aon&token=4c2mmul6mnh`;

		const resp = await fetch(url, {
			headers: {
				"User-Agent":
					"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
				Accept: "application/json",
				"Accept-Language": "en-US,en;q=0.5",
				"Accept-Encoding": "gzip, deflate, br",
				Connection: "keep-alive",
				"Upgrade-Insecure-Requests": "1",
				"Cache-Control": "max-age=0",
				TE: "Trailers",
			},
		});
		console.log(resp.status);

		const data = (await resp.json()) as Tweet;

		return Ok(data);
	} catch (e) {
		console.error("[Tweet Proceessing Error]", e);
		return Err(new ProcessTweetError(e, "getTweetData"));
	}
};

export const getThreadData = async (input: {
	tweetUrl: string;
	env: Env;
}): Promise<Result<any, ProcessTweetError>> => {
	try {
		// const threadRequest = await fetch(input.cf_thread_endpoint, {
		// 	method: "POST",
		// 	headers: {
		// 		"Content-Type": "application/json",
		// 		Authorization: input.authKey,
		// 	},
		// 	body: JSON.stringify({ url: input.tweetUrl }),
		// });
		// if (threadRequest.status !== 200) {
		// 	console.log(await threadRequest.text());
		// 	console.log(input.tweetUrl);
		// 	return Err(
		// 		new ProcessTweetError(
		// 			`Failed to fetch the thread: ${input.tweetUrl}, Reason: ${threadRequest.statusText}`,
		// 			"getThreadData",
		// 		),
		// 	);
		// }
		//@ts-ignore
		const thread = await input.env.THREAD.processTweets(input.tweetUrl);
		console.log("[thread response]", thread);

		if (!thread.length) {
			console.log("Thread is an empty array");
			return Err(
				new ProcessTweetError(
					"[THREAD FETCHING SERVICE] Got no content form thread worker",
					"getThreadData",
				),
			);
		}
		return Ok(thread);
	} catch (e) {
		console.error("[Thread Processing Error]", e);
		return Err(new ProcessTweetError((e as Error).message, "getThreadData"));
	}
};