Skip to content

Commit 17c5c0f

Browse files
committed
feat: saving data in codebase
1 parent 093fd65 commit 17c5c0f

File tree

9 files changed

+120
-39
lines changed

9 files changed

+120
-39
lines changed

.github/workflows/update-readme.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v4
11-
- name: Update followers list
11+
- name: Update followers list & Blogs list
1212
run: |
13-
cd scripts && npm install && ACCESS_TOKEN=${{ secrets.ACCESS_TOKEN }} npm run get:followers
14-
- name: Update Blog Posts
15-
run: |
16-
cd scripts && npm install && ACCESS_TOKEN=${{ secrets.ACCESS_TOKEN }} npm run get:posts
13+
cd scripts && npm install && ACCESS_TOKEN=${{ secrets.ACCESS_TOKEN }} npm start
1714
- name: Commit changes
1815
run: |
1916
git config --local user.email "${{ secrets.GIT_EMAIL }}"

scripts/package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8+
"start": "node src/index.js",
89
"get:posts": "node src/fetchLatestBlogPosts.js",
9-
"get:followers": "node src/fetchFollowers.js"
10+
"get:followers": "node src/fetchFollowers.js",
11+
"format": "prettier --write \"**/*.{js,json}\""
1012
},
1113
"keywords": [],
1214
"author": "",
@@ -15,6 +17,7 @@
1517
"@octokit/rest": "^21.0.2",
1618
"axios": "^1.7.9",
1719
"dotenv": "^16.4.7",
18-
"zod": "^3.23.8"
20+
"zod": "^3.23.8",
21+
"prettier": "^3.4.2"
1922
}
20-
}
23+
}

scripts/src/config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@ export const BlogsResponseSchema = z.object({
2222

2323
export const READMEFILE_PATH = path.resolve(
2424
path.dirname(new URL(import.meta.url).pathname),
25-
"../../README.md"
26-
);
25+
"../../README.md",
26+
);
27+
28+
export const DATA_DIR = path.resolve(
29+
path.dirname(new URL(import.meta.url).pathname),
30+
"../../data",
31+
);
32+
33+
export const TODAY = new Date(
34+
new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }),
35+
);

