Skip to content

Commit

Permalink
Merge pull request #6 from Nerixyz/update-april
Browse files Browse the repository at this point in the history
Update and Refactor
  • Loading branch information
Nerixyz authored Apr 25, 2021
2 parents f6fac85 + 30fce6a commit d0803f5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/background/ad.replacement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TwitchStitchedAdData } from '../types';
import { TwitchStitchedAdData } from './twitch-m3u8.types';
import { getPlayerAccessTokenRequest, makeAdRequest } from './gql.requests';
import { eventHandler } from './utilities/messaging';

Expand All @@ -12,6 +12,7 @@ export async function onAdPod(stitchedAd: TwitchStitchedAdData, stream: string)
creativeId: stitchedAd['X-TV-TWITCH-AD-CREATIVE-ID'],
adId: stitchedAd['X-TV-TWITCH-AD-ADVERTISER-ID'],
rollType: stitchedAd['X-TV-TWITCH-AD-ROLL-TYPE'],
duration: Math.round(stitchedAd.DURATION ?? 30),
};
await makeAdRequest(adPod);

Expand Down
5 changes: 3 additions & 2 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import _OnBeforeRequestDetails = browser.webRequest._OnBeforeRequestDetails;
import { StreamFilter, TwitchStitchedAdData } from '../types';
import { StreamFilter } from '../types';
import { parseAttributes } from './utilities/m3u8.utilities';
import { OverridePlayer, UserAgent } from '../options';
import { onAdPod, StreamTabs } from './ad.replacement';
import { TWITCH_USER_PAGE } from './utilities/request.utilities';
import { eventHandler } from './utilities/messaging';
import { TwitchStitchedAdData } from './twitch-m3u8.types';

