parent
47e2dbdd12
commit
4f0b08ea93
8 changed files with 163 additions and 12 deletions
@ -0,0 +1,41 @@ |
|||||||
|
name: Update Sponsors |
||||||
|
|
||||||
|
on: |
||||||
|
workflow_dispatch: # allow manual run |
||||||
|
schedule: |
||||||
|
- cron: '0 */3 * * *' # every 3 hours |
||||||
|
|
||||||
|
env: |
||||||
|
SPONSOR_SHEET_API_KEY: ${{ secrets.SPONSOR_SHEET_API_KEY }} |
||||||
|
SPONSOR_SHEET_ID: ${{ secrets.SPONSOR_SHEET_ID }} |
||||||
|
|
||||||
|
jobs: |
||||||
|
update-sponsors: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v2 |
||||||
|
- uses: actions/setup-node@v3 |
||||||
|
with: |
||||||
|
node-version: 18 |
||||||
|
- uses: pnpm/action-setup@v2.2.2 |
||||||
|
with: |
||||||
|
version: 7.13.4 |
||||||
|
- name: Update the Sponsors |
||||||
|
run: | |
||||||
|
pnpm install |
||||||
|
node bin/roadmap-ads.cjs |
||||||
|
- name: Create PR |
||||||
|
uses: peter-evans/create-pull-request@v4 |
||||||
|
with: |
||||||
|
delete-branch: false |
||||||
|
branch: "update-sponsors" |
||||||
|
base: "master" |
||||||
|
labels: | |
||||||
|
sponsors |
||||||
|
automated pr |
||||||
|
reviewers: kamranahmedse |
||||||
|
commit-message: "chore: update sponsors" |
||||||
|
title: "Upgrade Sponsor Banners" |
||||||
|
body: | |
||||||
|
Updates sponsor banners. |
||||||
|
Please review the changes and merge if everything looks good. |
@ -0,0 +1,109 @@ |
|||||||
|
const path = require('path'); |
||||||
|
const fs = require('fs'); |
||||||
|
const yaml = require('js-yaml'); |
||||||
|
|
||||||
|
const apiKey = process.env.SPONSOR_SHEET_API_KEY; |
||||||
|
const sheetId = process.env.SPONSOR_SHEET_ID; |
||||||
|
|
||||||
|
if (!apiKey || !sheetId) { |
||||||
|
console.error('Missing API key or sheet ID'); |
||||||
|
process.exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
const sheetRange = 'A3:I1001'; |
||||||
|
const sheetUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetRange}?key=${apiKey}`; |
||||||
|
|
||||||
|
function populateRoadmapAds({ |
||||||
|
roadmapUrl, |
||||||
|
company, |
||||||
|
redirectUrl, |
||||||
|
imageUrl, |
||||||
|
adTitle, |
||||||
|
adDescription, |
||||||
|
startDate, |
||||||
|
endDate, |
||||||
|
isActive, |
||||||
|
}) { |
||||||
|
const isConfiguredActive = isActive.toLowerCase() === 'yes'; |
||||||
|
|
||||||
|
const currentDate = new Date(); |
||||||
|
const isDateInRange = currentDate >= new Date(startDate) && currentDate <= new Date(endDate); |
||||||
|
const shouldShowAd = isConfiguredActive && isDateInRange; |
||||||
|
|
||||||
|
// get id from the roadmap URL |
||||||
|
const roadmapId = roadmapUrl |
||||||
|
.split('/') |
||||||
|
.pop() |
||||||
|
.replace(/\?.+?$/, ''); |
||||||
|
|
||||||
|
const roadmapFilePath = path.join(__dirname, '../src/data/roadmaps', `${roadmapId}/${roadmapId}.md`); |
||||||
|
|
||||||
|
if (!fs.existsSync(roadmapFilePath)) { |
||||||
|
console.error(`Roadmap file not found: ${roadmapFilePath}`); |
||||||
|
process.exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
console.log(`Updating roadmap: ${roadmapId}`); |
||||||
|
const roadmapFileContent = fs.readFileSync(roadmapFilePath, 'utf8'); |
||||||
|
|
||||||
|
const frontMatterRegex = /---\n([\s\S]*?)\n---/; |
||||||
|
|
||||||
|
const existingFrontmatter = roadmapFileContent.match(frontMatterRegex)[1]; |
||||||
|
const contentWithoutFrontmatter = roadmapFileContent.replace(frontMatterRegex, ``).trim(); |
||||||
|
|
||||||
|
const yamlObject = yaml.load(existingFrontmatter); |
||||||
|
|
||||||
|
if (shouldShowAd) { |
||||||
|
yamlObject.sponsor = { |
||||||
|
url: redirectUrl, |
||||||
|
title: adTitle, |
||||||
|
imageUrl, |
||||||
|
description: adDescription, |
||||||
|
event: { |
||||||
|
category: 'SponsorClick', |
||||||
|
action: `${company} Redirect`, |
||||||
|
label: `Clicked ${company} Link`, |
||||||
|
}, |
||||||
|
}; |
||||||
|
} else { |
||||||
|
delete yamlObject.sponsor; |
||||||
|
} |
||||||
|
|
||||||
|
const newFrontmatter = yaml.dump(yamlObject, { lineWidth: 10000, forceQuotes: true, quotingType: '"' }); |
||||||
|
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`; |
||||||
|
|
||||||
|
fs.writeFileSync(roadmapFilePath, newContent, 'utf8'); |
||||||
|
} |
||||||
|
|
||||||
|
fetch(sheetUrl) |
||||||
|
.then((res) => res.json()) |
||||||
|
.then((rawData) => { |
||||||
|
const rows = rawData.values; |
||||||
|
|
||||||
|
rows.map((row) => { |
||||||
|
// prettier-ignore |
||||||
|
const [ |
||||||
|
roadmapUrl, |
||||||
|
company, |
||||||
|
redirectUrl, |
||||||
|
imageUrl, |
||||||
|
adTitle, |
||||||
|
adDescription, |
||||||
|
startDate, |
||||||
|
endDate, |
||||||
|
isActive, |
||||||
|
] = row; |
||||||
|
|
||||||
|
populateRoadmapAds({ |
||||||
|
roadmapUrl, |
||||||
|
company, |
||||||
|
redirectUrl, |
||||||
|
imageUrl, |
||||||
|
adTitle, |
||||||
|
adDescription, |
||||||
|
startDate, |
||||||
|
endDate, |
||||||
|
isActive, |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
Loading…
Reference in new issue