-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (163 loc) · 6.05 KB
/
Copy pathindex.js
File metadata and controls
186 lines (163 loc) · 6.05 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
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;
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 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}` };
}
const fileBase = now.toISOString().split('T')[0];
const year = fileBase.split('-')[0];
const month = fileBase.split('-')[1];
return { fileBase, dateDir: `${dir(subdir)}/${year}/${month}` };
};
/**
* 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}`);
};
fetchTrendingRepos(parsePeriod());