From 24c67d7ce47dcb95790391687ffdd2d9aba21e08 Mon Sep 17 00:00:00 2001 From: Theo Date: Wed, 7 Aug 2024 11:40:19 +0800 Subject: [PATCH] Create npm-downloads.yml --- .github/workflows/npm-downloads.yml | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 .github/workflows/npm-downloads.yml diff --git a/.github/workflows/npm-downloads.yml b/.github/workflows/npm-downloads.yml new file mode 100644 index 00000000..9cebb0a3 --- /dev/null +++ b/.github/workflows/npm-downloads.yml @@ -0,0 +1,87 @@ +name: NPM Downloads to Bark + +on: + schedule: + - cron: '30 1 * * *' # 每天 UTC 时间 1:30 运行一次(对应中国时间早上 9:30) + workflow_dispatch: # 允许手动触发工作流 + +jobs: + send-downloads: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 'lts' + + - name: Install pnpm + run: npm install -g pnpm + + - name: Install dependencies + run: pnpm add axios + + - name: Fetch and send NPM downloads + env: + BARK_KEY: ${{ secrets.BARK_KEY }} + REPO_NAME: ${{ github.repository }} + DATE: ${{ steps.set-date.outputs.date }} + run: | + const axios = require('axios'); + + const packageName = 'tmfe'; + const barkUrl = process.env.BARK_KEY; + + async function getNpmDownloads(packageName) { + const url = `https://api.npmjs.org/downloads/range/last-month/${packageName}`; + try { + const response = await axios.get(url); + const downloads = response.data.downloads; + return downloads; + } catch (error) { + console.error('Error fetching npm downloads:', error); + return []; + } + } + + async function main() { + const downloads = await getNpmDownloads(packageName); + + if (downloads.length === 0) { + console.error('No download data found'); + return; + } + + const today = new Date().toISOString().split('T')[0]; + const todayDownloads = downloads.find(download => download.day === today); + + if (!todayDownloads) { + console.error('No download data for today'); + return; + } + + const title = `NPM 下载量 (${today})`; + const body = `包 ${packageName} 今天的下载量是 ${todayDownloads.downloads}`; + + const curlCommand = `curl -X "POST" "${barkUrl}" \ + -H 'Content-Type: application/json; charset=utf-8' \ + -d '{ + "body": "${packageName} 于 ${today} 的下载量是 ${todayDownloads.downloads}", + "title": "Github Actions", + "badge": 1, + "category": "Github Actions", + "sound": "multiwayinvitation.caf", + "icon": "https://authy.com/wp-content/uploads/npm-logo.png", + "group": "Github Actions" + }'`; + + const { execSync } = require('child_process'); + execSync(curlCommand); + + console.log('Notification sent to Bark'); + } + + main();