-
Notifications
You must be signed in to change notification settings - Fork 0
/
likesRanking.ts
133 lines (114 loc) · 3.58 KB
/
likesRanking.ts
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
import axios from "axios";
import * as fs from "node:fs/promises";
import dotenv from "dotenv";
import { makeRank } from "./helper/makeRank";
import { formatDate } from "./helper/formatDate";
import { makeCreatedAtRangeList } from "./helper/makeCreatedAtRangeList";
dotenv.config();
const accessToken = process.env.ACCESS_TOKEN_3;
makeLikesRankingArticle();
async function makeLikesRankingArticle() {
const likesRanking = await makeLikesRanking();
await makeAndPatchArticle(likesRanking);
}
async function makeLikesRanking() {
const createdAtRangeList = await makeCreatedAtRangeList();
const likesRanking = [];
for (const createdAtRange of createdAtRangeList) {
console.log(`-----${createdAtRange}-----`);
let pageNumber = 1;
let allResponseData = [];
while (true) {
const responseData = (
await axios.get("https://qiita.com/api/v2/items", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
params: {
query: `${createdAtRange} stocks:>500`, // 500以下の記事にいいねが多い記事がないことは担保しないといけない
page: pageNumber,
per_page: 100,
},
})
).data.map((article: any) => {
return {
title: article.title,
likesCount: article.likes_count,
createdAt: article.created_at,
updatedAt: article.updated_at,
url: article.url,
userId: `@${article.user.id}`,
};
});
if (responseData.length === 0) {
break;
}
console.log(`${pageNumber}ページ`);
allResponseData.push(responseData);
pageNumber++;
}
likesRanking.push(...allResponseData.flat());
console.log(`-----${createdAtRange}-----`);
}
return likesRanking
.sort((a, b) => {
if (a.likesCount > b.likesCount) {
return -1;
}
if (a.likesCount < b.likesCount) {
return 1;
}
return 0;
})
.slice(0, 100);
}
async function makeAndPatchArticle(likesRanking: any) {
const articleInformation = {
title: "【保存版】Qiita歴代いいね数ランキング100【自動更新】",
body: await makeArticleBody(likesRanking),
tags: [
{ name: "TypeScript" },
{ name: "QiitaAPI" },
{ name: "Qiita" },
{ name: "JavaScript" },
{ name: "初心者" },
],
};
try {
await axios.patch(
"https://qiita.com/api/v2/items/48a37fcdc458603797de",
articleInformation,
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}
);
console.log("更新が完了しました!");
} catch (e) {
console.log(e);
console.log("更新に失敗しました..");
}
}
async function makeArticleBody(likesRanking: any) {
const lead = await fs.readFile("likesRankingLead.md", "utf-8");
const articleBody = likesRanking.reduce(
async (prevArticleBody: string, rankingData: any, index: number) => {
const content = await fs.readFile("likesRanking.md", "utf-8");
return (
(await prevArticleBody) +
content
.replace("rankValue", makeRank(index + 1))
.replace("titleValue", rankingData.title)
.replace("likeValue", rankingData.likesCount)
.replace("urlValue", rankingData.url)
.replace("userIdValue", rankingData.userId)
.replace("createdAtValue", formatDate(rankingData.createdAt))
.replace("updatedAtValue", formatDate(rankingData.updatedAt))
);
},
lead
);
return articleBody;
}