diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml deleted file mode 100644 index f42dbff..0000000 --- a/.forgejo/workflows/build.yml +++ /dev/null @@ -1,87 +0,0 @@ -name: build-and-push - -on: - push: - branches: - - main - -jobs: - lint: - runs-on: self-hosted - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: 20 - - name: Install dependencies - run: npm ci - - name: Run ESLint - run: npm run lint - - sonar: - needs: lint - runs-on: self-hosted - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: 20 - - name: Install Sonar Scanner (npm) - run: npm install -g sonarqube-scanner - - name: SonarQube Scan - env: - SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }} - run: | - WORKDIR=${GITHUB_WORKSPACE:-$PWD} - HOST_URL=${SONAR_HOST_URL:?SONAR_HOST_URL secret not set} - PROJECT_KEY=${SONAR_PROJECT_KEY:-} - if [ -z "$PROJECT_KEY" ] && [ -f sonar-project.properties ]; then - PROJECT_KEY=$(grep -E '^sonar.projectKey=' sonar-project.properties | cut -d= -f2 | tr -d '\r') - fi - if [ -z "$PROJECT_KEY" ]; then - echo "SONAR_PROJECT_KEY secret not set and no sonar-project.properties entry found" >&2 - exit 1 - fi - echo "Sonar project key: $PROJECT_KEY" - echo "Listing workspace:" - ls -la - echo "Sample files:" - find . -maxdepth 2 -type f | head -n 20 - echo "Running local sonar-scanner..." - sonar-scanner \ - -Dsonar.host.url="$HOST_URL" \ - -Dsonar.token="$SONAR_TOKEN" \ - -Dsonar.projectKey="$PROJECT_KEY" \ - -Dsonar.sources=. \ - -Dsonar.scm.disabled=true \ - -Dsonar.projectBaseDir="$WORKDIR" - - docker: - needs: [lint, sonar] - runs-on: self-hosted - steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Checkout - uses: actions/checkout@v4 - - - name: Login to registry - run: echo "${{ secrets.REG_TOKEN }}" | docker login "${{ secrets.REGISTRY }}" -u "${{ secrets.REG_USER }}" --password-stdin - - - name: Build image - run: | - docker buildx build --load \ - -t "${{ secrets.REGISTRY_IMAGE }}:latest" . - - - name: Push image - run: | - docker push "${{ secrets.REGISTRY_IMAGE }}:latest" diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..9a0d965 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,13 @@ +# These are supported funding model platforms + +github: vogler # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: fgc # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: vogler # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: vogler # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry +custom: ["https://www.buymeacoffee.com/vogler", "https://paypal.me/voglerr"] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..1b47972 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,28 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "npm" + directory: "/" + schedule: + interval: "weekly" + # commit-message: + # prefix: "npm" + # include: "scope" + - package-ecosystem: "docker" + directory: "/" + schedule: + interval: "weekly" + # commit-message: + # prefix: "docker" + # include: "scope" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + # commit-message: + # prefix: "github-actions" + # include: "scope" diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 0000000..ecfd5ff --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "enabled": false, + "extends": [ + "config:recommended" + ] +} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..8c12487 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,72 @@ +name: Build and push Docker image (amd64, arm64 to hub.docker.com and ghcr.io) + +on: + workflow_dispatch: # allows manual trigger + push: # push on branch + branches: [main, dev] + paths: # ignore changes to .md files + - '**' + - '!*.md' + # - '!.github/**' + pull_request: # runs when opened/reopned or when the head branch is updated + +permissions: + contents: read + packages: write + +env: + BRANCH: ${{ github.head_ref || github.ref_name }} # head_ref/base_ref are only set for PRs, for branches ref_name will be used + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v4 + - + name: Set environment variables + run: | + echo "NOW=$(date -R)" >> $GITHUB_ENV # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S' + if [[ "$BRANCH" == "main" ]]; then + echo "IMAGE_TAG=latest" >> $GITHUB_ENV + else + echo "IMAGE_TAG=$BRANCH" >> $GITHUB_ENV + fi + - + name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - + name: Login to Docker Hub + uses: docker/login-action@v3 + # if: ${{ secrets.DOCKERHUB_USERNAME != '' && secrets.DOCKERHUB_TOKEN != '' }} # does not work: Unrecognized named-value: 'secrets' - https://www.cloudtruth.com/blog/skipping-jobs-in-github-actions-when-secrets-are-unavailable-securely-inject-configuration-secrets-into-github + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} # actor is user that opened PR, was repository_owner before + password: ${{ secrets.GITHUB_TOKEN }} + - + name: Build and push + uses: docker/build-push-action@v6 + if: ${{ env.IMAGE_TAG != '' }} + with: + context: . + push: ${{ secrets.DOCKERHUB_USERNAME != '' }} + build-args: | + COMMIT=${{ github.sha }} + BRANCH=${{ env.BRANCH }} + NOW=${{ env.NOW }} + platforms: linux/amd64,linux/arm64 + tags: | + ${{ secrets.DOCKERHUB_USERNAME }}/free-games-claimer:${{env.IMAGE_TAG}} + ghcr.io/${{ github.actor }}/free-games-claimer:${{env.IMAGE_TAG}} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..02ca3cb --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,36 @@ +# https://github.com/marketplace/actions/super-linter#get-started +name: Lint + +on: # yamllint disable-line rule:truthy + push: null + pull_request: null + +permissions: {} + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + + permissions: + contents: read + packages: read + # To report GitHub Actions status checks + statuses: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + # super-linter needs the full git history to get the + # list of files that changed across commits + fetch-depth: 0 + + - name: Super-linter + uses: super-linter/super-linter/slim@v7.4.0 # x-release-please-version + # TODO need to create problem matchers for each linter? https://github.com/rhysd/actionlint/blob/v1.7.7/docs/usage.md#problem-matchers + env: + # To report GitHub Actions status checks + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # TODO automatically fix linting issues and commit them for PRs + # fix-lint-issues: # https://github.com/marketplace/actions/super-linter#github-actions-workflow-example-pull-request diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml new file mode 100644 index 0000000..e11a854 --- /dev/null +++ b/.github/workflows/sonar.yml @@ -0,0 +1,42 @@ +name: Sonar + +on: + # Trigger analysis when pushing in main or pull requests, and when creating a pull request. + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened] + +permissions: + contents: read + +jobs: + sonarcloud: + runs-on: ubuntu-latest + steps: + - + uses: actions/checkout@v4 + with: + # Disabling shallow clone is recommended for improving relevancy of reporting. Otherwise sonarcloud will show a warning. + fetch-depth: 0 + - + uses: actions/setup-node@v6 + with: + cache: 'npm' + - + name: Install dev dependencies which includde ESLint + plugins + run: npm install --only=dev + - + name: Run ESLint + continue-on-error: true + run: npx eslint . -f json -o eslint_report.json + - + name: Fix ESLint paths + run: sed -i 's+/home/runner/work/free-games-claimer/free-games-claimer+/github/workspace+g' eslint_report.json + - + name: SonarCloud Scan + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/Dockerfile b/Dockerfile index cac91cf..a8c5e24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,8 @@ RUN apt-get update \ novnc websockify \ dos2unix \ python3-pip \ + # && npx playwright install-deps firefox \ + && apt-get install --no-install-recommends -y \ libgtk-3-0 \ libasound2 \ libxcomposite1 \ @@ -34,9 +36,6 @@ RUN apt-get update \ libgdk-pixbuf-2.0-0 \ libdbus-glib-1-2 \ libxcursor1 \ - libnss3 \ - libnspr4 \ - libgbm1 \ && apt-get autoremove -y \ && apt-get clean \ && rm -rf \ @@ -44,10 +43,13 @@ RUN apt-get update \ /usr/share/doc/* \ /var/cache/* \ /var/lib/apt/lists/* \ - /var/tmp/* \ - && useradd -ms /bin/bash fgc \ - && ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html \ - && pip install apprise + /var/tmp/* + +# RUN node --version +# RUN npm --version + +RUN ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html +RUN pip install apprise WORKDIR /fgc COPY package*.json ./ @@ -59,15 +61,10 @@ RUN npm install # From 1.38 Playwright will no longer install browser automatically for playwright, but apparently still for playwright-firefox: https://github.com/microsoft/playwright/releases/tag/v1.38.0 # RUN npx playwright install firefox -# Only copy the files we actually need in the image to avoid accidentally adding secrets. -COPY *.js ./ -COPY eslint.config.js jsconfig.json sonar-project.properties ./ -COPY src ./src -COPY test ./test -COPY docker-entrypoint.sh ./ +COPY . . # Shell scripts need Linux line endings. On Windows, git might be configured to check out dos/CRLF line endings, so we convert them for those people in case they want to build the image. They could also use --config core.autocrlf=input -RUN dos2unix ./*.sh && chmod +x ./*.sh && chown -R fgc:fgc /fgc +RUN dos2unix ./*.sh && chmod +x ./*.sh COPY docker-entrypoint.sh /usr/local/bin/ ARG COMMIT="" @@ -90,9 +87,8 @@ LABEL org.opencontainers.image.title="free-games-claimer" \ # Configure VNC via environment variables: ENV VNC_PORT 5900 ENV NOVNC_PORT 6080 -# Ports are not exposed by default; publish explicitly with -p when you really need GUI access. -# EXPOSE 5900 -# EXPOSE 6080 +EXPOSE 5900 +EXPOSE 6080 # Configure Xvfb via environment variables: ENV WIDTH 1920 @@ -102,8 +98,6 @@ ENV DEPTH 24 # Show browser instead of running headless ENV SHOW 1 -USER fgc - # Script to setup display server & VNC is always executed. ENTRYPOINT ["docker-entrypoint.sh"] # Default command to run. This is replaced by appending own command, e.g. `docker run ... node prime-gaming` to only run this script. diff --git a/README.md b/README.md index a8fae1f..17ec854 100644 --- a/README.md +++ b/README.md @@ -1,88 +1,224 @@ -Free Games Claimer (Fork) -========================== +
+
+
- const gamesTitle = page.locator('p.offer-filters__button__title:has-text("Games"), p.offer-filters__button__title:has-text("einlösen")');
- if (await gamesTitle.count()) {
- const btn = gamesTitle.first().locator('xpath=ancestor::button[1]');
- if (await btn.count()) {
- await btn.click();
- return;
- }
- }
- // New Luna claims home already shows games list
- };
-
- await openGamesTab();
-
- const locateGamesList = async () => {
- const selectors = [
- 'div[data-a-target="offer-list-FGWP_FULL"]', // old layout
- '[data-testid="offer-list"]',
- '[data-test-selector="offer-list"]',
- 'section:has(h2:has-text("Games with Prime"))',
- 'section:has(h2:has-text("Games"))',
- ];
- for (const sel of selectors) {
- const loc = page.locator(sel).first();
- if (await loc.count()) return loc;
- }
- return null;
- };
-
- const games = await locateGamesList();
- // Load all cards (old and new layout) by scrolling the container or the page
- if (games) await scrollUntilStable(() => games.evaluate(el => el.scrollHeight).catch(() => 0));
- await scrollUntilStable(() => page.evaluate(() => globalThis.document?.scrollingElement?.scrollHeight ?? 0));
-
- const normalizeClaimUrl = url => {
- if (!url) return { url, key: null };
- const m = url.match(/(https?:\/\/[^/]+)?(\/claims\/[^?#]+)/);
- if (!m) return { url, key: url };
- const path = m[2];
- const slug = path.split('/')[2];
- return { url: 'https://luna.amazon.com' + path, key: slug || path };
- };
-
- const cards = [];
- // New layout: direct claim buttons on cards (FGWPOffer) with "Spiel aktivieren"/"Claim"
- const fgwps = page.locator('[data-a-target="FGWPOffer"]');
- if (await fgwps.count()) {
- for (const handle of await fgwps.elementHandles()) {
- const href = await handle.getAttribute('href');
- const { url, key } = normalizeClaimUrl(href?.startsWith('/') ? href : href || '');
- const title =
- await handle.$eval('p[title], span[title]', el => el.getAttribute('title')).catch(() => null) ||
- await handle.$eval('p, span', el => el.textContent).catch(() => null) ||
- key ||
- 'Unknown title';
- cards.push({ kind: 'external', title, url, key });
- }
- }
-
- const anchorClaims = page.locator('a[href*="/claims/"][href*="amzn1.pg.item"]');
- if (await anchorClaims.count()) {
- const hrefs = [...new Set(await anchorClaims.evaluateAll(anchors => anchors.map(a => a.getAttribute('href')).filter(Boolean)))];
- for (const href of hrefs) {
- const { url, key } = normalizeClaimUrl(href);
- const title = key || await anchorClaims.first().innerText() || 'Unknown title';
- cards.push({ kind: 'external', title, url, key });
- }
- }
-
- if (!cards.length && games) {
- const cardLocator = games.locator([
- '[data-testid="offer-card"]',
- '[data-test-selector="offer-card"]',
- '.item-card__action',
- ].join(','));
- if (await cardLocator.count() === 0) {
- console.log('No games found in list.');
- } else {
- for (const handle of await cardLocator.elementHandles()) {
- const text = (await handle.textContent() || '').toLowerCase();
- if (text.includes('collected')) continue; // skip already claimed
- const title = await (await handle.$('h3, h4, [data-testid="item-card-title"], [data-test-selector="item-card-title"], .item-card-details__body__primary'))?.innerText() || 'Unknown title';
- const linkEl = await handle.$('a[href]');
- let url = linkEl && await linkEl.getAttribute('href');
- if (url?.startsWith('/')) url = 'https://luna.amazon.com' + url;
- const { url: normUrl, key } = normalizeClaimUrl(url);
- const hasLinkClaim = await handle.$('a:has-text("Claim"), a:has-text("Get"), a:has-text("Details")');
- const hasButtonClaim = await handle.$('button:has-text("Claim"), button:has-text("Get"), button:has-text("Get game"), button:has-text("Play")');
- const hasGermanCTA = await handle.$(':is(button,p,a):has-text("Spiel aktivieren"), :is(button,p,a):has-text("Spiel holen")');
- if (hasLinkClaim || hasGermanCTA) cards.push({ kind: 'external', title, url: normUrl, key });
- else if (hasButtonClaim) cards.push({ kind: 'internal', title, url: normUrl, key, handle });
- }
- }
- }
-
- // dedup by URL to avoid duplicates from multiple selectors
- const seenUrl = new Set();
- const uniq = cards.filter(c => {
- const key = c.key || c.url || c.title;
- if (seenUrl.has(key)) return false;
- seenUrl.add(key);
- return true;
- });
-
- const internal = uniq.filter(c => c.kind == 'internal');
- const external = uniq.filter(c => c.kind == 'external');
+ await page.click('button[data-type="Game"]');
+ const games = page.locator('div[data-a-target="offer-list-FGWP_FULL"]');
+ await games.waitFor();
+ // await scrollUntilStable(() => games.locator('.item-card__action').count()); // number of games
+ await scrollUntilStable(() => page.evaluate(() => document.querySelector('.tw-full-width').scrollHeight)); // height may change during loading while number of games is still the same?
+ console.log('Number of already claimed games (total):', await games.locator('p:has-text("Collected")').count());
+ // can't use .all() since the list of elements via locator will change after click while we iterate over it
+ const internal = await games.locator('.item-card__action:has(button[data-a-target="FGWPOffer"])').elementHandles();
+ const external = await games.locator('.item-card__action:has(a[data-a-target="FGWPOffer"])').all();
// bottom to top: oldest to newest games
internal.reverse();
external.reverse();
- const sameOrNewPage = async url => {
+ const sameOrNewPage = async url => new Promise(async (resolve, _reject) => {
const isNew = page.url() != url;
let p = page;
if (isNew) {
p = await context.newPage();
await p.goto(url, { waitUntil: 'domcontentloaded' });
}
- return [p, isNew];
- };
+ resolve([p, isNew]);
+ });
const skipBasedOnTime = async url => {
+ // console.log(' Checking time left for game:', url);
const [p, isNew] = await sameOrNewPage(url);
- const dueDateLoc = p.locator('.availability-date .tw-bold, [data-testid="availability-end-date"], [data-test-selector="availability-end-date"]');
- if (!await dueDateLoc.count()) {
- if (isNew) await p.close();
- return false;
- }
- const dueDateOrg = await dueDateLoc.first().innerText();
+ const dueDateOrg = await p.locator('.availability-date .tw-bold').innerText();
const dueDate = new Date(Date.parse(dueDateOrg + ' 17:00'));
- const daysLeft = (dueDate.getTime() - Date.now()) / 1000 / 60 / 60 / 24;
- const availabilityText = await p.locator('.availability-date, [data-testid="availability-end-date"], [data-test-selector="availability-end-date"]').first().innerText().catch(() => dueDateOrg);
- console.log(' ', availabilityText, '->', daysLeft.toFixed(2));
+ const daysLeft = (dueDate.getTime() - Date.now())/1000/60/60/24;
+ console.log(' ', await p.locator('.availability-date').innerText(), '->', daysLeft.toFixed(2));
if (isNew) await p.close();
return daysLeft > cfg.pg_timeLeft;
- };
+ }
console.log('\nNumber of free unclaimed games (Prime Gaming):', internal.length);
// claim games in internal store
for (const card of internal) {
- await card.handle.scrollIntoViewIfNeeded();
- const title = card.title;
- const url = card.url;
+ await card.scrollIntoViewIfNeeded();
+ const title = await (await card.$('.item-card-details__body__primary')).innerText();
+ const slug = await (await card.$('a')).getAttribute('href');
+ const url = 'https://gaming.amazon.com' + slug.split('?')[0];
console.log('Current free game:', chalk.blue(title));
- if (cfg.pg_timeLeft && url && await skipBasedOnTime(url)) continue;
+ if (cfg.pg_timeLeft && await skipBasedOnTime(url)) continue;
if (cfg.dryrun) continue;
- if (cfg.interactive) {
- const confirmed = await confirm();
- if (!confirmed) continue;
- }
- await card.handle.locator('.tw-button:has-text("Claim"), .tw-button:has-text("Get"), button:has-text("Claim"), button:has-text("Get")').first().click();
+ if (cfg.interactive && !await confirm()) continue;
+ await (await card.$('.tw-button:has-text("Claim")')).click();
db.data[user][title] ||= { title, time: datetime(), url, store: 'internal' };
notify_games.push({ title, status: 'claimed', url });
- await card.handle.screenshot({ path: screenshot('internal', `${filenamify(title)}.png`) });
+ // const img = await (await card.$('img.tw-image')).getAttribute('src');
+ // console.log('Image:', img);
+ await card.screenshot({ path: screenshot('internal', `${filenamify(title)}.png`) });
}
console.log('\nNumber of free unclaimed games (external stores):', external.length);
// claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com, microsoft
const external_info = [];
for (const card of external) { // need to get data incl. URLs in this loop and then navigate in another, otherwise .all() would update after coming back and .elementHandles() like above would lead to error due to page navigation: elementHandle.$: Protocol error (Page.adoptNode)
- const title = card.title;
- const url = card.url ? card.url.split('?')[0] : undefined;
- if (!url) continue;
+ const title = await card.locator('.item-card-details__body__primary').innerText();
+ const slug = await card.locator('a:has-text("Claim")').first().getAttribute('href');
+ const url = 'https://gaming.amazon.com' + slug.split('?')[0];
+ // await (await card.$('text=Claim')).click(); // goes to URL of game, no need to wait
external_info.push({ title, url });
}
- const clickCTA = async p => {
- const candidates = [
- p.locator('button[data-a-target="buy-box_call-to-action"]').first(),
- p.locator('[data-a-target="buy-box_call-to-action"]').first(),
- p.locator('[data-a-target="buy-box"] .tw-button:has-text("Get game")').first(),
- p.locator('[data-a-target="buy-box"] .tw-button:has-text("Claim")').first(),
- p.locator('.tw-button:has-text("Complete Claim")').first(),
- p.locator('[data-a-target="buy-box_call-to-action-text"]').first().locator('xpath=ancestor::button[1]'),
- p.locator('.tw-button:has-text("Spiel holen"), .tw-button:has-text("Spiel aktivieren")').first(),
- p.locator('p:has-text("Spiel holen"), p:has-text("Spiel aktivieren")').first().locator('xpath=ancestor::button[1]'),
- ];
- for (const c of candidates) {
- if (await c.count()) {
- try {
- await c.waitFor({ state: 'visible', timeout: 5000 });
- const enabled = await c.isEnabled();
- if (enabled) await c.click();
- else {
- await c.evaluate(el => {
- el.disabled = false;
- el.removeAttribute('disabled');
- el.click();
- });
- }
- return true;
- } catch {
- // try next candidate
- }
- }
- }
- return false;
- };
-
+ // external_info = [ { title: 'Fallout 76 (XBOX)', url: 'https://gaming.amazon.com/fallout-76-xbox-fgwp/dp/amzn1.pg.item.9fe17d7b-b6c2-4f58-b494-cc4e79528d0b?ingress=amzn&ref_=SM_Fallout76XBOX_S01_FGWP_CRWN' } ];
for (const { title, url } of external_info) {
console.log('Current free game:', chalk.blue(title)); // , url);
- const existingStatus = db.data[user]?.[title]?.status;
- if (existingStatus && !existingStatus.startsWith('failed')) {
- console.log(` Already recorded as ${existingStatus}, skipping.`);
- notify_games.push({ title, url, status: 'existed' });
- continue;
- }
await page.goto(url, { waitUntil: 'domcontentloaded' });
- await page.waitForSelector('[data-a-target="buy-box"]', { timeout: 10000 }).catch(() => {});
if (cfg.debug) await page.pause();
- let store = 'unknown';
- const detailLoc = page.locator('[data-a-target="DescriptionItemDetails"], [data-testid="DescriptionItemDetails"]');
- if (await detailLoc.count()) {
- const item_text = await detailLoc.first().innerText();
- const lower = item_text.toLowerCase();
- const onPos = lower.lastIndexOf(' on ');
- if (onPos >= 0) store = lower.slice(onPos + 4).replace(/[.!]$/, '');
- } else if (url.includes('/claims/')) {
- const slug = url.split('/claims/')[1]?.split('/')[0] || '';
- if (slug.includes('gog')) store = 'gog.com';
- else if (slug.includes('epic')) store = 'epic-games';
- else if (slug.includes('origin')) store = 'origin';
- else if (slug.includes('xbox') || slug.includes('microsoft')) store = 'microsoft store';
- else if (slug.includes('legacy')) store = 'legacy games';
- const lunaPlay = await page.locator('[data-a-target="LunaOffer"], button[data-a-target="LunaOffer"], button:has-text("Spielen")').count();
- if (store == 'unknown' && lunaPlay) store = 'luna';
- }
+ const item_text = await page.innerText('[data-a-target="DescriptionItemDetails"]');
+ const store = item_text.toLowerCase().replace(/.* on /, '').slice(0, -1);
console.log(' External store:', store);
- const notify_game = { title, url };
- notify_games.push(notify_game); // status is updated below
- // Already collected? skip
- const collectedLoc = page.locator('[data-a-target="ClaimStateQuantityAndDateContent"], [data-a-target="ClaimStateClaimCodeContent"]:has-text("Collected"), [data-a-target="ClaimStateVendorContent"], [data-a-target="ClaimStateViewDetailsAndInstructions"]');
- const collectedText = page.getByText(/You collected this on/i);
- const collectedEpic = page.getByText(/Sent to your Epic Games Store library/i);
- const collectedBanner = page.locator('p.tw-c-text-alert-success:has-text("Collected"), p.tw-c-text-alert-success:has-text("Collected this")');
- const collectedSuccessIcon = page.locator('[data-a-target="ItemCardDetailSuccessStatus"], .claim-state__success-icon');
- const disabledCTA = page.locator('[data-a-target="buy-box_call-to-action"][disabled], button[disabled]:has-text("Get game")');
- const collectedAny = await Promise.all([
- collectedLoc.count(),
- collectedBanner.count(),
- collectedText.count(),
- collectedEpic.count(),
- collectedSuccessIcon.count(),
- disabledCTA.count(),
- ]).then(([a, b, c, d, e, f]) => a + b + c + d + e + f > 0);
- if (collectedAny) {
- console.log(' Already collected, skipping.');
- notify_game.status = 'existed';
- db.data[user][title] ||= { title, time: datetime(), url, store, status: 'existed' };
- continue;
- }
- // Disabled CTA (e.g., needs linking or not available)
- if (await disabledCTA.count()) {
- if (store !== 'epic-games') {
- console.log(' CTA is disabled, skipping (likely needs linking/not available).');
- notify_game.status = 'disabled';
- db.data[user][title] ||= { title, time: datetime(), url, store, status: 'disabled' };
- continue;
- } else {
- console.log(' CTA disabled for epic-games, will still try to link/claim.');
- }
- }
- if (store == 'luna') {
- console.log(' Luna cloud title detected, skipping code redemption.');
- notify_game.status = 'luna (play)';
- db.data[user][title] ||= { title, time: datetime(), url, store: 'luna', status: 'luna (play)' };
- continue;
- }
if (cfg.pg_timeLeft && await skipBasedOnTime(url)) continue;
if (cfg.dryrun) continue;
- if (cfg.interactive) {
- const confirmed = await confirm();
- if (!confirmed) continue;
- }
- await clickCTA(page);
- await Promise.any([
- page.waitForSelector('.thank-you-title:has-text("Success")', { timeout: cfg.timeout }).catch(() => {}),
- page.waitForSelector('div:has-text("Link game account")', { timeout: cfg.timeout }).catch(() => {}),
- ]).catch(() => {});
+ if (cfg.interactive && !await confirm()) continue;
+ await Promise.any([page.click('[data-a-target="buy-box"] .tw-button:has-text("Get game")'), page.click('[data-a-target="buy-box"] .tw-button:has-text("Claim")'), page.click('.tw-button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")'), page.waitForSelector('.thank-you-title:has-text("Success")')]); // waits for navigation
db.data[user][title] ||= { title, time: datetime(), url, store };
- if (await page.locator('div:has-text("Link game account")').count() // epic games store also shows "Link account"
+ const notify_game = { title, url };
+ notify_games.push(notify_game); // status is updated below
+ if (await page.locator('div:has-text("Link game account")').count() // TODO still needed? epic games store just has 'Link account' as the button text now.
|| await page.locator('div:has-text("Link account")').count()) {
console.error(' Account linking is required to claim this offer!');
notify_game.status = `failed: need account linking for ${store}`;
db.data[user][title].status = 'failed: need account linking';
// await page.pause();
// await page.click('[data-a-target="LinkAccountModal"] [data-a-target="LinkAccountButton"]');
- // login for epic games also needed if already logged in
+ // TODO login for epic games also needed if already logged in
// wait for https://www.epicgames.com/id/authorize?redirect_uri=https%3A%2F%2Fservice.link.amazon.gg...
// await page.click('button[aria-label="Allow"]');
} else {
db.data[user][title].status = 'claimed';
// print code if there is one
const redeem = {
- // 'origin': 'https://www.origin.com/redeem', // kept for legacy flows; current path uses account linking
+ // 'origin': 'https://www.origin.com/redeem', // TODO still needed or now only via account linking?
'gog.com': 'https://www.gog.com/redeem',
'microsoft store': 'https://account.microsoft.com/billing/redeem',
xbox: 'https://account.microsoft.com/billing/redeem',
'legacy games': 'https://www.legacygames.com/primedeal',
};
if (store in redeem) { // did not work for linked origin: && !await page.locator('div:has-text("Successfully Claimed")').count()
- let code;
- try {
- // ensure CTA was clicked in case code is behind it
- await clickCTA(page).catch(() => {});
- code = await Promise.any([
- page.inputValue('input[type="text"]'),
- page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', '')),
- ]);
- } catch {
- console.error(' Could not find claim code on page (timeout). Please check manually.');
- db.data[user][title].status = 'claimed (code not found)';
- notify_game.status = 'claimed (code not found)';
- await page.screenshot({ path: screenshot('external', `${filenamify(title)}_nocode.png`), fullPage: true }).catch(() => {});
- continue;
- }
+ const code = await Promise.any([page.inputValue('input[type="text"]'), page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', ''))]); // input: Legacy Games; text: gog.com
console.log(' Code to redeem game:', chalk.blue(code));
if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/
redeem[store] = await (await page.$('li:has-text("Click here") a')).getAttribute('href'); // full text: Click here to enter your redemption code.
@@ -510,9 +229,13 @@ try {
const page2 = await context.newPage();
await page2.goto(redeem[store], { waitUntil: 'domcontentloaded' });
if (store == 'gog.com') {
+ // await page.goto(`https://redeem.gog.com/v1/bonusCodes/${code}`); // {"reason":"Invalid or no captcha"}
await page2.fill('#codeInput', code);
+ // wait for responses before clicking on Continue and then Redeem
+ // first there are requests with OPTIONS and GET to https://redeem.gog.com/v1/bonusCodes/XYZ?language=de-DE
const r1 = page2.waitForResponse(r => r.request().method() == 'GET' && r.url().startsWith('https://redeem.gog.com/'));
await page2.click('[type="submit"]'); // click Continue
+ // console.log(await page2.locator('.warning-message').innerText()); // does not exist if there is no warning
const r1t = await (await r1).text();
const reason = JSON.parse(r1t).reason;
// {"reason":"Invalid or no captcha"}
@@ -527,9 +250,11 @@ try {
} else if (reason == 'code_not_found') {
redeem_action = 'redeem (not found)';
console.error(' Code was not found!');
- } else { // unknown state; keep info log for later analysis
+ } else { // TODO not logged in? need valid unused code to test.
redeem_action = 'redeemed?';
+ // console.log(' Redeemed successfully? Please report your Responses (if new) in https://github.com/vogler/free-games-claimer/issues/5');
console.debug(` Response 1: ${r1t}`);
+ // then after the click on Redeem there is a POST request which should return {} if claimed successfully
const r2 = page2.waitForResponse(r => r.request().method() == 'POST' && r.url().startsWith('https://redeem.gog.com/'));
await page2.click('[type="submit"]'); // click Redeem
const r2t = await (await r2).text();
@@ -548,6 +273,7 @@ try {
}
} else if (store == 'microsoft store' || store == 'xbox') {
console.error(` Redeem on ${store} is experimental!`);
+ // await page2.pause();
if (page2.url().startsWith('https://login.')) {
console.error(' Not logged in! Please redeem the code above manually. You can now login in the browser for next time. Waiting for 60s.');
await page2.waitForTimeout(60 * 1000);
@@ -558,7 +284,9 @@ try {
await input.waitFor();
await input.fill(code);
const r = page2.waitForResponse(r => r.url().startsWith('https://cart.production.store-web.dynamics.com/v1.0/Redeem/PrepareRedeem'));
+ // console.log(await page2.locator('.redeem_code_error').innerText());
const rt = await (await r).text();
+ // {"code":"NotFound","data":[],"details":[],"innererror":{"code":"TokenNotFound",...
const j = JSON.parse(rt);
const reason = j?.events?.cart.length && j.events.cart[0]?.data?.reason;
if (reason == 'TokenNotFound') {
@@ -572,24 +300,26 @@ try {
if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') {
redeem_action = 'already redeemed';
console.error(' error: UserAlreadyOwnsContent');
- } else { // success path not seen yet; log below if needed
+ } else if (true) { // TODO what's returned on success?
redeem_action = 'redeemed';
db.data[user][title].status = 'claimed and redeemed?';
console.log(' Redeemed successfully? Please report if not in https://github.com/vogler/free-games-claimer/issues/5');
}
- } else { // other responses; keep info log for analysis
+ } else { // TODO find out other responses
redeem_action = 'unknown';
console.debug(` Response: ${rt}`);
console.log(' Redeemed successfully? Please report your Response from above (if it is new) in https://github.com/vogler/free-games-claimer/issues/5');
}
}
} else if (store == 'legacy games') {
+ // await page2.pause();
await page2.fill('[name=coupon_code]', code);
await page2.fill('[name=email]', cfg.lg_email);
await page2.fill('[name=email_validate]', cfg.lg_email);
await page2.uncheck('[name=newsletter_sub]');
await page2.click('[type="submit"]');
try {
+ // await page2.waitForResponse(r => r.url().startsWith('https://promo.legacygames.com/promotion-processing/order-management.php')); // status code 302
await page2.waitForSelector('h2:has-text("Thanks for redeeming")');
redeem_action = 'redeemed';
db.data[user][title].status = 'claimed and redeemed';
@@ -610,18 +340,18 @@ try {
notify_game.status = `claimed on ${store}`;
db.data[user][title].status = 'claimed';
}
+ // save screenshot of potential code just in case
await page.screenshot({ path: screenshot('external', `${filenamify(title)}.png`), fullPage: true });
+ // console.info(' Saved a screenshot of page to', p);
}
+ // await page.pause();
}
await page.goto(URL_CLAIM, { waitUntil: 'domcontentloaded' });
- try {
- await page.click('button[data-type="Game"]');
- } catch {
- // ignore if filter already selected
- }
+ await page.click('button[data-type="Game"]');
- if (notify_games.length && games) { // make screenshot of all games if something was claimed and list exists
+ if (notify_games.length) { // make screenshot of all games if something was claimed
const p = screenshot(`${filenamify(datetime())}.png`);
+ // await page.screenshot({ path: p, fullPage: true }); // fullPage does not make a difference since scroll not on body but on some element
await scrollUntilStable(() => games.locator('.item-card__action').count());
const viewportSize = page.viewportSize(); // current viewport size
await page.setViewportSize({ ...viewportSize, height: 3000 }); // increase height, otherwise element screenshot is cut off at the top and bottom
@@ -636,7 +366,7 @@ try {
await loot.waitFor();
process.stdout.write('Loading all DLCs on page...');
- await scrollUntilStable(() => loot.locator('[data-a-target="item-card"]').count());
+ await scrollUntilStable(() => loot.locator('[data-a-target="item-card"]').count())
console.log('\nNumber of already claimed DLC:', await loot.locator('p:has-text("Collected")').count());
@@ -664,30 +394,17 @@ try {
await page.goto(url, { waitUntil: 'domcontentloaded' });
// most games have a button 'Get in-game content'
// epic-games: Fall Guys: Claim -> Continue -> Go to Epic Games (despite account linked and logged into epic-games) -> not tied to account but via some cookie?
- const claimOptions = [
- page.click('.tw-button:has-text("Get in-game content")'),
- page.click('.tw-button:has-text("Claim your gift")'),
- (async () => {
- await page.click('.tw-button:has-text("Claim")');
- await page.click('button:has-text("Continue")').catch(() => {});
- })(),
- ];
- await Promise.any(claimOptions);
- try {
- await page.click('button:has-text("Continue")');
- } catch {
- // continue button not always present
- }
+ await Promise.any([page.click('.tw-button:has-text("Get in-game content")'), page.click('.tw-button:has-text("Claim your gift")'), page.click('.tw-button:has-text("Claim")').then(() => page.click('button:has-text("Continue")'))]);
+ page.click('button:has-text("Continue")').catch(_ => { });
const linkAccountButton = page.locator('[data-a-target="LinkAccountButton"]');
let unlinked_store;
if (await linkAccountButton.count()) {
unlinked_store = await linkAccountButton.first().getAttribute('aria-label');
console.debug(' LinkAccountButton label:', unlinked_store);
- const match = unlinked_store?.match(/Link (.*) account/);
- const extracted = match?.[1];
- if (extracted) unlinked_store = extracted;
+ const match = unlinked_store.match(/Link (.*) account/);
+ if (match && match.length == 2) unlinked_store = match[1];
} else if (await page.locator('text=Link game account').count()) { // epic-games only?
- console.error(' Missing account linking (epic-games specific button?):', await page.locator('button[data-a-target="gms-cta"]').innerText()); // track account-linking UI drift
+ console.error(' Missing account linking (epic-games specific button?):', await page.locator('button[data-a-target="gms-cta"]').innerText()); // TODO needed?
unlinked_store = 'epic-games';
}
if (unlinked_store) {
@@ -695,11 +412,13 @@ try {
dlc_unlinked[unlinked_store] ??= [];
dlc_unlinked[unlinked_store].push(title);
} else {
- const code = await page.inputValue('input[type="text"]').catch(() => undefined);
+ const code = await page.inputValue('input[type="text"]').catch(_ => undefined);
console.log(' Code to redeem game:', chalk.blue(code));
db.data[user][title].code = code;
db.data[user][title].status = 'claimed';
+ // notify_game.status = `${redeem_action} ${code} on ${store}`;
}
+ // await page.pause();
} catch (error) {
console.error(error);
} finally {
diff --git a/src/migrate.js b/src/migrate.js
index e28e88d..b2db945 100644
--- a/src/migrate.js
+++ b/src/migrate.js
@@ -1,4 +1,4 @@
-import { existsSync } from 'node:fs';
+import { existsSync } from 'fs';
import { Low } from 'lowdb';
import { JSONFile } from 'lowdb/node';
import { datetime } from './util.js';
@@ -18,6 +18,7 @@ const datetime_UTCtoLocalTimezone = async file => {
db.data[user][game].time = time2;
}
}
+ // console.log(db.data);
await db.write(); // write out json db
};
diff --git a/src/util.js b/src/util.js
index dab047f..308952d 100644
--- a/src/util.js
+++ b/src/util.js
@@ -19,9 +19,7 @@ export const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
export const datetimeUTC = (d = new Date()) => d.toISOString().replace('T', ' ').replace('Z', '');
// same as datetimeUTC() but for local timezone, e.g., UTC + 2h for the above in DE
export const datetime = (d = new Date()) => datetimeUTC(new Date(d.getTime() - d.getTimezoneOffset() * 60000));
-export const filenamify = s => s
- .replaceAll(':', '.')
- .replaceAll(/[^a-z0-9 _\-.]/gi, '_'); // alternative: https://www.npmjs.com/package/filenamify - On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods are reserved.
+export const filenamify = s => s.replaceAll(':', '.').replace(/[^a-z0-9 _\-.]/gi, '_'); // alternative: https://www.npmjs.com/package/filenamify - On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods are reserved.
export const handleSIGINT = (context = null) => process.on('SIGINT', async () => { // e.g. when killed by Ctrl-C
console.error('\nInterrupted by SIGINT. Exit!'); // Exception shows where the script was:\n'); // killed before catch in docker...
@@ -92,50 +90,42 @@ export const stealth = async context => {
// alternative inquirer is big (node_modules 29MB, enquirer 9.7MB, prompts 9.8MB, none 9.4MB) and slower
// open issue: prevents handleSIGINT() to work if prompt is cancelled with Ctrl-C instead of Escape: https://github.com/enquirer/enquirer/issues/372
import Enquirer from 'enquirer'; const enquirer = new Enquirer();
-const timeoutHint = () => 'timeout';
-const cancelPromptWithHint = prompt => {
- prompt.hint = timeoutHint;
- prompt.cancel();
+const timeoutPlugin = timeout => enquirer => { // cancel prompt after timeout ms
+ enquirer.on('prompt', prompt => {
+ const t = setTimeout(() => {
+ prompt.hint = () => 'timeout';
+ prompt.cancel();
+ }, timeout);
+ prompt.on('submit', _ => clearTimeout(t));
+ prompt.on('cancel', _ => clearTimeout(t));
+ });
};
-const applyPromptTimeout = (prompt, timeout) => {
- if (!timeout) return;
- const timer = setTimeout(cancelPromptWithHint, timeout, prompt);
- const clearTimer = () => clearTimeout(timer);
- prompt.on('submit', clearTimer);
- prompt.on('cancel', clearTimer);
-};
-// cancel prompt after timeout ms; can be disabled per prompt via options.timeout = 0
-const timeoutPlugin = defaultTimeout => enquirerInstance => {
- const onPrompt = prompt => applyPromptTimeout(prompt, prompt.options?.timeout ?? defaultTimeout);
- enquirerInstance.on('prompt', onPrompt);
-};
-enquirer.use(timeoutPlugin(cfg.login_timeout));
+enquirer.use(timeoutPlugin(cfg.login_timeout)); // TODO may not want to have this timeout for all prompts; better extend Prompt and add a timeout prompt option
// single prompt that just returns the non-empty value instead of an object
// @ts-ignore
-export const prompt = o => enquirer.prompt({ name: 'name', type: 'input', message: 'Enter value', ...o }).then(r => r.name).catch(() => {});
+export const prompt = o => enquirer.prompt({ name: 'name', type: 'input', message: 'Enter value', ...o }).then(r => r.name).catch(_ => {});
export const confirm = o => prompt({ type: 'confirm', message: 'Continue?', ...o });
// notifications via apprise CLI
-import { execFile } from 'node:child_process';
+import { execFile } from 'child_process';
import { cfg } from './config.js';
-export const notify = html => new Promise(resolve => {
+export const notify = html => new Promise((resolve, reject) => {
if (!cfg.notify) {
if (cfg.debug) console.debug('notify: NOTIFY is not set!');
return resolve();
}
- const appriseBin = process.env.APPRISE_BIN || '/usr/local/bin/apprise';
+ // const cmd = `apprise '${cfg.notify}' ${title} -i html -b '${html}'`; // this had problems if e.g. ' was used in arg; could have `npm i shell-escape`, but instead using safer execFile which takes args as array instead of exec which spawned a shell to execute the command
const args = [cfg.notify, '-i', 'html', '-b', `'${html}'`];
- if (cfg.notify_title) args.push('-t', cfg.notify_title);
- if (cfg.debug) console.debug(`${appriseBin} ${args.join(' ')}`); // this also doesn't escape, but it's just for info
- execFile(appriseBin, args, (error, stdout, stderr) => {
+ if (cfg.notify_title) args.push(...['-t', cfg.notify_title]);
+ if (cfg.debug) console.debug(`apprise ${args.map(a => `'${a}'`).join(' ')}`); // this also doesn't escape, but it's just for info
+ execFile('apprise', args, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
if (error.message.includes('command not found')) {
console.info('Run `pip install apprise`. See https://github.com/vogler/free-games-claimer#notifications');
}
- // don't fail the whole run on notification errors
- return resolve();
+ return reject(error);
}
if (stderr) console.error(`stderr: ${stderr}`);
if (stdout) console.log(`stdout: ${stdout}`);
diff --git a/src/version.js b/src/version.js
index f838d8f..bfcd12a 100644
--- a/src/version.js
+++ b/src/version.js
@@ -1,14 +1,15 @@
-import { log } from 'node:console';
-import { execFile } from 'node:child_process';
+// check if running the latest version
-const gitBin = process.env.GIT_BIN || '/usr/bin/git';
+import { log } from 'console';
+import { exec } from 'child_process';
-const runGit = (...args) => new Promise((resolve, reject) => {
- execFile(gitBin, args, { cwd: process.cwd() }, (error, stdout, stderr) => {
+const execp = cmd => new Promise((resolve, reject) => {
+ exec(cmd, (error, stdout, stderr) => {
if (stderr) console.error(`stderr: ${stderr}`);
+ // if (stdout) console.log(`stdout: ${stdout}`);
if (error) {
console.log(`error: ${error.message}`);
- if (error.code === 'ENOENT' || error.message.includes('command not found')) {
+ if (error.message.includes('command not found')) {
console.info('Install git to check for updates!');
}
return reject(error);
@@ -17,7 +18,10 @@ const runGit = (...args) => new Promise((resolve, reject) => {
});
});
+// const git_main = () => readFileSync('.git/refs/heads/main').toString().trim();
+
let sha, date;
+// if (existsSync('/.dockerenv')) { // did not work
if (process.env.NOVNC_PORT) {
log('Running inside Docker.');
['COMMIT', 'BRANCH', 'NOW'].forEach(v => log(` ${v}:`, process.env[v]));
@@ -25,15 +29,24 @@ if (process.env.NOVNC_PORT) {
date = process.env.NOW;
} else {
log('Not running inside Docker.');
- sha = await runGit('rev-parse', 'HEAD');
- date = await runGit('show', '-s', '--format=%cD'); // same as format as `date -R` (RFC2822)
+ sha = await execp('git rev-parse HEAD');
+ date = await execp('git show -s --format=%cD'); // same as format as `date -R` (RFC2822)
+ // date = await execp('git show -s --format=%ch'); // %ch is same as --date=human (short/relative)
}
-const gh = await (await fetch('https://api.github.com/repos/vogler/free-games-claimer/commits/main')).json();
+const gh = await (await fetch('https://api.github.com/repos/vogler/free-games-claimer/commits/main', {
+ // headers: { accept: 'application/vnd.github.VERSION.sha' }
+})).json();
+// log(gh);
log('Local commit:', sha, new Date(date));
log('Online commit:', gh.sha, new Date(gh.commit.committer.date));
+// git describe --all --long --dirty
+// --> heads/main-0-gdee47d2-dirty
+// git describe --tags --long --dirty
+// --> v1.7-35-gdee47d2-dirty
+
if (sha == gh.sha) {
log('Running the latest version!');
} else {
diff --git a/steam-games.js b/steam-games.js
index b23fd31..9b307fc 100644
--- a/steam-games.js
+++ b/steam-games.js
@@ -12,22 +12,24 @@ import { FingerprintInjector } from 'fingerprint-injector';
import { FingerprintGenerator } from 'fingerprint-generator';
const { fingerprint, headers } = new FingerprintGenerator().getFingerprint({
- devices: ['desktop'],
- operatingSystems: ['windows'],
+ devices: ["desktop"],
+ operatingSystems: ["windows"],
});
const context = await firefox.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless,
+ // viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL
userAgent: fingerprint.navigator.userAgent,
viewport: {
- width: fingerprint.screen.width,
- height: fingerprint.screen.height,
+ width: fingerprint.screen.width,
+ height: fingerprint.screen.height,
},
extraHTTPHeaders: {
- 'accept-language': headers['accept-language'],
+ 'accept-language': headers['accept-language'],
},
});
+// await stealth(context);
await new FingerprintInjector().attachFingerprintToPlaywright(context, { fingerprint, headers });
context.setDefaultTimeout(cfg.debug ? 0 : cfg.timeout);
@@ -59,6 +61,7 @@ try {
db.data[title] = stat;
}
+ // await page.pause();
} catch (error) {
process.exitCode ||= 1;
console.error('--- Exception:');
diff --git a/test/notify.js b/test/notify.js
index c5709a9..6d89086 100644
--- a/test/notify.js
+++ b/test/notify.js
@@ -1,3 +1,4 @@
+/* eslint-disable no-constant-condition */
import { delay, html_game_list, notify } from '../src/util.js';
import { cfg } from '../src/config.js';
@@ -5,38 +6,33 @@ const URL_CLAIM = 'https://gaming.amazon.com/home'; // dummy URL
console.debug('NOTIFY:', cfg.notify);
-const scenarios = [
- {
- enabled: process.env.TEST_NOTIFY_EPIC === '1',
- title: 'epic-games',
- games: [
- { title: 'Epistory - Typing Chronicles', status: 'claimed', url: URL_CLAIM },
- ],
- },
- {
- enabled: process.env.TEST_NOTIFY_PG === '1',
- delayMs: 1000,
- title: 'prime-gaming',
- games: [
- { title: 'Faraway 2: Jungle Escape', status: 'claimed', url: URL_CLAIM },
- { title: 'Chicken Police - Paint it RED!', status: 'claimed', url: URL_CLAIM },
- { title: 'Lawn Mowing Simulator', status: 'claimed', url: URL_CLAIM },
- { title: 'Breathedge', status: 'claimed', url: URL_CLAIM },
- { title: 'The Evil Within 2', status: `redeem H97S6FB38FA6D09DEA on gog.com`, url: URL_CLAIM },
- { title: 'Beat Cop', status: `redeem BMKM8558EC55F7B38F on gog.com`, url: URL_CLAIM },
- { title: 'Dishonored 2', status: `redeem NNEK0987AB20DFBF8F on gog.com`, url: URL_CLAIM },
- ],
- },
- {
- enabled: process.env.TEST_NOTIFY_GOG === '1',
- delayMs: 1000,
- title: 'gog',
- games: [{ title: 'Haven Park', status: 'claimed', url: URL_CLAIM }],
- },
-];
-
-for (const scenario of scenarios) {
- if (!scenario.enabled) continue;
- if (scenario.delayMs) await delay(scenario.delayMs);
- await notify(`${scenario.title}:
${html_game_list(scenario.games)}`);
+if (true) {
+ const notify_games = [
+ // { title: 'Kerbal Space Program', status: 'claimed', url: URL_CLAIM },
+ // { title: "Shadow Tactics - Aiko's Choice", status: 'claimed', url: URL_CLAIM },
+ { title: 'Epistory - Typing Chronicles', status: 'claimed', url: URL_CLAIM },
+ ];
+ await notify(`epic-games:
${html_game_list(notify_games)}`);
+}
+
+if (false) {
+ await delay(1000);
+ const notify_games = [
+ { title: 'Faraway 2: Jungle Escape', status: 'claimed', url: URL_CLAIM },
+ { title: 'Chicken Police - Paint it RED!', status: 'claimed', url: URL_CLAIM },
+ { title: 'Lawn Mowing Simulator', status: 'claimed', url: URL_CLAIM },
+ { title: 'Breathedge', status: 'claimed', url: URL_CLAIM },
+ { title: 'The Evil Within 2', status: `redeem H97S6FB38FA6D09DEA on gog.com`, url: URL_CLAIM },
+ { title: 'Beat Cop', status: `redeem BMKM8558EC55F7B38F on gog.com`, url: URL_CLAIM },
+ { title: 'Dishonored 2', status: `redeem NNEK0987AB20DFBF8F on gog.com`, url: URL_CLAIM },
+ ];
+ notify(`prime-gaming:
${html_game_list(notify_games)}`);
+}
+
+if (false) {
+ await delay(1000);
+ const notify_games = [
+ { title: 'Haven Park', status: 'claimed', url: URL_CLAIM },
+ ];
+ notify(`gog:
${html_game_list(notify_games)}`);
}
diff --git a/test/sigint-enquirer-raw-keeps-running.js b/test/sigint-enquirer-raw-keeps-running.js
index ccb3081..23d9983 100644
--- a/test/sigint-enquirer-raw-keeps-running.js
+++ b/test/sigint-enquirer-raw-keeps-running.js
@@ -12,10 +12,10 @@ function onRawSIGINT(fn) {
}
});
}
-console.log(1);
+console.log(1)
onRawSIGINT(() => {
console.log('raw'); process.exit(1);
});
-console.log(2);
+console.log(2)
// onRawSIGINT workaround for enquirer keeps the process from exiting here...
diff --git a/test/sigint-enquirer-raw.js b/test/sigint-enquirer-raw.js
index f9f365c..e6b538d 100644
--- a/test/sigint-enquirer-raw.js
+++ b/test/sigint-enquirer-raw.js
@@ -1,14 +1,37 @@
// https://github.com/enquirer/enquirer/issues/372
import { prompt, handleSIGINT } from '../src/util.js';
+// const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
+// console.log('\nInterrupted by SIGINT. Exit!');
+// process.exitCode = 130;
+// });
handleSIGINT();
+function onRawSIGINT(fn) {
+ const { stdin, stdout } = process;
+ stdin.setRawMode(true);
+ stdin.resume();
+ stdin.on('data', data => {
+ const key = data.toString('utf-8');
+ if (key === '\u0003') { // ctrl + c
+ fn();
+ } else {
+ stdout.write(key);
+ }
+ });
+}
+// onRawSIGINT(() => {
+// console.log('raw'); process.exit(1);
+// });
+
console.log('hello');
console.error('hello error');
try {
- const first = await prompt(); // SIGINT no longer handled if this is executed
- const second = await prompt(); // SIGINT no longer handled if this is executed
- console.log('values:', first, second);
+ let i = 'foo';
+ i = await prompt(); // SIGINT no longer handled if this is executed
+ i = await prompt(); // SIGINT no longer handled if this is executed
+ // handleSIGINT();
+ console.log('value:', i);
setTimeout(() => console.log('timeout 3s'), 3000);
} catch (e) {
process.exitCode ||= 1;
diff --git a/unrealengine.js b/unrealengine.js
index 6eb7d79..2bb8ee9 100644
--- a/unrealengine.js
+++ b/unrealengine.js
@@ -1,7 +1,10 @@
+// TODO This is mostly a copy of epic-games.js
+// New assets to claim every first Tuesday of a month.
+
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra
import { authenticator } from 'otplib';
-import path from 'node:path';
-import { writeFileSync } from 'node:fs';
+import path from 'path';
+import { writeFileSync } from 'fs';
import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js';
@@ -18,7 +21,8 @@ const db = await jsonDb('unrealengine.json', {});
const context = await firefox.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless,
viewport: { width: cfg.width, height: cfg.height },
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36', // Windows UA avoids "device not supported"; update when browser version changes
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36', // see replace of Headless in util.newStealthContext. TODO Windows UA enough to avoid 'device not supported'? update if browser is updated?
+ // userAgent for firefox: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0
locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/ue-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
@@ -32,7 +36,8 @@ await stealth(context);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
-await page.setViewportSize({ width: cfg.width, height: cfg.height }); // workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
+await page.setViewportSize({ width: cfg.width, height: cfg.height }); // TODO workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
+// console.debug('userAgent:', await page.evaluate(() => navigator.userAgent));
const notify_games = [];
let user;
@@ -55,32 +60,23 @@ try {
const email = cfg.eg_email || await prompt({ message: 'Enter email' });
const password = email && (cfg.eg_password || await prompt({ type: 'password', message: 'Enter password' }));
if (email && password) {
+ // await page.click('text=Sign in with Epic Games');
await page.fill('#email', email);
await page.click('button[type="submit"]');
await page.fill('#password', password);
await page.click('button[type="submit"]');
- const watchCaptchaDuringLogin = async () => {
- try {
- await page.waitForSelector('#h_captcha_challenge_login_prod iframe', { timeout: 15000 });
- console.error('Got a captcha during login (likely due to too many attempts)! You may solve it in the browser, get a new IP or try again in a few hours.');
- notify('unrealengine: got captcha during login. Please check.');
- } catch {
- return;
- }
- };
- const watchMfa = async () => {
- try {
- await page.waitForURL('**/id/login/mfa**', { timeout: cfg.login_timeout });
- console.log('Enter the security code to continue - This appears to be a new device, browser or location. A security code has been sent to your email address at ...');
- const otp = cfg.eg_otpkey && authenticator.generate(cfg.eg_otpkey) || await prompt({ type: 'text', message: 'Enter two-factor sign in code', validate: n => n.toString().length == 6 || 'The code must be 6 digits!' }); // can't use type: 'number' since it strips away leading zeros and codes sometimes have them
- await page.locator('input[name="code-input-0"]').pressSequentially(otp.toString());
- await page.click('button[type="submit"]');
- } catch {
- return;
- }
- };
- watchCaptchaDuringLogin();
- watchMfa();
+ page.waitForSelector('#h_captcha_challenge_login_prod iframe').then(() => {
+ console.error('Got a captcha during login (likely due to too many attempts)! You may solve it in the browser, get a new IP or try again in a few hours.');
+ notify('unrealengine: got captcha during login. Please check.');
+ }).catch(_ => { });
+ // handle MFA, but don't await it
+ page.waitForURL('**/id/login/mfa**').then(async () => {
+ console.log('Enter the security code to continue - This appears to be a new device, browser or location. A security code has been sent to your email address at ...');
+ // TODO locator for text (email or app?)
+ const otp = cfg.eg_otpkey && authenticator.generate(cfg.eg_otpkey) || await prompt({ type: 'text', message: 'Enter two-factor sign in code', validate: n => n.toString().length == 6 || 'The code must be 6 digits!' }); // can't use type: 'number' since it strips away leading zeros and codes sometimes have them
+ await page.locator('input[name="code-input-0"]').pressSequentially(otp.toString());
+ await page.click('button[type="submit"]');
+ }).catch(_ => { });
} else {
console.log('Waiting for you to login in the browser.');
await notify('unrealengine: no longer signed in and not enough options set for automatic login.');
@@ -127,7 +123,7 @@ try {
}
ids.push(id);
}
- if (ids.length === 0) {
+ if (!ids.length) {
console.log('Nothing to claim');
} else {
await page.waitForTimeout(2000);
@@ -139,22 +135,18 @@ try {
notify('unrealengine: ' + err);
process.exit(1);
}
+ // await page.pause();
console.log('Click shopping cart');
await page.locator('.shopping-cart').click();
+ // await page.waitForTimeout(2000);
await page.locator('button.checkout').click();
console.log('Click checkout');
// maybe: Accept End User License Agreement
- const acceptEulaIfPresent = async () => {
- try {
- await page.locator('[name=accept-label]').check({ timeout: 10000 });
- console.log('Accept End User License Agreement');
- await page.locator('span:text-is("Accept")').click(); // otherwise matches 'Accept All Cookies'
- } catch {
- return;
- }
- };
- acceptEulaIfPresent();
- await page.waitForSelector('#webPurchaseContainer iframe');
+ page.locator('[name=accept-label]').check().then(() => {
+ console.log('Accept End User License Agreement');
+ page.locator('span:text-is("Accept")').click(); // otherwise matches 'Accept All Cookies'
+ }).catch(_ => { });
+ await page.waitForSelector('#webPurchaseContainer iframe'); // TODO needed?
const iframe = page.frameLocator('#webPurchaseContainer iframe');
if (cfg.debug) await page.pause();
@@ -169,27 +161,14 @@ try {
// I Agree button is only shown for EU accounts! https://github.com/vogler/free-games-claimer/pull/7#issuecomment-1038964872
const btnAgree = iframe.locator('button:has-text("I Agree")');
- const acceptIfRequired = async () => {
- try {
- await btnAgree.waitFor({ timeout: 10000 });
- await btnAgree.click();
- } catch {
- return;
- }
- }; // EU: wait for and click 'I Agree'
- acceptIfRequired();
+ btnAgree.waitFor().then(() => btnAgree.click()).catch(_ => { }); // EU: wait for and click 'I Agree'
try {
// context.setDefaultTimeout(100 * 1000); // give time to solve captcha, iframe goes blank after 60s?
const captcha = iframe.locator('#h_captcha_challenge_checkout_free_prod iframe');
- const watchCaptchaChallenge = async () => {
- try {
- await captcha.waitFor({ timeout: 10000 });
- console.error(' Got hcaptcha challenge! Lost trust due to too many login attempts? You can solve the captcha in the browser or get a new IP address.');
- } catch {
- return;
- }
- }; // may time out if not shown
- watchCaptchaChallenge();
+ captcha.waitFor().then(async () => { // don't await, since element may not be shown
+ // console.info(' Got hcaptcha challenge! NopeCHA extension will likely solve it.')
+ console.error(' Got hcaptcha challenge! Lost trust due to too many login attempts? You can solve the captcha in the browser or get a new IP address.');
+ }).catch(_ => { }); // may time out if not shown
await page.waitForSelector('text=Thank you');
for (const id of ids) {
db.data[user][id].status = 'claimed';
@@ -197,12 +176,16 @@ try {
}
notify_games.forEach(g => g.status == 'failed' && (g.status = 'claimed'));
console.log('Claimed successfully!');
+ // context.setDefaultTimeout(cfg.timeout);
} catch (e) {
console.log(e);
+ // console.error(' Failed to claim! Try again if NopeCHA timed out. Click the extension to see if you ran out of credits (refill after 24h). To avoid captchas try to get a new IP or set a cookie from https://www.hcaptcha.com/accessibility');
console.error(' Failed to claim! To avoid captchas try to get a new IP address.');
await page.screenshot({ path: screenshot('failed', `${filenamify(datetime())}.png`), fullPage: true });
+ // db.data[user][id].status = 'failed';
notify_games.forEach(g => g.status = 'failed');
}
+ // notify_game.status = db.data[user][game_id].status; // claimed or failed
if (notify_games.length) await page.screenshot({ path: screenshot(`${filenamify(datetime())}.png`), fullPage: false }); // fullPage is quite long...
console.log('Done');