Skip to content

Commit

Permalink
Create npm-downloads.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Messi authored Aug 7, 2024
1 parent b2ab658 commit 24c67d7
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/npm-downloads.yml
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 24c67d7

Please sign in to comment.