Skip to content

Commit

Permalink
chore: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
p1atdev committed Mar 7, 2023
1 parent 9982b7c commit 25dafeb
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 117 deletions.
105 changes: 56 additions & 49 deletions src/graph_query_ids.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
import { TwitterURL } from "./mod.ts"
import { UserAgent } from "./static.ts"
import { TwitterURL } from "./mod.ts";
import { UserAgent } from "./static.ts";

interface GraphQueryIdResponse {
queryId: string
operationName: string
operationType: "mutations" | "queries"
queryId: string;
operationName: string;
operationType: "mutations" | "queries";
}

export type GraphQueryIds = Map<string, string>
export type GraphQueryIds = Map<string, string>;

export class GraphQuery {
/**
* Get graphql endpoint random ids
* @returns Promise<GraphQueryId[]>
*/
static getIds = async (): Promise<GraphQueryIds> => {
const html = await fetch(TwitterURL.WebClient.value, {
headers: {
"User-Agent": UserAgent.Firefox,
},
})
/**
* Get graphql endpoint random ids
* @returns Promise<GraphQueryId[]>
*/
static getIds = async (): Promise<GraphQueryIds> => {
const html = await fetch(TwitterURL.WebClient.value, {
headers: {
"User-Agent": UserAgent.Firefox,
},
});

const htmlText = await html.text()
// console.log(htmlText)
const htmlText = await html.text();
// console.log(htmlText)

// get api.*..js from html
// the format is `api:"9eacf99",`
const apiJsId = htmlText.match(/api:"([^"]+)",/)
if (apiJsId === null) {
throw new Error("Cannot get api.*.js id")
}
// console.log(apiJsId)

const link = `https://abs.twimg.com/responsive-web/client-web/api.${apiJsId[1]}a.js`
// console.log(link)
// get api.*..js from html
// the format is `api:"9eacf99",`
const apiJsId = htmlText.match(/api:"([^"]+)",/);
if (apiJsId === null) {
throw new Error("Cannot get api.*.js id");
}
// console.log(apiJsId)

const mainJs = await fetch(link)
const link = `https://abs.twimg.com/responsive-web/client-web/api.${
apiJsId[1]
}a.js`;
// console.log(link)

// get all query ids from main.js
const mainJsText = await mainJs.text()
// console.log(mainJsText)
const queryIds = mainJsText.match(/{queryId:"([^"]+)",operationName:"([^"]+)",operationType:"([^"]+)"/g) || []
const patchedQueryArray = queryIds.map((query) => query + "}")
const mainJs = await fetch(link);

const queries = patchedQueryArray.map((query) => {
const correctQuery = query.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')
const queryJson = JSON.parse(correctQuery)
const queryData: GraphQueryIdResponse = {
queryId: queryJson.queryId,
operationName: queryJson.operationName,
operationType: queryJson.operationType,
}
return queryData
})
// get all query ids from main.js
const mainJsText = await mainJs.text();
// console.log(mainJsText)
const queryIds = mainJsText.match(
/{queryId:"([^"]+)",operationName:"([^"]+)",operationType:"([^"]+)"/g,
) || [];
const patchedQueryArray = queryIds.map((query) => query + "}");

const queryIdsMap = new Map<string, string>()
for (const query of queries) {
queryIdsMap.set(query.operationName, query.queryId)
}
const queries = patchedQueryArray.map((query) => {
const correctQuery = query.replace(
/(['"])?([a-z0-9A-Z_]+)(['"])?:/g,
'"$2": ',
);
const queryJson = JSON.parse(correctQuery);
const queryData: GraphQueryIdResponse = {
queryId: queryJson.queryId,
operationName: queryJson.operationName,
operationType: queryJson.operationType,
};
return queryData;
});

return queryIdsMap
const queryIdsMap = new Map<string, string>();
for (const query of queries) {
queryIdsMap.set(query.operationName, query.queryId);
}

return queryIdsMap;
};
}
131 changes: 69 additions & 62 deletions src/static.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,98 @@
export class Authorization {
type: "Bearer" | "OAuth"
token: string
type: "Bearer" | "OAuth";
token: string;

constructor(type: "Bearer" | "OAuth", token: string) {
this.type = type
this.token = token
}
constructor(type: "Bearer" | "OAuth", token: string) {
this.type = type;
this.token = token;
}
}