function onRequest(request: _OnBeforeRequestDetails) {
if (!request.url.includes('video-weaver')) return;
Expand Down Expand Up @@ -102,7 +103,7 @@ function extractAdData(data: string, doc: string, tabId: number) {
}
if(!TWITCH_USER_PAGE.test(doc)) return;

const attr = parseAttributes(attrString) as TwitchStitchedAdData;
const attr = parseAttributes<TwitchStitchedAdData>(attrString);
onAdPod(attr, StreamTabs.get(tabId) ?? TWITCH_USER_PAGE.exec(doc)?.[1] ?? '').then(() => {
eventHandler.emitContext('adSkipped', {});
console.debug('"Skipped" ad.');
Expand Down
7 changes: 4 additions & 3 deletions src/background/gql.requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function makeGraphQlAdPacket(event: string, radToken: string, payload: any) {
];
}

export async function makeAdRequest({ adId, creativeId, lineItemId, orderId, radToken, rollType, podLength }: AdPod) {
export async function makeAdRequest({ adId, creativeId, lineItemId, orderId, radToken, rollType, podLength, duration }: AdPod) {
const baseData = {
stitched: true,
roll_type: rollType,
Expand All @@ -58,14 +58,15 @@ export async function makeAdRequest({ adId, creativeId, lineItemId, orderId, rad
...baseData,
ad_id: adId,
ad_position: podPosition,
duration: 30,
duration,
creative_id: creativeId,
total_ads: podLength,
order_id: orderId,
line_item_id: lineItemId
};

await gqlRequest(makeGraphQlAdPacket('video_ad_impression', radToken, extendedData));

for (let quartile = 0; quartile < 4; quartile++) {
await gqlRequest(
makeGraphQlAdPacket('video_ad_quartile_complete', radToken, {
Expand Down Expand Up @@ -102,7 +103,7 @@ export async function getPlayerAccessTokenRequest(login: string): Promise<{value
const res = await gqlRequest(makeRawGqlPacket('PlaybackAccessToken', PLAYBACK_ACCESS_TOKEN_QUERY, {
isLive: true,
isVod: false,
login: login,
login,
playerType: 'site',
vodID: ''
})).then(x => x.json());
Expand Down
22 changes: 22 additions & 0 deletions src/background/twitch-m3u8.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface TwitchStitchedAdData {
ID: string;
CLASS: string;
'START-DATE': string;
DURATION: number;
'X-TV-TWITCH-AD-ADVERTISER-ID': string;
'X-TV-TWITCH-AD-CLICK-TRACKING-URL': string;
'X-TV-TWITCH-AD-LINE-ITEM-ID': string;
'X-TV-TWITCH-AD-LOUDNESS': string;
'X-TV-TWITCH-AD-COMMERCIAL-ID': string;
'X-TV-TWITCH-AD-ROLL-TYPE': string;
'X-TV-TWITCH-AD-AD-FORMAT': string;
'X-TV-TWITCH-AD-AD-SESSION-ID': string;
'X-TV-TWITCH-AD-URL': string;
'X-TV-TWITCH-AD-POD-POSITION': string;
'X-TV-TWITCH-AD-CREATIVE-ID': string;
'X-TV-TWITCH-AD-ORDER-ID': string;
'X-TV-TWITCH-AD-ADVERIFICATIONS': string;
'X-TV-TWITCH-AD-POD-LENGTH': string;
'X-TV-TWITCH-AD-RADS-TOKEN': string;
}

18 changes: 14 additions & 4 deletions src/background/utilities/m3u8.utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export function parseM3u8(content: string) {
/**
* Parses a whole m3u8 file into an object.
* @param content A whole m3u8 file.
* @returns All 'EXT-X-DATERANGE' tags put into an object (key = CLASS)
*/
export function parseM3u8<T extends Record<string, Record<string, any>>>(content: string): T {
const data = [];
const lines = content.split('\n').filter(Boolean);
let line;
Expand All @@ -17,16 +22,21 @@ export function parseM3u8(content: string) {
return Object.fromEntries(data.map(x => [x.CLASS, x]));
}

export function parseAttributes(str: string) {
/**
*
* @param str The attribute string essentially: `'CLASS="twitch-stitched-ad",DURATION=15.187,...'`
* @returns The object-version: `{ CLASS: 'twitch-stitched-ad', DURATION: 15.187 }`
*/
export function parseAttributes<T extends Record<string, any> = Record<string, any>>(str: string): T {
return Object.fromEntries(
str
.split(/(?:^|,)([^=]*=(?:"[^"]*"|[^,]*))/)
.filter(Boolean)
.map(x => {
const idx = x.indexOf('=');
const key = x.substring(0, idx);
const value = x.substring(idx +1);
const value = x.substring(idx + 1);
const num = Number(value);
return [key, Number.isNaN(num) ? value.startsWith('"') ? JSON.parse(value) : value : num]
}));
})) as T;
}
5 changes: 4 additions & 1 deletion src/background/utilities/request.utilities.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export async function makeGqlHeaders() {
const auth = await getCookie('auth-token');
return {
'Client-Id': await getClientId(),
Authorization: `OAuth ${await getCookie('auth-token')}`,
'X-Device-Id': await getCookie('unique_id') ?? 'PogO',
...(auth && {
'Authorization': `OAuth ${auth}`,
})
}
}

Expand Down
23 changes: 1 addition & 22 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,5 @@ export interface AdPod {
rollType: string;
podPosition: number;
podLength: number;
}

export interface TwitchStitchedAdData {
ID: string;
CLASS: string;
'START-DATE': string;
DURATION: number;
'X-TV-TWITCH-AD-ADVERTISER-ID': string;
'X-TV-TWITCH-AD-CLICK-TRACKING-URL': string;
'X-TV-TWITCH-AD-LINE-ITEM-ID': string;
'X-TV-TWITCH-AD-LOUDNESS': string;
'X-TV-TWITCH-AD-COMMERCIAL-ID': string;
'X-TV-TWITCH-AD-ROLL-TYPE': string;
'X-TV-TWITCH-AD-AD-FORMAT': string;
'X-TV-TWITCH-AD-AD-SESSION-ID': string;
'X-TV-TWITCH-AD-URL': string;
'X-TV-TWITCH-AD-POD-POSITION': string;
'X-TV-TWITCH-AD-CREATIVE-ID': string;
'X-TV-TWITCH-AD-ORDER-ID': string;
'X-TV-TWITCH-AD-ADVERIFICATIONS': string;
'X-TV-TWITCH-AD-POD-LENGTH': string;
'X-TV-TWITCH-AD-RADS-TOKEN': string;
duration: number;
}

0 comments on commit d0803f5

Please sign in to comment.