summaryrefslogtreecommitdiff
path: root/packages/tweetdeck/src/lib/fetching/python.ts
blob: 760cb2c81e6c61fcd2948fc45f4406598b813ecd (plain)
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
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;
  }
}