mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 02:57:34 +00:00
9f470f06e9
Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
168 lines
5.1 KiB
TypeScript
168 lines
5.1 KiB
TypeScript
import {Browser, Response} from 'puppeteer';
|
||
import {NvidiaRegionInfo, regionInfos} from '../nvidia-api';
|
||
import {Config} from '../../../config';
|
||
import {Link} from '../store';
|
||
import {Logger} from '../../../logger';
|
||
import open from 'open';
|
||
import {timestampUrlParameter} from '../../timestamp-url-parameter';
|
||
|
||
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', fe2060SuperId: 5379432500, fe3080Id: 5438481700, fe3090Id: null, nvidiaLocale: 'en_us'};
|
||
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}` +
|
||
`&locale=${drLocale}` +
|
||
timestampUrlParameter();
|
||
}
|
||
|
||
interface NvidiaSessionTokenJSON {
|
||
access_token: string;
|
||
}
|
||
|
||
function nvidiaSessionUrl(nvidiaLocale: string): string {
|
||
return `https://store.nvidia.com/store/nvidia/SessionToken?format=json&locale=${nvidiaLocale}` +
|
||
`&apiKey=${nvidiaApiKey}` +
|
||
timestampUrlParameter();
|
||
}
|
||
|
||
function addToCartUrl(id: number, drLocale: string, token: string): string {
|
||
return 'https://api.digitalriver.com/v1/shoppers/me/carts/active/line-items?format=json&method=post' +
|
||
`&productId=${id}` +
|
||
`&token=${token}` +
|
||
'&quantity=1' +
|
||
`&locale=${drLocale}` +
|
||
timestampUrlParameter();
|
||
}
|
||
|
||
function checkoutUrl(drLocale: string, token: string): string {
|
||
return `https://api.digitalriver.com/v1/shoppers/me/carts/active/web-checkout?token=${token}&locale=${drLocale}`;
|
||
}
|
||
|
||
function fallbackCartUrl(nvidiaLocale: string): string {
|
||
return `https://www.nvidia.com/${nvidiaLocale}/shop/geforce?${timestampUrlParameter()}`;
|
||
}
|
||
|
||
export function generateSetupAction() {
|
||
return async (browser: Browser) => {
|
||
const {drLocale, nvidiaLocale} = getRegionInfo();
|
||
|
||
const page = await browser.newPage();
|
||
|
||
let response: Response | null;
|
||
try {
|
||
Logger.debug('creating cart/session token...');
|
||
|
||
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;
|
||
const cartUrl = checkoutUrl(drLocale, accessToken);
|
||
|
||
Logger.debug(cartUrl);
|
||
|
||
if (Config.browser.open) {
|
||
Logger.info('ℹ opening browser for user to login');
|
||
|
||
await open(cartUrl);
|
||
}
|
||
} catch (error) {
|
||
Logger.error('✖ [nvidia] cannot generate cart/session token, continuing without; auto "add to cart" may not work', error);
|
||
}
|
||
|
||
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 🚀🚀🚀`);
|
||
|
||
let response: Response | null;
|
||
let cartUrl: string;
|
||
try {
|
||
Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, getting access token 🚀🚀🚀`);
|
||
|
||
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] ${cardName}, adding to cart 🚀🚀🚀`);
|
||
|
||
response = await page.goto(addToCartUrl(id, drLocale, accessToken), {waitUntil: 'networkidle0'});
|
||
|
||
Logger.info(`🚀🚀🚀 [nvidia] ${cardName}, opening checkout page 🚀🚀🚀`);
|
||
|
||
cartUrl = checkoutUrl(drLocale, accessToken);
|
||
|
||
Logger.info(cartUrl);
|
||
|
||
await open(cartUrl);
|
||
} catch (error) {
|
||
Logger.debug(error);
|
||
Logger.error(`✖ [nvidia] ${cardName} could not automatically add to cart, opening page`, error);
|
||
|
||
cartUrl = fallbackCartUrl(nvidiaLocale);
|
||
await open(cartUrl);
|
||
}
|
||
|
||
await page.close();
|
||
|
||
return cartUrl;
|
||
};
|
||
}
|
||
|
||
export function generateLinks(): Link[] {
|
||
const {drLocale, nvidiaLocale, fe3080Id, fe3090Id, fe2060SuperId} = getRegionInfo();
|
||
|
||
const links: Link[] = [];
|
||
|
||
if (fe2060SuperId) {
|
||
links.push({
|
||
brand: 'test:brand',
|
||
model: 'test:model',
|
||
openCartAction: generateOpenCartAction(fe2060SuperId, nvidiaLocale, drLocale, 'TEST CARD debug'),
|
||
series: 'test:series',
|
||
url: digitalRiverStockUrl(fe2060SuperId, drLocale)
|
||
});
|
||
}
|
||
|
||
if (fe3080Id) {
|
||
links.push({
|
||
brand: 'nvidia',
|
||
model: 'founders edition',
|
||
openCartAction: generateOpenCartAction(fe3080Id, nvidiaLocale, drLocale, 'nvidia founders edition 3080'),
|
||
series: '3080',
|
||
url: digitalRiverStockUrl(fe3080Id, drLocale)
|
||
});
|
||
}
|
||
|
||
if (fe3090Id) {
|
||
links.push({
|
||
brand: 'nvidia',
|
||
model: 'founders edition',
|
||
openCartAction: generateOpenCartAction(fe3090Id, nvidiaLocale, drLocale, 'nvidia founders edition 3090'),
|
||
series: '3090',
|
||
url: digitalRiverStockUrl(fe3090Id, drLocale)
|
||
});
|
||
}
|
||
|
||
return links;
|
||
}
|