generated from p1atdev/deno_template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,64 @@ | ||
import { TwitterURL } from "./mod.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); | ||
/** | ||
* 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(); | ||
const htmlText = await html.text() | ||
// console.log(htmlText) | ||
|
||
// get main.js from html | ||
// const links = htmlText.match(/https:\/\/abs\.twimg\.com\/responsive-web\/client-web([^\/]+|)\/main\.[^.]+\.js/g) | ||
const links = htmlText.match( | ||
/https:\/\/abs\.twimg\.com\/responsive-web\/client-web([^\/]+|)\/main\.[^.]+\.js/g, | ||
); | ||
// 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) | ||
|
||
// if mainJsURLs is null, return empty array | ||
if (links === null) { | ||
return new Map(); | ||
} | ||
const link = `https://abs.twimg.com/responsive-web/client-web/api.${apiJsId[1]}a.js` | ||
// console.log(link) | ||
|
||
// get main.js from mainJsURLs[0] | ||
const link = links[0]; | ||
const mainJs = await fetch(link); | ||
const mainJs = await fetch(link) | ||
|
||
// get all query ids from main.js | ||
const mainJsText = await mainJs.text(); | ||
const queryIds = mainJsText.match( | ||
/{queryId:"([^"]+)",operationName:"([^"]+)",operationType:"([^"]+)"/g, | ||
) || []; | ||
const patchedQueryArray = queryIds.map((query) => query + "}"); | ||
// 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 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; | ||
}); | ||
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 | ||
}) | ||
|
||
const queryIdsMap = new Map<string, string>(); | ||
for (const query of queries) { | ||
queryIdsMap.set(query.operationName, query.queryId); | ||
} | ||
const queryIdsMap = new Map<string, string>() | ||
for (const query of queries) { | ||
queryIdsMap.set(query.operationName, query.queryId) | ||
} | ||
|
||
return queryIdsMap; | ||
}; | ||
return queryIdsMap | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,91 @@ | ||
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +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() | ||
|
||
assertExists(graphQueryIds.get("UserByScreenName")); | ||
}); | ||
console.log(graphQueryIds) | ||
|
||
assertExists(graphQueryIds.get("UserByScreenName")) | ||
}) |