Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (227 loc) · 8.55 KB
/
Copy pathindex.js
File metadata and controls
260 lines (227 loc) · 8.55 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import axios from 'axios';
import fs from 'fs-extra';
import { Parser } from 'json2csv';
import dotenv from 'dotenv';
import { TOPICS, getTopicSlug } from './src/data/topics.js';
dotenv.config();
const GITHUB_API_URL = 'https://api.github.com';
const GITHUB_SEARCH_URI = '/search/repositories';
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const PERIODS = {
daily: { daysBack: 7, subdir: '' },
weekly: { daysBack: 30, subdir: 'weekly' },
monthly: { daysBack: 90, subdir: 'monthly' },
};
/**
* Fetches the trending repositories from the GitHub API for the given period and saves them to JSON/CSV.
*
* @param {string} period - One of 'daily', 'weekly', 'monthly'.
* @return {Promise<void>} This function does not return anything explicitly.
*/
const fetchTrendingRepos = async (period) => {
const config = PERIODS[period];
if (!config) {
throw new Error(`Unknown period "${period}". Expected one of: ${Object.keys(PERIODS).join(', ')}`);
}
try {
const response = await axios.get(`${GITHUB_API_URL}${GITHUB_SEARCH_URI}`, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
params: {
q: 'created:>=' + getCutoffDate(config.daysBack),
sort: 'stars',
order: 'desc',
per_page: 20,
},
});
const repos = response.data.items;
const { fileBase, dateDir } = getPeriodFileInfo(period, config.subdir);
fs.ensureDirSync(dateDir);
saveReposToJson(repos, fileBase, dateDir);
saveReposToCsv(repos, fileBase, dateDir);
} catch (error) {
console.error('Error fetching trending repositories:', error);
}
};
/**
* Gets the ISO date `daysBack` days before today.
*
* @param {number} daysBack - How many days back from today.
* @return {string} The cutoff date in ISO format (YYYY-MM-DD).
*/
const getCutoffDate = (daysBack) => {
const today = new Date();
const cutoff = new Date(today);
cutoff.setDate(today.getDate() - daysBack);
return cutoff.toISOString().split('T')[0];
};
/**
* Gets the Monday on or before the given date (ISO week start).
*
* @param {Date} date - The reference date.
* @return {Date} The Monday of that week.
*/
const getWeekStart = (date) => {
const d = new Date(date);
const day = d.getDay();
const diff = day === 0 ? 6 : day - 1;
d.setDate(d.getDate() - diff);
return d;
};
/**
* Computes the filename base and output directory for "one file per day" outputs
* (the daily period, and each topic's daily snapshot), as of now.
*
* @param {string} subdir - The output subdirectory (e.g. '' for daily, 'topics/react' for a topic).
* @return {{ fileBase: string, dateDir: string }}
*/
const getDailyFileInfo = (subdir) => {
const now = new Date();
const fileBase = now.toISOString().split('T')[0];
const year = fileBase.split('-')[0];
const month = fileBase.split('-')[1];
return { fileBase, dateDir: `${dir(subdir)}/${year}/${month}` };
};
/**
* Computes the filename base and output directory for a period, as of now.
*
* @param {string} period - One of 'daily', 'weekly', 'monthly'.
* @param {string} subdir - The period's output subdirectory (from PERIODS).
* @return {{ fileBase: string, dateDir: string }}
*/
const getPeriodFileInfo = (period, subdir) => {
const now = new Date();
if (period === 'monthly') {
const year = String(now.getFullYear());
const month = String(now.getMonth() + 1).padStart(2, '0');
return { fileBase: `${year}-${month}`, dateDir: `${dir(subdir)}/${year}` };
}
if (period === 'weekly') {
const weekStart = getWeekStart(now);
const fileBase = weekStart.toISOString().split('T')[0];
const year = fileBase.split('-')[0];
const month = fileBase.split('-')[1];
return { fileBase, dateDir: `${dir(subdir)}/${year}/${month}` };
}
return getDailyFileInfo(subdir);
};
/**
* Parses the --period=<daily|weekly|monthly> CLI argument, defaulting to 'daily'.
*
* @return {string} The requested period.
*/
const parsePeriod = () => {
const arg = process.argv.find((a) => a.startsWith('--period='));
return arg ? arg.split('=')[1] : 'daily';
};
const dir = (subdir) => {
return subdir ? `./docs/${subdir}` : './docs';
};
/**
* Saves the trending GitHub repositories to a JSON file.
*
* @param {Array} repos - The list of repositories to save.
* @param {string} fileBase - The filename base (no extension) to save under.
* @param {string} dateDir - The directory to save the file in.
* @return {void} This function does not return anything explicitly.
*/
const saveReposToJson = (repos, fileBase, dateDir) => {
const filePath = `${dateDir}/${fileBase}.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} fileBase - The filename base (no extension) to save under.
* @param {string} dateDir - The directory to save the file in.
* @return {void} This function does not return anything explicitly.
*/
const saveReposToCsv = (repos, fileBase, dateDir) => {
const filePath = `${dateDir}/${fileBase}.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}`);
};
/**
* Pauses execution for the given number of milliseconds.
*
* @param {number} ms - How long to sleep.
* @return {Promise<void>}
*/
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
/**
* Fetches trending repos for every topic in TOPICS and saves each to its own
* JSON/CSV under docs/topics/<slug>/<year>/<month>/<date>.*. Runs once per day,
* alongside (not instead of) the main daily fetch. Each topic is isolated in
* its own try/catch so one bad slug or transient API error doesn't abort the rest.
*
* @return {Promise<void>}
*/
const fetchTopicRepos = async () => {
for (const topic of TOPICS) {
const slug = getTopicSlug(topic);
try {
const response = await axios.get(`${GITHUB_API_URL}${GITHUB_SEARCH_URI}`, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
params: {
q: `topic:${slug} created:>=${getCutoffDate(7)}`,
sort: 'stars',
order: 'desc',
per_page: 20,
},
});
const repos = response.data.items;
if (!repos || repos.length === 0) {
console.error(`No repos found for topic "${slug}" — check the slug is still a valid GitHub topic.`);
continue;
}
const { fileBase, dateDir } = getDailyFileInfo(`topics/${slug}`);
fs.ensureDirSync(dateDir);
saveReposToJson(repos, fileBase, dateDir);
saveReposToCsv(repos, fileBase, dateDir);
} catch (error) {
console.error(`Error fetching trending repos for topic "${slug}":`, error.message);
}
// Safety margin here comes from TOPICS.length staying small, not from this delay enforcing
// the rate limit directly — revisit if TOPICS grows much past ~20 entries.
await sleep(400);
}
};
const period = parsePeriod();
fetchTrendingRepos(period).then(async () => {
if (period === 'daily') {
await fetchTopicRepos();
}
});