136 lines
3.8 KiB
JavaScript
136 lines
3.8 KiB
JavaScript
const https = require('https');
|
|
const fs = require('fs');
|
|
|
|
// 获取 GitHub 趋势项目
|
|
function fetchGitHubTrending() {
|
|
return new Promise((resolve, reject) => {
|
|
const options = {
|
|
hostname: 'api.github.com',
|
|
path: '/search/repositories?q=stars:%3E1000&sort=stars&order=desc&per_page=10',
|
|
headers: {
|
|
'User-Agent': 'Node.js'
|
|
}
|
|
};
|
|
|
|
https.get(options, (res) => {
|
|
let data = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
try {
|
|
const result = JSON.parse(data);
|
|
const trendingProjects = result.items.map(item => ({
|
|
name: item.full_name,
|
|
description: item.description || 'No description',
|
|
stars: item.stargazers_count,
|
|
language: item.language || 'Unknown',
|
|
url: item.html_url
|
|
}));
|
|
|
|
resolve(trendingProjects);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
}).on('error', (error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
// 生成文本文件
|
|
async function generateTrendingFile() {
|
|
try {
|
|
console.log('正在获取 GitHub 趋势项目...');
|
|
const projects = await fetchGitHubTrending();
|
|
|
|
let content = 'GitHub 趋势项目 (基于星标数排名)\n';
|
|
content += '='.repeat(50) + '\n\n';
|
|
|
|
projects.forEach((project, index) => {
|
|
content += `${index + 1}. ${project.name}\n`;
|
|
content += ` 描述: ${project.description}\n`;
|
|
content += ` 语言: ${project.language}\n`;
|
|
content += ` 星标: ${project.stars}\n`;
|
|
content += ` 链接: ${project.url}\n\n`;
|
|
});
|
|
|
|
fs.writeFileSync('github_trending.txt', content, 'utf8');
|
|
console.log('文件已生成: github_trending.txt');
|
|
|
|
} catch (error) {
|
|
console.error('获取数据时出错:', error.message);
|
|
// 创建示例数据以防 API 调用失败
|
|
const fallbackContent = `GitHub 趋势项目 (示例数据)
|
|
==================================================
|
|
|
|
1. microsoft/vscode
|
|
描述: Visual Studio Code
|
|
语言: TypeScript
|
|
星标: 158000
|
|
链接: https://github.com/microsoft/vscode
|
|
|
|
2. facebook/react
|
|
描述: A declarative, efficient, and flexible JavaScript library for building user interfaces.
|
|
语言: JavaScript
|
|
星标: 218000
|
|
链接: https://github.com/facebook/react
|
|
|
|
3. tensorflow/tensorflow
|
|
描述: An Open Source Machine Learning Framework for Everyone
|
|
语言: C++
|
|
星标: 180000
|
|
链接: https://github.com/tensorflow/tensorflow
|
|
|
|
4. kubernetes/kubernetes
|
|
描述: Production-Grade Container Scheduling and Management
|
|
语言: Go
|
|
星标: 105000
|
|
链接: https://github.com/kubernetes/kubernetes
|
|
|
|
5. golang/go
|
|
描述: The Go programming language
|
|
语言: Go
|
|
星标: 118000
|
|
链接: https://github.com/golang/go
|
|
|
|
6. flutter/flutter
|
|
描述: Flutter makes it easy and fast to build beautiful apps for mobile and beyond
|
|
语言: Dart
|
|
星标: 162000
|
|
链接: https://github.com/flutter/flutter
|
|
|
|
7. denoland/deno
|
|
描述: A modern runtime for JavaScript and TypeScript.
|
|
语言: Rust
|
|
星标: 92000
|
|
链接: https://github.com/denoland/deno
|
|
|
|
8. rust-lang/rust
|
|
描述: Empowering everyone to build reliable and efficient software.
|
|
语言: Rust
|
|
星标: 89000
|
|
链接: https://github.com/rust-lang/rust
|
|
|
|
9. vercel/next.js
|
|
描述: The React Framework for the Web
|
|
语言: JavaScript
|
|
星标: 125000
|
|
链接: https://github.com/vercel/next.js
|
|
|
|
10. nodejs/node
|
|
描述: Node.js JavaScript runtime
|
|
语言: JavaScript
|
|
星标: 102000
|
|
链接: https://github.com/nodejs/node`;
|
|
|
|
fs.writeFileSync('github_trending.txt', fallbackContent, 'utf8');
|
|
console.log('已生成示例数据文件: github_trending.txt');
|
|
}
|
|
}
|
|
|
|
// 执行
|
|
generateTrendingFile(); |