Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
feat(tktrex): defined hook for login
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Jun 21, 2022
1 parent 835310d commit 0b04327
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/shared/src/providers/puppeteer/DirectiveHook.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as puppeteer from 'puppeteer-core';
import { OpenURLDirective } from '../../models/Directive';

export type HookHandler = (
page: puppeteer.Page,
directive: any,
options?: any
) => Promise<any>;

export interface DirectiveHooks<
DO extends string,
CS extends {
[key: string]: (
page: puppeteer.Page,
directive: any,
opts?: any
) => Promise<any>;
[key: string]: HookHandler;
}
> {
openURL: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const ExperimentList: React.FC<ExperimentListProps> = ({
variant="body1"
style={{ marginLeft: theme.spacing(1) }}
>
{d.tags.join(', ')}
{d.directiveType} {d.tags.join(', ')}
</Typography>
<Box
style={{
Expand Down
5 changes: 4 additions & 1 deletion platforms/guardoni/src/guardoni/directives/tk.directives.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DirectiveHooks } from '@shared/providers/puppeteer/DirectiveHook';
import { tkLogin } from '@tktrex/shared/methodology/directives/tiktokLogin.directive';
import * as puppeteer from 'puppeteer-core';
import { GuardoniProfile } from '../types';

Expand Down Expand Up @@ -42,7 +43,9 @@ export const GetTKHooks: GetTKHooks = (ctx) => {
afterWait,
completed,
},
customs: {},
customs: {
tiktokLogin: tkLogin
},
DOMAIN_NAME: 'tiktok.com',
};
};
7 changes: 7 additions & 0 deletions platforms/guardoni/src/guardoni/directives/yt.directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as puppeteer from 'puppeteer-core';
import url from 'url';
import { GuardoniProfile } from '../types';


const debug = D('guardoni:youtube');
const logreqst = D('guardoni:requests');
const screendebug = D('guardoni:screenshots');
Expand Down Expand Up @@ -359,8 +360,10 @@ async function interactWithYT(
specialwatch,
'milliseconds'
);

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
await page.waitForTimeout(specialwatch as any);

debug('Finished special watchining time of:', specialwatch, 'milliseconds');
} else {
// eslint-disable-next-line no-console
Expand All @@ -369,14 +372,17 @@ async function interactWithYT(
}
}


type YTHooks = DirectiveHooks<
'youtube.com',
{
interactWithYT: (

page: puppeteer.Page,
directive: OpenURLDirective,
opts: string
) => Promise<any>;

getYTstatus: (
page: puppeteer.Page,
directive: OpenURLDirective
Expand All @@ -403,5 +409,6 @@ export const GetYTHooks: GetYTHooks = (ctx) => {
interactWithYT,
},
DOMAIN_NAME: 'youtube.com' as const,

};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { HookHandler } from '@shared/providers/puppeteer/DirectiveHook';
import { sleep } from '@shared/utils/promise.utils';
import * as qs from 'qs';
import { URL } from 'url';

interface TKLoginContext {
getUsername: () => Promise<string>;
getPassword: () => Promise<string>;
}

export const tkLogin =
(ctx: TKLoginContext): HookHandler =>
async (page, directive, opts) => {
const url = page.url();
const search = qs.parse(new URL(url).search);
const redirectUrl = search.redirect_url as string;
await page.goto('https://www.tiktok.com/login');

const loginButton = await page.waitForSelector(
'[class*="login-container"] > [class*="social-container"] > [class*="channel-item-wrapper"]:nth-of-type(2)'
);

await loginButton?.click();

const loginWithEmailButton = await page.waitForSelector(
'a[href="/login/phone-or-email/email"]'
);

await loginWithEmailButton?.click();

const username = await ctx.getUsername();
await page.type('input[name="email"]', username);

const password = await ctx.getPassword();
await page.type('input[name="password"]', password);

await sleep(10000);

const submitButton = await page.waitForSelector(
'button[class*="login-button"]:not(disabled)',
{
timeout: 10000,
}
);

await submitButton?.click();

await page.goto(redirectUrl);

return Promise.resolve(undefined);
};

0 comments on commit 0b04327

Please sign in to comment.