Skip to content

Commit

Permalink
Create cart when starting run to manually log in early
Browse files Browse the repository at this point in the history
  • Loading branch information
fuckingrobot committed Sep 21, 2020
1 parent c982efd commit b6aa057
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ async function main() {

for (const store of Stores) {
Logger.debug(store.links);
if (store.setupAction !== undefined) {
store.setupAction(browser);
}
setTimeout(tryLookupAndLoop, getSleepTime(), browser, store);
}
}
Expand Down
50 changes: 38 additions & 12 deletions src/store/model/helpers/nvidia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import {Logger} from '../../../logger';
import open from 'open';
import {Link} from '../store';
import {Config} from '../../../config';
import {regionInfos} from '../nvidia';
import {NvidiaRegionInfo, regionInfos} from '../nvidia';

const nvidiaApiKey = '9485fa7b159e42edb08a83bde0d83dia';

function getRegionInfo(): NvidiaRegionInfo {
const country = Array.from(regionInfos.keys()).includes(Config.store.country) ? Config.store.country : 'usa';

const defaultRegionInfo: NvidiaRegionInfo = {drLocale: 'en_us', nvidiaLocale: 'en_us', fe3080Id: 5438481700, fe3090Id: null, fe2060SuperId: 5379432500};
return regionInfos.get(country) ?? defaultRegionInfo;
}

function digitalRiverStockUrl(id: number, drLocale: string): string {
return `https://api.digitalriver.com/v1/shoppers/me/products/${id}/inventory-status?` +
`&apiKey=${nvidiaApiKey}` +
Expand Down Expand Up @@ -42,7 +49,35 @@ function fallbackCartUrl(nvidiaLocale: string): string {
return `https://www.nvidia.com/${nvidiaLocale}/shop/geforce?${timestampUrlParameter()}`;
}

function generateOpenCartAction(id: number, nvidiaLocale: string, drLocale: string, cardName: string) {
export function generateSetupAction() {
return async (browser: Browser) => {
const {drLocale, nvidiaLocale} = getRegionInfo();

const page = await browser.newPage();

Logger.info(`[nvidia] creating cart/session token...`);
let response: Response | null;
try {
response = await page.goto(nvidiaSessionUrl(nvidiaLocale), {waitUntil: 'networkidle0'});
if (response === null) {
throw new Error('NvidiaAccessTokenUnavailable');
}

const data = await response.json() as NvidiaSessionTokenJSON;
const accessToken = data.access_token;

Logger.info(`[nvidia] you can log into your cart now...`);
Logger.info(checkoutUrl(drLocale, accessToken));
} catch (error) {
Logger.debug(error);
Logger.error(`✖ [nvidia] cannot generate cart/session token, continuing without, auto-"add to cart" may not work...`);
}

await page.close();
}
}

export function generateOpenCartAction(id: number, nvidiaLocale: string, drLocale: string, cardName: string) {
return async (browser: Browser) => {
const page = await browser.newPage();
Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, starting auto add to cart... 🚀🚀🚀`);
Expand Down Expand Up @@ -74,16 +109,7 @@ function generateOpenCartAction(id: number, nvidiaLocale: string, drLocale: stri
}

export function generateLinks(): Link[] {
const country = Array.from(regionInfos.keys()).includes(Config.store.country) ? Config.store.country : 'usa';

const defaultRegionInfo = {drLocale: 'en_us', nvidiaLocale: 'en_us', fe3080Id: 5438481700, fe3090Id: null, fe2060SuperId: 5379432500};
const regionInfo = regionInfos.get(country) ?? defaultRegionInfo;

const fe2060SuperId = regionInfo.fe2060SuperId;
const fe3080Id = regionInfo.fe3080Id;
const fe3090Id = regionInfo.fe3090Id;
const nvidiaLocale = regionInfo.nvidiaLocale;
const drLocale = regionInfo.drLocale;
const {drLocale, nvidiaLocale, fe3080Id, fe3090Id, fe2060SuperId} = getRegionInfo();

const links: Link[] = [];

Expand Down
16 changes: 13 additions & 3 deletions src/store/model/nvidia.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {Store} from './store';
import {generateLinks} from './helpers/nvidia';
import {generateLinks, generateSetupAction} from './helpers/nvidia';

// Region/country set by config file, silently ignores null / missing values and defaults to usa
export const regionInfos = new Map<string, {drLocale: string; nvidiaLocale: string; fe3080Id: number | null; fe3090Id: number | null; fe2060SuperId: number | null}>([

export interface NvidiaRegionInfo {
drLocale: string;
nvidiaLocale: string;
fe3080Id: number | null;
fe3090Id: number | null;
fe2060SuperId: number | null
}

export const regionInfos = new Map<string, NvidiaRegionInfo>([
['austria', {drLocale: 'de_de', nvidiaLocale: 'de_de', fe3080Id: 5440853700, fe3090Id: null, fe2060SuperId: null}],
['belgium', {drLocale: 'fr_fr', nvidiaLocale: 'fr_fr', fe3080Id: 5438795700, fe3090Id: null, fe2060SuperId: 5394902700}],
['canada', {drLocale: 'en_us', nvidiaLocale: 'en_ca', fe3080Id: 5438481700, fe3090Id: null, fe2060SuperId: null}],
Expand All @@ -27,5 +36,6 @@ export const Nvidia: Store = {
labels: {
outOfStock: ['product_inventory_out_of_stock', 'rate limit exceeded', 'request timeout']
},
name: 'nvidia'
name: 'nvidia',
setupAction: generateSetupAction()
};
1 change: 1 addition & 0 deletions src/store/model/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface Store {
links: Link[];
labels: Labels;
name: string;
setupAction?: (browser: Browser) => void;
}

0 comments on commit b6aa057

Please sign in to comment.