forked from lowlighter/metrics
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
148 lines (141 loc) · 6.17 KB
/
index.mjs
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
//Setup
export default async function({login, q, imports, data, account}, {enabled = false, extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.stackoverflow) || (!imports.metadata.plugins.stackoverflow.enabled(enabled, {extras})))
return null
//Load inputs
let {sections, user, limit, lines, "lines.snippet": codelines} = imports.metadata.plugins.stackoverflow.inputs({data, account, q})
if (!user)
throw {error: {message: "Stack Overflow user id is not set"}}
//Initialization
//See https://api.stackexchange.com/docs
const api = {base: "https://api.stackexchange.com/2.2", user: `https://api.stackexchange.com/2.2/users/${user}`}
const filters = {user: "!0Z-LvgkLYnTCu1858)*D0lcx2", answer: "!7goY5TLWwCz.BaGpe)tv5C6Bks2q8siMH6", question: "!)EhwvzgX*hrClxjLzqxiZHHbTPRE5Pb3B9vvRaqCx5-ZY.vPr"}
const result = {sections, lines}
//Stackoverflow user metrics
{
//Account metrics
console.debug(`metrics/compute/${login}/plugins > stackoverflow > querying api for user ${user}`)
const {data: {items: [{reputation, badge_counts: {bronze, silver, gold}, answer_count: answers, question_count: questions, view_count: views}]}} = await imports.axios.get(`${api.user}?site=stackoverflow&filter=${filters.user}`)
const {data: {total: comments}} = await imports.axios.get(`${api.user}/comments?site=stackoverflow&filter=total`)
//Save result
result.user = {id: user, reputation, badges: bronze + silver + gold, questions, answers, comments, views}
}
//Answers
for (const {key, sort} of [{key: "answers-recent", sort: "sort=activity&order=desc"}, {key: "answers-top", sort: "sort=votes&order=desc"}].filter(({key}) => sections.includes(key))) {
//Load and format answers
console.debug(`metrics/compute/${login}/plugins > stackoverflow > querying api for ${key}`)
const {data: {items}} = await imports.axios.get(`${api.user}/answers?site=stackoverflow&pagesize=${limit}&filter=${filters.answer}&${sort}`)
result[key] = await Promise.all(items.map(item => format.answer(item, {imports, codelines})))
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loaded ${result[key].length} items`)
//Load related questions
const ids = result[key].map(({question_id}) => question_id).filter(id => id)
if (ids) {
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loading ${ids.length} related items`)
const {data: {items}} = await imports.axios.get(`${api.base}/questions/${ids.join(";")}?site=stackoverflow&filter=${filters.question}`)
await Promise.all(items.map(item => format.question(item, {imports, codelines})))
}
}
//Questions
for (const {key, sort} of [{key: "questions-recent", sort: "sort=activity&order=desc"}, {key: "questions-top", sort: "sort=votes&order=desc"}].filter(({key}) => sections.includes(key))) {
//Load and format questions
console.debug(`metrics/compute/${login}/plugins > stackoverflow > querying api for ${key}`)
const {data: {items}} = await imports.axios.get(`${api.user}/questions?site=stackoverflow&pagesize=${limit}&filter=${filters.question}&${sort}`)
result[key] = await Promise.all(items.map(item => format.question(item, {imports, codelines})))
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loaded ${result[key].length} items`)
//Load related answers
const ids = result[key].map(({accepted_answer_id}) => accepted_answer_id).filter(id => id)
if (ids) {
console.debug(`metrics/compute/${login}/plugins > stackoverflow > loading ${ids.length} related items`)
const {data: {items}} = await imports.axios.get(`${api.base}/answers/${ids.join(";")}?site=stackoverflow&filter=${filters.answer}`)
await Promise.all(items.map(item => format.answer(item, {imports, codelines})))
}
}
//Results
return result
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}
//Formatters
const format = {
/**Cached */
cached: new Map(),
/**Format stackoverflow code snippets */
code(text) {
return text.replace(/<!-- language: lang-(?<lang>\w+) -->\s*(?<snippet> {4}[\s\S]+?)(?=(?:<!-- end snippet -->)|(?:<!-- language: lang-))/g, "```$<lang>\n$<snippet>```")
},
/**Format answers */
async answer({body_markdown: body, score, up_vote_count: upvotes, down_vote_count: downvotes, is_accepted: accepted, comment_count: comments = 0, creation_date, owner: {display_name: author}, link, answer_id: id, question_id}, {imports, codelines}) {
const formatted = {
type: "answer",
body: await imports.markdown(format.code(imports.htmlunescape(body)), {codelines}),
score,
upvotes,
downvotes,
accepted,
comments,
author,
created: imports.format.date(creation_date * 1000, {date: true}),
link,
id,
question_id,
get question() {
return format.cached.get(`q${this.question_id}`) ?? null
},
}
this.cached.set(`a${id}`, formatted)
return formatted
},
/**Format questions */
async question(
{
title,
body_markdown: body,
score,
up_vote_count: upvotes,
down_vote_count: downvotes,
favorite_count: favorites,
tags,
is_answered: answered,
answer_count: answers,
comment_count: comments = 0,
view_count: views,
creation_date,
owner: {display_name: author},
link,
question_id: id,
accepted_answer_id = null,
},
{imports, codelines},
) {
const formatted = {
type: "question",
title: await imports.markdown(title),
body: await imports.markdown(format.code(imports.htmlunescape(body)), {codelines}),
score,
upvotes,
downvotes,
favorites,
tags,
answered,
answers,
comments,
views,
author,
created: imports.format.date(creation_date * 1000, {date: true}),
link,
id,
accepted_answer_id,
get answer() {
return format.cached.get(`a${this.accepted_answer_id}`) ?? null
},
}
this.cached.set(`q${id}`, formatted)
return formatted
},
}