/**
* Class of bearer token.
*/
export class Bearer extends Authorization {
private constructor(token: string) {
super("Bearer", token)
}
private constructor(token: string) {
super("Bearer", token);
}

/**
* The official web client bearer token. (Recommended)
*/
static Web: Bearer = new Bearer(
"AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
)
/**
* The official web client bearer token. (Recommended)
*/
static Web: Bearer = new Bearer(
"AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA",
);

/**
* Use your own bearer token if you want to use.
* @param token
* @returns
*/
static Own(token: string): Bearer {
return new Bearer(token)
}
/**
* Use your own bearer token if you want to use.
* @param token
* @returns
*/
static Own(token: string): Bearer {
return new Bearer(token);
}
}

/**
* Class of bearer token.
*/
export class OAuth extends Authorization {
private constructor(token: string) {
super("OAuth", token)
}
private constructor(token: string) {
super("OAuth", token);
}

/**
* Use your own oauth token if you want to use.
* @param token
* @returns
*/
static Own(token: string): Bearer {
return new OAuth(token)
}
/**
* Use your own oauth token if you want to use.
* @param token
* @returns
*/
static Own(token: string): Bearer {
return new OAuth(token);
}
}

export type APIURLType = "gql" | "api" | "api/1.1" | "i/api/1.1" | "i/api/2" | "i/api/i"
export type APIURLType =
| "gql"
| "api"
| "api/1.1"
| "i/api/1.1"
| "i/api/2"
| "i/api/i";

export class TwitterURL {
value: string
value: string;

private constructor(link: string) {
this.value = link
}
private constructor(link: string) {
this.value = link;
}

static WebClient = new TwitterURL("https://twitter.com")
static WebClient = new TwitterURL("https://twitter.com");

static API(urlType: APIURLType): TwitterURL {
switch (urlType) {
case "gql": {
return new TwitterURL("https://api.twitter.com/graphql")
}
case "api": {
return new TwitterURL("https://api.twitter.com")
}
case "api/1.1": {
return new TwitterURL("https://api.twitter.com/1.1")
}
case "i/api/1.1": {
return new TwitterURL("https://twitter.com/i/api/1.1")
}
case "i/api/2": {
return new TwitterURL("https://twitter.com/i/api/2")
}
case "i/api/i": {
return new TwitterURL("https://twitter.com/i/api/i")
}
}
static API(urlType: APIURLType): TwitterURL {
switch (urlType) {
case "gql": {
return new TwitterURL("https://api.twitter.com/graphql");
}
case "api": {
return new TwitterURL("https://api.twitter.com");
}
case "api/1.1": {
return new TwitterURL("https://api.twitter.com/1.1");
}
case "i/api/1.1": {
return new TwitterURL("https://twitter.com/i/api/1.1");
}
case "i/api/2": {
return new TwitterURL("https://twitter.com/i/api/2");
}
case "i/api/i": {
return new TwitterURL("https://twitter.com/i/api/i");
}
}
}
}

export enum UserAgent {
Firefox = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0",
Firefox =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0",
}
12 changes: 6 additions & 6 deletions test/graph_query_ids.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { assertExists } from "../deps.ts"
import { GraphQuery } from "../src/mod.ts"
import { assertExists } from "../deps.ts";
import { GraphQuery } from "../src/mod.ts";

Deno.test("Get graph query ids", async () => {
const graphQueryIds = await GraphQuery.getIds()
const graphQueryIds = await GraphQuery.getIds();

console.log(graphQueryIds)
console.log(graphQueryIds);

assertExists(graphQueryIds.get("UserByScreenName"))
})
assertExists(graphQueryIds.get("UserByScreenName"));
});

0 comments on commit 25dafeb

Please sign in to comment.