scripts/src/fetchFollowers.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ if (process.env.ACCESS_TOKEN === undefined) {
88

99
import { Octokit } from "@octokit/rest";
1010
import { readFileSync, writeFileSync } from "fs";
11-
import { updateWorkflowNumber } from "./updateWorkflow.js";
1211
import { READMEFILE_PATH } from "./config.js";
1312

1413
const octokit = new Octokit({
@@ -22,7 +21,7 @@ const getLatestFollowers = async () => {
2221
{
2322
username: username,
2423
per_page: 1000000000000,
25-
}
24+
},
2625
);
2726

2827
const followers = data.map((follower) => ({
@@ -40,14 +39,14 @@ const getLatestFollowers = async () => {
4039
}
4140
};
4241

43-
async function main() {
42+
export async function handleFetchFollowers() {
4443
const followers = await getLatestFollowers();
4544
const followersLength = followers.length;
4645
let readme = readFileSync(READMEFILE_PATH, "utf-8");
4746

4847
readme = readme.replace(
4948
/(?<=<!--START_SECTION:top-followers-heading-->\n)[\s\S]*(?=\n<!--End_SECTION:top-followers-heading-->)/,
50-
`\n### :sparkles: [My followers (${followersLength})](https://github.com/Pulkitxm?tab=followers)\n`
49+
`\n### :sparkles: [My followers (${followersLength})](https://github.com/Pulkitxm?tab=followers)\n`,
5150
);
5251

5352
readme = readme.replace(
@@ -56,15 +55,12 @@ async function main() {
5655
followers
5756
.map(
5857
(follower) =>
59-
`<a href="${follower.profileUrl}" target="_blank"><img src="${follower.picUrl}" alt="Follower" width="50" height="50" style="border-radius: 50%; margin: 3px;"/></a>`
58+
`<a href="${follower.profileUrl}" target="_blank"><img src="${follower.picUrl}" alt="Follower" width="50" height="50" style="border-radius: 50%; margin: 3px;"/></a>`,
6059
)
6160
.join("\n") +
62-
`</div>`
61+
`</div>`,
6362
);
6463

6564
writeFileSync(READMEFILE_PATH, readme);
65+
return followers;
6666
}
67-
68-
main().then(async () => {
69-
await updateWorkflowNumber();
70-
});

scripts/src/fetchLatestBlogPosts.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dotenv.config();
33

44
import axios from "axios";
55
import { readFileSync, writeFileSync } from "fs";
6-
import { updateWorkflowNumber } from "./updateWorkflow.js";
76
import { BlogsResponseSchema, READMEFILE_PATH } from "./config.js";
87

98
const fetchLatestBlogs = async () => {
@@ -51,7 +50,7 @@ query Publication($id: ObjectId = "66213f8be5371b46eac0e05e") {
5150
}
5251
};
5352

54-
async function main() {
53+
export async function handleFetchBlogs() {
5554
const blogs = await fetchLatestBlogs();
5655

5756
let readme = readFileSync(READMEFILE_PATH, "utf-8");
@@ -62,13 +61,10 @@ async function main() {
6261
blogs
6362
.map(
6463
(blog) =>
65-
`| [${blog.title}](${blog.url}) | ${blog.dateAdded} | <img src="${blog.coverImage.url}?w=300&h=157&fit=crop&crop=entropy&auto=compress,format&format=webp" />`
64+
`| [${blog.title}](${blog.url}) | ${blog.dateAdded} | <img src="${blog.coverImage.url}?w=300&h=157&fit=crop&crop=entropy&auto=compress,format&format=webp" />`,
6665
)
67-
.join("\n")
66+
.join("\n"),
6867
);
6968
writeFileSync(READMEFILE_PATH, readme);
69+
return blogs;
7070
}
71-
72-
main().then(async () => {
73-
await updateWorkflowNumber();
74-
});

scripts/src/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { exec } from "child_process";
2+
import { DATA_DIR, TODAY } from "./config.js";
3+
import { handleFetchFollowers } from "./fetchFollowers.js";
4+
import { handleFetchBlogs } from "./fetchLatestBlogPosts.js";
5+
import { writeFileSync, mkdirSync, renameSync, rmSync, existsSync } from "fs";
6+
import { updateWorkflowNumber } from "./updateWorkflow.js";
7+
8+
async function main() {
9+
console.log("Starting data fetch process...");
10+
11+
try {
12+
console.log("Fetching followers and blogs...");
13+
const [followers, blogs] = await Promise.all([
14+
handleFetchFollowers(),
15+
handleFetchBlogs(),
16+
]);
17+
await updateWorkflowNumber();
18+
console.log(
19+
`Successfully fetched ${followers.length} followers and ${blogs.length} blogs, respectively.`,
20+
);
21+
22+
const TODAY_DIR_NAME = `${TODAY.getDate()}-${TODAY.getMonth() + 1}-${TODAY.getFullYear()}`;
23+
const targetDir = `${DATA_DIR}/${TODAY_DIR_NAME}`;
24+
25+
console.log(`Creating directory: ${DATA_DIR}`);
26+
mkdirSync(`${DATA_DIR}`, { recursive: true });
27+
28+
console.log(`Creating temporary directory: ${TODAY_DIR_NAME}`);
29+
mkdirSync(`${TODAY_DIR_NAME}`, { recursive: true });
30+
31+
console.log("Writing followers data...");
32+
writeFileSync(
33+
`${TODAY_DIR_NAME}/followers.json`,
34+
JSON.stringify(followers, null, 2),
35+
);
36+
37+
console.log("Writing blogs data...");
38+
writeFileSync(
39+
`${TODAY_DIR_NAME}/blogs.json`,
40+
JSON.stringify(blogs, null, 2),
41+
);
42+
43+
console.log("Running formatter...");
44+
exec("npm run format", (err, stdout, stderr) => {
45+
if (err) {
46+
console.error("Formatting failed:", err);
47+
return;
48+
}
49+
console.log("Formatting completed:", stdout);
50+
});
51+
52+
if (existsSync(targetDir)) {
53+
rmSync(targetDir, { recursive: true });
54+
}
55+
renameSync(TODAY_DIR_NAME, targetDir);
56+
console.log(`Renamed ${TODAY_DIR_NAME} to ${targetDir}`);
57+
58+
console.log("Process completed successfully!");
59+
} catch (error) {
60+
console.error("An error occurred:", error);
61+
process.exit(1);
62+
}
63+
}
64+
65+
main();

scripts/src/langGraph.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function getAllRepositories(octokit) {
1919
affiliation: "owner",
2020
sort: "updated",
2121
direction: "desc",
22-
}
22+
},
2323
);
2424

2525
const nonForkedRepos = repos.filter((repo) => !repo.fork);
@@ -47,7 +47,7 @@ async function getLanguageStats(octokit, repos) {
4747
console.log(`Processed languages for ${repo.name}`);
4848
} catch (error) {
4949
console.log(
50-
`Error fetching languages for ${repo.name}: ${error.message}`
50+
`Error fetching languages for ${repo.name}: ${error.message}`,
5151
);
5252
}
5353
}
@@ -59,7 +59,7 @@ async function main() {
5959
try {
6060
if (!process.env.ACCESS_TOKEN) {
6161
throw new Error(
62-
"GitHub token not found. Please set ACCESS_TOKEN in .env file."
62+
"GitHub token not found. Please set ACCESS_TOKEN in .env file.",
6363
);
6464
}
6565

scripts/src/updateWorkflow.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@ const getLatestWorkflow = async () => {
3030
console.error(error.message);
3131
return {
3232
count: 0,
33-
timeStamp: new Date().toISOString(),
33+
timeStamp: TODAY.toISOString(),
3434
};
3535
}
3636
};
3737

3838
export async function updateWorkflowNumber() {
3939
const workflowDetails = await getLatestWorkflow();
4040
const count = workflowDetails.count;
41-
const timeStamp = new Date(workflowDetails.timeStamp);
4241
const time =
43-
new Date(workflowDetails.timeStamp).toLocaleString("en-US", {
44-
timeZone: "Asia/Kolkata",
45-
dateStyle: "medium",
46-
timeStyle: "short",
47-
}) + " IST";
42+
new Date(workflowDetails.timeStamp).toLocaleString("en-US", {
43+
timeZone: "Asia/Kolkata",
44+
dateStyle: "medium",
45+
timeStyle: "short",
46+
}) + " IST";
4847

4948
const readmeContent = readFileSync(READMEFILE_PATH, "utf-8");
5049

@@ -55,7 +54,7 @@ export async function updateWorkflowNumber() {
5554
Last refresh: <b>${time}</b><br/>
5655
Number of workflows: <b>${count}</b><br/><br/>
5756
Made with ❤️ by Pulkit
58-
</p>\n`
57+
</p>\n`,
5958
);
6059

6160
writeFileSync(READMEFILE_PATH, updatedContent, "utf-8");

0 commit comments

Comments
 (0)