This commit is contained in:
TheNetsky
2023-09-26 12:04:34 +02:00
parent c802492f18
commit 2291245657
7 changed files with 128 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "microsoft-rewards-script",
"version": "1.0.0",
"version": "1.0.1",
"description": "Automatically do tasks for Microsoft Rewards but in TS",
"main": "index.js",
"scripts": {

View File

@@ -4,13 +4,15 @@ import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import { getUserAgent } from './util/UserAgent'
import { loadSesion } from './BrowserFunc'
import { headless } from './config.json'
puppeteer.use(StealthPlugin())
export async function Browser(email: string, headless = false) {
export async function Browser(email: string) {
const userAgent = await getUserAgent(false)
const browser = await puppeteer.launch({
headless: headless, // Set to true for a headless browser
headless: headless,
userDataDir: await loadSesion(email),
args: [
'--no-sandbox',
@@ -22,11 +24,11 @@ export async function Browser(email: string, headless = false) {
return browser
}
export async function mobileBrowser(email: string, headless = false) {
export async function mobileBrowser(email: string) {
const userAgent = await getUserAgent(true)
const browser = await puppeteer.launch({
headless: headless, // Set to true for a headless browser,
headless: headless,
userDataDir: await loadSesion(email),
args: [
'--no-sandbox',

View File

@@ -3,7 +3,7 @@ import fs from 'fs'
import path from 'path'
import { baseURL, sessionPath } from './config.json'
import { wait } from './util/Utils'
import { getFormattedDate, wait } from './util/Utils'
import { tryDismissAllMessages, tryDismissCookieBanner } from './BrowserUtil'
import { log } from './util/Logger'
@@ -13,7 +13,7 @@ import { QuizData } from './interface/QuizData'
export async function goHome(page: Page): Promise<void> {
try {
const targetUrl = new URL(baseURL)
const dashboardURL = new URL(baseURL)
await page.goto(baseURL)
@@ -32,9 +32,9 @@ export async function goHome(page: Page): Promise<void> {
// Continue if element is not found
}
const currentUrl = new URL(page.url())
const currentURL = new URL(page.url())
if (currentUrl.hostname !== targetUrl.hostname) {
if (currentURL.hostname !== dashboardURL.hostname) {
await tryDismissAllMessages(page)
await wait(2000)
@@ -42,7 +42,7 @@ export async function goHome(page: Page): Promise<void> {
}
await wait(5000)
log('MAIN', 'Visited homepage successfully')
log('GO-HOME', 'Visited homepage successfully')
}
} catch (error) {
@@ -51,6 +51,16 @@ export async function goHome(page: Page): Promise<void> {
}
export async function getDashboardData(page: Page): Promise<DashboardData> {
const dashboardURL = new URL(baseURL)
const currentURL = new URL(page.url())
// Should never happen since tasks are opened in a new tab!
if (currentURL.hostname !== dashboardURL.hostname) {
log('DASHBOARD-DATA', 'Provided page did not equal dashboard page, redirecting to dashboard page')
await goHome(page)
}
// Reload the page to get new data
await page.reload({ waitUntil: 'networkidle2' })
const scriptContent = await page.evaluate(() => {
@@ -60,7 +70,7 @@ export async function getDashboardData(page: Page): Promise<DashboardData> {
if (targetScript) {
return targetScript.innerText
} else {
throw new Error('Script containing dashboard data not found')
throw log('GET-DASHBOARD-DATA', 'Script containing dashboard data not found', 'error')
}
})
@@ -73,19 +83,13 @@ export async function getDashboardData(page: Page): Promise<DashboardData> {
if (match && match[1]) {
return JSON.parse(match[1])
} else {
throw new Error('Dashboard data not found in the script')
throw log('GET-DASHBOARD-DATA', 'Dashboard data not found within script', 'error')
}
}, scriptContent)
return dashboardData
}
export async function getSearchPoints(page: Page): Promise<Counters> {
const dashboardData = await getDashboardData(page)
return dashboardData.userStatus.counters
}
export async function getQuizData(page: Page): Promise<QuizData> {
const scriptContent = await page.evaluate(() => {
const scripts = Array.from(document.querySelectorAll('script'))
@@ -94,7 +98,7 @@ export async function getQuizData(page: Page): Promise<QuizData> {
if (targetScript) {
return targetScript.innerText
} else {
throw new Error('Script containing quiz data not found')
throw log('GET-QUIZ-DATA', 'Script containing quiz data not found', 'error')
}
})
@@ -106,13 +110,48 @@ export async function getQuizData(page: Page): Promise<QuizData> {
if (match && match[1]) {
return JSON.parse(match[1])
} else {
throw new Error('Dashboard data not found in the script')
throw log('GET-QUIZ-DATA', 'Quiz data not found within script', 'error')
}
}, scriptContent)
return quizData
}
export async function getSearchPoints(page: Page): Promise<Counters> {
const dashboardData = await getDashboardData(page) // Always fetch newest data
return dashboardData.userStatus.counters
}
export async function getEarnablePoints(data: DashboardData, page: null | Page = null): Promise<number> {
// Fetch new data if page is provided
if (page) {
data = await getDashboardData(page)
}
// These only include the points from tasks that the script can complete!
let totalEarnablePoints = 0
// Desktop Search Points
data.userStatus.counters.pcSearch.forEach(x => totalEarnablePoints += (x.pointProgressMax - x.pointProgress))
// Mobile Search Points
data.userStatus.counters.mobileSearch.forEach(x => totalEarnablePoints += (x.pointProgressMax - x.pointProgress))
// Daily Set
data.dailySetPromotions[getFormattedDate()]?.forEach(x => totalEarnablePoints += (x.pointProgressMax - x.pointProgress))
// More Promotions
data.morePromotions.forEach(x => {
// Only count points from supported activities
if (['quiz', 'urlreward'].includes(x.activityType)) {
totalEarnablePoints += (x.pointProgressMax - x.pointProgress)
}
})
return totalEarnablePoints
}
export async function loadSesion(email: string): Promise<string> {
const sessionDir = path.join(__dirname, sessionPath, email)

View File

@@ -1,4 +1,10 @@
{
"baseURL" : "https://rewards.bing.com",
"sessionPath": "sessions"
"baseURL": "https://rewards.bing.com",
"sessionPath": "sessions",
"headless": false,
"runOnZeroPoints": false,
"searches": {
"doMobile": true,
"doDesktop": true
}
}

View File

@@ -47,10 +47,12 @@ export async function doQuiz(page: Page, data: PromotionalItem | MorePromotion)
}
}
// Click the answers
for (const answer of answers) {
await wait(2000)
// Click the answer on page
await quizPage.click(answer)
await wait(1500)
const refreshSuccess = await waitForQuizRefresh(quizPage)
if (!refreshSuccess) {
@@ -72,7 +74,7 @@ export async function doQuiz(page: Page, data: PromotionalItem | MorePromotion)
if (dataOption === correctOption) {
// Click the answer on page
await quizPage.click(`#rqAnswerOption${i}`)
await wait(1500)
await wait(2000)
const refreshSuccess = await waitForQuizRefresh(quizPage)
if (!refreshSuccess) {
@@ -88,6 +90,7 @@ export async function doQuiz(page: Page, data: PromotionalItem | MorePromotion)
}
// Done with
await wait(2000)
await quizPage.close()
log('QUIZ', 'Completed the quiz successfully')
} catch (error) {

View File

@@ -2,7 +2,7 @@ import { Page } from 'puppeteer'
import axios from 'axios'
import { log } from '../../util/Logger'
import { shuffleArray, wait } from '../../util/Utils'
import { wait } from '../../util/Utils'
import { getSearchPoints } from '../../BrowserFunc'
import { DashboardData, DashboardImpression } from '../../interface/DashboardData'
@@ -29,7 +29,8 @@ export async function doSearch(page: Page, data: DashboardData, mobile: boolean)
}
// Generate search queries
const googleSearchQueries = shuffleArray(await getGoogleTrends(locale, missingPoints))
const googleSearchQueries = await getGoogleTrends(locale, missingPoints)
//const googleSearchQueries = shuffleArray(await getGoogleTrends(locale, missingPoints))
// Open a new tab
const browser = page.browser()
@@ -162,7 +163,7 @@ async function bingSearch(page: Page, searchPage: Page, query: string) {
}
async function getGoogleTrends(locale: string, queryCount: number): Promise<string[]> {
const queryTerms: string[] = []
let queryTerms: string[] = []
let i = 0
while (queryCount > queryTerms.length) {
@@ -193,10 +194,7 @@ async function getGoogleTrends(locale: string, queryCount: number): Promise<stri
}
// Deduplicate the search terms
const uniqueSearchTerms = Array.from(new Set(queryTerms))
queryTerms.length = 0
queryTerms.push(...uniqueSearchTerms)
queryTerms = [...new Set(queryTerms)]
} catch (error) {
log('SEARCH-GOOGLE-TRENDS', 'An error occurred:' + error, 'error')
}

View File

@@ -1,24 +1,33 @@
import { Browser, mobileBrowser } from './Browser'
import { getDashboardData, goHome } from './BrowserFunc'
import { doDailySet } from './functions/DailySet'
import { getDashboardData, getEarnablePoints, goHome } from './BrowserFunc'
import { log } from './util/Logger'
import { login } from './functions/Login'
import { doDailySet } from './functions/DailySet'
import { doMorePromotions } from './functions/MorePromotions'
import { doSearch } from './functions/activities/Search'
import { log } from './util/Logger'
import accounts from './accounts.json'
import { Account } from './interface/Account'
async function init() {
import accounts from './accounts.json'
import { runOnZeroPoints, searches } from './config.json'
let collectedPoints = 0
async function main() {
log('MAIN', 'Bot started')
for (const account of accounts) {
log('MAIN', `Started tasks for account ${account.email}`)
// DailySet, MorePromotions and Desktop Searches
// Desktop Searches, DailySet and More Promotions
await Desktop(account)
// If runOnZeroPoints is false and 0 points to earn, stop and try the next account
if (!runOnZeroPoints && collectedPoints === 0) {
continue
}
// Mobile Searches
await Mobile(account)
@@ -26,34 +35,49 @@ async function init() {
}
// Clean exit
log('MAIN', 'Completed tasks for ALL accounts')
log('MAIN', 'Bot exited')
process.exit()
process.exit(0)
}
// Desktop
async function Desktop(account: Account) {
const browser = await Browser(account.email)
const page = await browser.newPage()
log('MAIN', 'Starting DESKTOP browser')
// Login into MS Rewards
await login(page, account.email, account.password)
await goHome(page)
const data = await getDashboardData(page)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
log('MAIN-POINTS', `Current point count: ${data.userStatus.availablePoints}`)
const earnablePoints = await getEarnablePoints(data)
collectedPoints = earnablePoints
log('MAIN-POINTS', `You can earn ${earnablePoints} points today`)
// If runOnZeroPoints is false and 0 points to earn, don't continue
if (!runOnZeroPoints && collectedPoints === 0) {
log('MAIN', 'No points to earn and "runOnZeroPoints" is set to "false", stopping')
// Close desktop browser
return await browser.close()
}
// Complete daily set
await doDailySet(page, data)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
// Complete more promotions
await doMorePromotions(page, data)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
// Do desktop searches
if (searches.doDesktop) {
await doSearch(page, data, false)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
}
// Close desktop browser
await browser.close()
@@ -63,20 +87,29 @@ async function Mobile(account: Account) {
const browser = await mobileBrowser(account.email)
const page = await browser.newPage()
log('MAIN', 'Starting MOBILE browser')
// Login into MS Rewards
await login(page, account.email, account.password)
await goHome(page)
const data = await getDashboardData(page)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
// Do mobile searches
if (searches.doMobile) {
await doSearch(page, data, true)
log('MAIN', `Current point count: ${data.userStatus.availablePoints}`)
}
// Fetch new points
const earnablePoints = await getEarnablePoints(data, page)
// If the new earnable is 0, means we got all the points, else retract
collectedPoints = earnablePoints === 0 ? collectedPoints : (collectedPoints - earnablePoints)
log('MAIN-POINTS', `The script collected ${collectedPoints} points today`)
// Close mobile browser
await browser.close()
}
init()
// Run main script
main()