diff options
Diffstat (limited to 'packages/tweetdeck/src/lib/fetching/python.ts')
| -rw-r--r-- | packages/tweetdeck/src/lib/fetching/python.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/packages/tweetdeck/src/lib/fetching/python.ts b/packages/tweetdeck/src/lib/fetching/python.ts new file mode 100644 index 0000000..760cb2c --- /dev/null +++ b/packages/tweetdeck/src/lib/fetching/python.ts @@ -0,0 +1,52 @@ +import python from "bun_python"; + +export class TransactionIdGenerator { + private initialHtmlContent!: string; + private client_transaction: any; + private cookie: string; + private headers: any; + + private BeautifulSoup: any; + private ClientTransaction: any; + + constructor(cookie: string) { + this.cookie = cookie; + } + public async init() { + const genheaders = await python.import("x_client_transaction.utils") + .generate_headers; + const hs = genheaders(); + this.headers = { ...hs, Cookie: this.cookie }; + const currentUrl = "https://x.com"; + const response = await fetch(currentUrl, { headers: this.headers }); + const html = await response.text(); + this.initialHtmlContent = html; + } + + public async getTransactionId(method: string, path: string): Promise<string> { + if (!this.BeautifulSoup || !this.ClientTransaction) { + this.BeautifulSoup = await python.import("bs4").BeautifulSoup; + this.ClientTransaction = await python.import("x_client_transaction") + .ClientTransaction; + } + + if (!this.client_transaction) { + const soup = this.BeautifulSoup(this.initialHtmlContent, "lxml"); + const onDemand = await python.import("x_client_transaction.utils") + .get_ondemand_file_url; + const file = onDemand(soup); + const ondemand_res = await fetch(file, { + method: "GET", + headers: this.headers, + }); + const ondemand_text = await ondemand_res.text(); + this.client_transaction = this.ClientTransaction(soup, ondemand_text); + } + + const transaction_id = this.client_transaction.generate_transaction_id( + method, + path, + ); + return transaction_id; + } +} |
