Skip to content

Commit

Permalink
feat!: moderation fns
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed Aug 22, 2021
1 parent f77a3b8 commit 739130f
Show file tree
Hide file tree
Showing 20 changed files with 1,606 additions and 983 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ npm i masterchat
## Use

```js
import { iterateChat, fetchContext } from "masterchat";
import { Masterchat, convertRunsToString } from "masterchat";

const { chat, apiKey } = await fetchContext("<videoId>");
const mc = await Masterchat.init("<videoId>");

const token = chat.continuations.top.token;
const history = [];

for await (const res of iterateChat({ apiKey, token })) {
for await (const res of mc.iterateChat({ tokenType: "top" })) {
if (res.error) break;

const { actions } = res;
const chats = actions.filter((action) => action.type === "addChatItemAction");

history.push(...actions);
for (const chat of chats) {
console.log(chat.authorName, convertRunsToString(chat.rawMessage));
}
}
```

Expand Down Expand Up @@ -62,3 +62,7 @@ For a desktop app, see [Komet](https://github.com/holodata/komet).
## Contribution

See [Contribution Guide](./CONTRIBUTING.md) for more information.

## Community

Ask questions in `#masterchat` channel on [holodata Discord server](https://holodata.org/discord).
24 changes: 4 additions & 20 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sha1 from "sha1";
import { DEFAULT_ORIGIN } from "./constants";

export interface Credentials {
SAPISID: string;
Expand All @@ -8,28 +9,11 @@ export interface Credentials {
SSID: string;
}

export const DEFAULT_CLIENT = {
clientName: "WEB",
clientVersion: "2.20210618.05.00-canary_control",
};

const DEFAULT_ORIGIN = "https://www.youtube.com";

export function withAuthHeader(
creds: Credentials | undefined,
headers: any = {}
) {
const defaultHeaders = {
...headers,
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
};

if (!creds) return defaultHeaders;
export function buildAuthHeaders(creds: Credentials | undefined) {
if (!creds) return undefined;

return {
...defaultHeaders,
Cookie: genCookieString(creds) + (headers.Cookie ?? ""),
Cookie: genCookieString(creds),
Authorization: genAuthToken(creds.SAPISID, DEFAULT_ORIGIN),
"X-Origin": DEFAULT_ORIGIN,
};
Expand Down
69 changes: 69 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Credentials, buildAuthHeaders } from "./auth";
import { DEFAULT_HEADERS, DEFAULT_ORIGIN } from "./constants";
import fetch from "cross-fetch";
import { LiveChatContext, Metadata } from "./services/context/exports";
import { ReloadContinuationItems } from "./services/chat/exports";
import { debugLog } from "./util";

export class Base {
public videoId!: string;
protected apiKey!: string;
protected credentials?: Credentials;

public metadata!: Metadata;
public continuation!: ReloadContinuationItems;
protected isReplay!: boolean;
protected liveChatContext?: LiveChatContext;

protected get(input: string, init?: RequestInit) {
if (!input.startsWith("http")) {
input = DEFAULT_ORIGIN + input;
}
const parsedUrl = new URL(input);

if (!parsedUrl.searchParams.has("key")) {
parsedUrl.searchParams.append("key", this.apiKey);
}

const authHeaders = buildAuthHeaders(this.credentials);
const headers = {
...DEFAULT_HEADERS,
...authHeaders,
...init?.headers,
};

debugLog("GET", parsedUrl.toString());

return fetch(parsedUrl.toString(), {
...init,
headers,
});
}

protected post(input: string, init?: RequestInit) {
if (!input.startsWith("http")) {
input = DEFAULT_ORIGIN + input;
}
const parsedUrl = new URL(input);

if (!parsedUrl.searchParams.has("key")) {
parsedUrl.searchParams.append("key", this.apiKey);
}

const authHeaders = buildAuthHeaders(this.credentials);
const headers = {
...DEFAULT_HEADERS,
...authHeaders,
...init?.headers,
"Content-Type": "application/json",
};

debugLog("POST", parsedUrl.toString(), init?.body);

return fetch(parsedUrl.toString(), {
...init,
method: "POST",
headers,
});
}
}
15 changes: 15 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const DEFAULT_ORIGIN = "https://www.youtube.com";

export const DEFAULT_HEADERS = {
"Accept-Language": "en",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
};

export const DEFAULT_CLIENT = {
clientName: "WEB",
// clientVersion: "2.20210618.05.00-canary_control",
clientVersion: "2.20210813.00.00",
};

export const DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
210 changes: 0 additions & 210 deletions src/context.ts

This file was deleted.

0 comments on commit 739130f

Please sign in to comment.