mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-21 07:23:59 +00:00
v3 init
Based of v3.0.0b10.
This commit is contained in:
123
src/util/Load.ts
Normal file
123
src/util/Load.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import type { Cookie } from 'patchright'
|
||||
import type { BrowserFingerprintWithHeaders } from 'fingerprint-generator'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import type { Account } from '../interface/Account'
|
||||
import type { Config, ConfigSaveFingerprint } from '../interface/Config'
|
||||
|
||||
let configCache: Config
|
||||
|
||||
export function loadAccounts(): Account[] {
|
||||
try {
|
||||
let file = 'accounts.json'
|
||||
|
||||
if (process.argv.includes('-dev')) {
|
||||
file = 'accounts.dev.json'
|
||||
}
|
||||
|
||||
const accountDir = path.join(__dirname, '../', file)
|
||||
const accounts = fs.readFileSync(accountDir, 'utf-8')
|
||||
|
||||
return JSON.parse(accounts)
|
||||
} catch (error) {
|
||||
throw new Error(error as string)
|
||||
}
|
||||
}
|
||||
|
||||
export function loadConfig(): Config {
|
||||
try {
|
||||
if (configCache) {
|
||||
return configCache
|
||||
}
|
||||
|
||||
const configDir = path.join(__dirname, '../', 'config.json')
|
||||
const config = fs.readFileSync(configDir, 'utf-8')
|
||||
|
||||
const configData = JSON.parse(config)
|
||||
configCache = configData
|
||||
|
||||
return configData
|
||||
} catch (error) {
|
||||
throw new Error(error as string)
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadSessionData(
|
||||
sessionPath: string,
|
||||
email: string,
|
||||
saveFingerprint: ConfigSaveFingerprint,
|
||||
isMobile: boolean
|
||||
) {
|
||||
try {
|
||||
const cookiesFileName = isMobile ? 'session_mobile.json' : 'session_desktop.json'
|
||||
const cookieFile = path.join(__dirname, '../browser/', sessionPath, email, cookiesFileName)
|
||||
|
||||
let cookies: Cookie[] = []
|
||||
if (fs.existsSync(cookieFile)) {
|
||||
const cookiesData = await fs.promises.readFile(cookieFile, 'utf-8')
|
||||
cookies = JSON.parse(cookiesData)
|
||||
}
|
||||
|
||||
const fingerprintFileName = isMobile ? 'session_fingerprint_mobile.json' : 'session_fingerprint_desktop.json'
|
||||
const fingerprintFile = path.join(__dirname, '../browser/', sessionPath, email, fingerprintFileName)
|
||||
|
||||
let fingerprint!: BrowserFingerprintWithHeaders
|
||||
const shouldLoadFingerprint = isMobile ? saveFingerprint.mobile : saveFingerprint.desktop
|
||||
if (shouldLoadFingerprint && fs.existsSync(fingerprintFile)) {
|
||||
const fingerprintData = await fs.promises.readFile(fingerprintFile, 'utf-8')
|
||||
fingerprint = JSON.parse(fingerprintData)
|
||||
}
|
||||
|
||||
return {
|
||||
cookies: cookies,
|
||||
fingerprint: fingerprint
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(error as string)
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSessionData(
|
||||
sessionPath: string,
|
||||
cookies: Cookie[],
|
||||
email: string,
|
||||
isMobile: boolean
|
||||
): Promise<string> {
|
||||
try {
|
||||
const sessionDir = path.join(__dirname, '../browser/', sessionPath, email)
|
||||
const cookiesFileName = isMobile ? 'session_mobile.json' : 'session_desktop.json'
|
||||
|
||||
if (!fs.existsSync(sessionDir)) {
|
||||
await fs.promises.mkdir(sessionDir, { recursive: true })
|
||||
}
|
||||
|
||||
await fs.promises.writeFile(path.join(sessionDir, cookiesFileName), JSON.stringify(cookies))
|
||||
|
||||
return sessionDir
|
||||
} catch (error) {
|
||||
throw new Error(error as string)
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveFingerprintData(
|
||||
sessionPath: string,
|
||||
email: string,
|
||||
isMobile: boolean,
|
||||
fingerpint: BrowserFingerprintWithHeaders
|
||||
): Promise<string> {
|
||||
try {
|
||||
const sessionDir = path.join(__dirname, '../browser/', sessionPath, email)
|
||||
const fingerprintFileName = isMobile ? 'session_fingerprint_mobile.json' : 'session_fingerprint_desktop.json'
|
||||
|
||||
if (!fs.existsSync(sessionDir)) {
|
||||
await fs.promises.mkdir(sessionDir, { recursive: true })
|
||||
}
|
||||
|
||||
await fs.promises.writeFile(path.join(sessionDir, fingerprintFileName), JSON.stringify(fingerpint))
|
||||
|
||||
return sessionDir
|
||||
} catch (error) {
|
||||
throw new Error(error as string)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user