-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (152 loc) · 7.35 KB
/
index.js
File metadata and controls
175 lines (152 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import axios from 'axios';
import fs from 'fs-extra';
import { Parser } from 'json2csv';
import dotenv from 'dotenv';
dotenv.config();
const GITHUB_API_URL = 'https://api.github.com';
const GITHUB_SEARCH_URI = '/search/repositories';
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
/**
* Fetches the trending repositories from the GitHub API based on certain criteria and saves the repositories to markdown.
*
* @return {Promise<void>} This function does not return anything explicitly.
*/
const fetchTrendingRepos = async () => {
try {
const response = await axios.get(`${GITHUB_API_URL}${GITHUB_SEARCH_URI}`, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
params: {
q: 'created:>=' + getLastWeekDate(),
sort: 'stars',
order: 'desc',
per_page: 20,
},
});
const repos = response.data.items;
const today = new Date().toISOString().split('T')[0];
const year = today.split('-')[0];
const month = today.split('-')[1];
const dateDir = `${dir()}/${year}/${month}`;
fs.ensureDirSync(dateDir);
// saveReposToMarkdown(repos, today, dateDir)
// saveReposToTableInMarkdown(repos, today, dateDir);
saveReposToJson(repos, today, dateDir);
saveReposToCsv(repos, today, dateDir);
} catch (error) {
console.error('Error fetching trending repositories:', error);
}
};
/**
* Get the date of last week in ISO format.
*
* @return {string} The date of last week.
*/
const getLastWeekDate = () => {
const today = new Date();
const lastWeek = new Date(today);
lastWeek.setDate(today.getDate() - 7);
return lastWeek.toISOString().split('T')[0];
};
const dir = () => {
return './docs';
}
/**
* Save the trending GitHub repositories to a markdown file.
*
* @param {Array} repos - The list of repositories to save.
* @return {void} This function does not return anything explicitly.
*/
const saveReposToMarkdown = (repos, today, dateDir) => {
const filePath = `${dateDir}/${today}.md`;
const markdownContent = `# Top 20 Trending GitHub Repositories of the Week (as of ${today})\n\n` +
repos.map((repo, index) => (
`${index + 1}. **[${repo.full_name}](https://github.com/${repo.full_name})** - 🌟 ${repo.stargazers_count}\n` +
` - **Owner**: ${repo.owner.login}\n` +
` - <img src="${repo.owner.avatar_url}" width="100" height="100">\n` +
` - **Description**: ${repo.description || 'No description'}\n` +
` - **Topics**: ${repo.topics.join(', ') || 'No topics'}\n` +
` - **URL**: [${repo.html_url}](${repo.html_url})\n` +
` - **Created At**: ${repo.created_at}\n` +
` - **Updated At**: ${repo.updated_at}\n` +
` - **Pushed At**: ${repo.pushed_at}\n` +
` - **Git URL**: ${repo.git_url}\n` +
` - **SSH URL**: ${repo.ssh_url}\n` +
` - **Clone URL**: ${repo.clone_url}\n` +
` - **SVN URL**: ${repo.svn_url}\n` +
` - **Homepage**: ${repo.homepage || 'No homepage'}\n` +
` - **Size**: ${repo.size}\n` +
` - **Language**: ${repo.language || 'No language specified'}\n` +
` - **Forks Count**: ${repo.forks_count}\n` +
` - **Open Issues Count**: ${repo.open_issues_count}\n` +
` - **Default Branch**: ${repo.default_branch}\n` +
` - **License**: ${repo.license ? repo.license.name : 'No license'}\n`
)).join('\n');
fs.writeFileSync(filePath, markdownContent, 'utf8');
console.log(`Saved trending repos to ${filePath}`);
};
const saveReposToTableInMarkdown = (repos, today, dateDir) => {
const filePath = `${dateDir}/${today}-table.md`;
const markdownContent = `# Top 20 Trending GitHub Repositories of the Week (as of ${today})\n\n` +
'| # | Repository | Stars | Owner | Avatar | Description | Topics | URL | Created At | Updated At | Pushed At | Git URL | SSH URL | Clone URL | SVN URL | Homepage | Size | Language | Forks Count | Open Issues Count | Default Branch | License |\n' +
'|---|------------|-------|-------|--------|-------------|--------|-----|------------|------------|-----------|---------|---------|-----------|---------|----------|------|----------|--------------|-------------------|----------------|---------|\n' +
repos.map((repo, index) => (
`| ${index + 1} | [${repo.full_name}](https://github.com/${repo.full_name}) | ${repo.stargazers_count} | ${repo.owner.login} |  | ${repo.description || 'No description'} | ${repo.topics.join(', ') || 'No topics'} | [${repo.html_url}](${repo.html_url}) | ${repo.created_at} | ${repo.updated_at} | ${repo.pushed_at} | ${repo.git_url} | ${repo.ssh_url} | ${repo.clone_url} | ${repo.svn_url} | ${repo.homepage || 'No homepage'} | ${repo.size} | ${repo.language || 'No language specified'} | ${repo.forks_count} | ${repo.open_issues_count} | ${repo.default_branch} | ${repo.license ? repo.license.name : 'No license'} |`
)).join('\n');
fs.writeFileSync(filePath, markdownContent, 'utf8');
console.log(`Saved trending repos to ${filePath}`);
};
/**
* Saves the trending GitHub repositories to a JSON file.
*
* @param {Array} repos - The list of repositories to save.
* @param {string} today - The current date in the format 'YYYY-MM-DD'.
* @return {void} This function does not return anything explicitly.
*/
const saveReposToJson = (repos, today, dateDir) => {
const filePath = `${dateDir}/${today}.json`;
// JSON File with Selected Fields
const selectedFields = repos.map(repo => ({
id: repo.id,
name: repo.name,
full_name: repo.full_name,
owner: {
login: repo.owner.login,
avatar_url: repo.owner.avatar_url
},
description: repo.description,
topics: repo.topics,
html_url: repo.html_url,
stargazers_count: repo.stargazers_count,
language: repo.language,
forks_count: repo.forks_count,
open_issues_count: repo.open_issues_count,
created_at: repo.created_at,
updated_at: repo.updated_at,
pushed_at: repo.pushed_at,
}));
fs.writeFileSync(filePath, JSON.stringify(selectedFields, null, 2), 'utf8');
console.log(`Saved trending repos to ${filePath}`);
};
/**
* Saves the trending GitHub repositories to a CSV file.
*
* @param {Array} repos - The list of repositories to save.
* @param {string} today - The current date in the format 'YYYY-MM-DD'.
* @return {void} This function does not return anything explicitly.
*/
const saveReposToCsv = (repos, today, dateDir) => {
const filePath = `${dateDir}/${today}.csv`;
const fields = [
'full_name', 'stargazers_count', 'owner.login', 'owner.avatar_url',
'description', 'topics', 'html_url', 'created_at', 'updated_at', 'pushed_at',
'git_url', 'ssh_url', 'clone_url', 'svn_url', 'homepage', 'size', 'language',
'forks_count', 'open_issues_count', 'default_branch', 'license.name'
];
const parser = new Parser({ fields });
const csvFields = parser.parse(repos);
fs.writeFileSync(filePath, csvFields, 'utf8');
console.log(`Saved trending repos to ${filePath}`);
};
fetchTrendingRepos();