const API_VER = 2.3;
const API_BASE = "https://api.stackexchange.com";
const API_KEY = "zhhhBNmsqZLZ967tc2dn8w((";
const delay = (s) => new Promise((r) => setTimeout(r, s*1e3));
const searchEarlyQuestions = async ({
key,
site = "stackoverflow",
...rest
}) => {
const url = new URL(`${API_BASE}/${API_VER}/search`);
const params = new URLSearchParams({
key,
site,
order: "asc",
sort: "creation",
...rest,
});
url.search = params.toString();
const res = await fetch(url.toString());
if (!res.ok) return [];
const { items = [], backoff } = await res.json();
if (backoff) {
await delay(backoff);
return searchEarlyQuestions({
key,
site,
...rest,
});
}
return items;
};
const getUserQuestions = async (userId, {
key,
site = "stackoverflow",
page = 1,
...rest
}) => {
const url = new URL(`${API_BASE}/${API_VER}/users/${userId}/questions`);
const params = new URLSearchParams({
key,
site,
page,
...rest,
});
url.search = params.toString();
const res = await fetch(url.toString());
if (!res.ok) return [];
const { items = [], backoff, has_more } = await res.json();
if (backoff) {
await delay(backoff);
return getUserQuestions(userId, {
key,
site,
page,
...rest,
});
}
if (has_more) {
items.push(
...(await getUserQuestions(userId, {
key,
site,
page: page + 1,
...rest,
}))
);
}
return items;
};
const getTags = async (names, {
key,
site = "stackoverflow",
page = 1,
...rest
}) => {
const url = new URL(`${API_BASE}/${API_VER}/tags/${names.join(";")}/info`);
const params = new URLSearchParams({
key,
site,
page,
...rest,
});
url.search = params.toString();
const res = await fetch(url.toString());
if (!res.ok) return [];
const { items = [], has_more = false, backoff } = await res.json();
if (backoff) {
await delay(backoff);
return getTags(names, {
key,
site,
page,
...rest,
});
}
if (has_more) {
items.push(
...(await getTags(names, {
key,
site,
page: page + 1,
...rest,
}))
);
}
return items;
};
const makeCell = (content) => {
const td = document.createElement("td");
td.append(content);
return td;
};
const makeRow = (columns) => {
const tr = document.createElement("tr");
const cells = columns.map(makeCell);
tr.append(...cells);
return tr;
};
const makeLink = (url, label) => {
const a = document.createElement("a");
a.target = "_blank";
a.href = url;
a.innerText = label;
return a;
};
const uniqify = (arr) => [...new Set(arr)];
window.addEventListener("load", () => {
const useridInput = document.getElementById("user_id");
const hostnameInput = document.getElementById("host");
const progress = document.getElementById("load");
const submit = document.getElementById("search");
submit.addEventListener("click", async() => {
progress.value = 0;
const { value: userId } = useridInput;
const { value: hostname } = hostnameInput;
const apiSlug = hostname.slice(0, hostname.lastIndexOf("."));
console.log({ apiSlug });
const report = document.querySelector("#preview > tbody");
const commonOpts = {
key: API_KEY,
pagesize: 100,
site: apiSlug
};
const questions = await getUserQuestions(userId, {
...commonOpts,
filter: "!.FdHWl*M9(MQSmLP_ZZzycLIeIL6j",
});
progress.value = 10;
console.log({ questions });
const notEditedQuestions = questions.filter(
({ last_edit_date }) => (!last_edit_date)
);
console.log({ notEditedQuestions });
progress.value = 20;
const notEditedTags = uniqify(
notEditedQuestions
.flatMap(({ tags = [] }) => tags)
.filter(Boolean) // just in case
);
console.log({ notEditedTags });
// since SE API treats user ids as numbers...
const numUserId = +userId;
const tagCreationStatuses = [];
const step = 70 / notEditedTags.length;
let simultaneousReqs = 0;
let throttles = 0;
for(const tagged of notEditedTags) {
simultaneousReqs += 1;
const [earliest] = await searchEarlyQuestions({
...commonOpts, tagged,
filter: "!.FdHWl*M9(MQSmLP_ZZzycLIeIL6j",
});
tagCreationStatuses.push(
earliest.owner.user_id === numUserId
)
if(simultaneousReqs > 10) {
throttles += 1;
await delay(1.1);
console.log(`${throttles} throttled for 1.1s`);
simultaneousReqs = 0;
}
progress.value += step;
}
console.log({ tagCreationStatuses });
const userCreatedTags = notEditedTags.filter((_,i) => tagCreationStatuses[i]);
console.log({ userCreatedTags });
const tags = userCreatedTags.length ? await getTags(userCreatedTags, commonOpts) : [];
console.log({ tags });
progress.value = 100;
const rows = tags
.sort((a, b) => b.count - a.count)
.map(({
name, count, is_required, is_moderator_only
}) => makeRow([
makeLink(`https://${hostname}/tags/${name}`,name),
count,
is_required ? "Yes": "No",
is_moderator_only ? "Yes": "No"
]));
report.querySelectorAll("tr").forEach((r) => r.remove());
report.append(...rows);
report.closest("table").classList.remove("hidden");
});
});
label {
display: block;
font-weight: 600;
}
caption,
label {
font-weight: 600;
margin: 1vh 0;
}
table {
margin-top: 4vh;
border-collapse: collapse;
}
th,
td {
padding: 2vh 2vw;
border: 1px solid lightgray;
}
.hidden {
display: none;
}
<form>
<label for="host">Hostname</label>
<input id="host" type="text" title="Hostname" placeholder="host.com" value="stackoverflow.com" />
<label for="user_id">User Id</label>
<input id="user_id" type="text" title="User Id" placeholder="12345" value="3904066" />
<button id="search" type="button">Get Info</button>
</form>
<progress id="load" max="100" value="0"></progress>
<table id="preview" class="hidden" cellspacing="0">
<caption>Tags created by the user</caption>
<thead>
<tr>
<th>Tag</th>
<th>Questions</th>
<th>Required?</th>
<th>Mod-only?</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Tags
table has theExerptPostID
andWikiPostID
values, however, the owner of those posts might not be the creator of the tag. TheBadges
table is present, but it doesn't tell you what tag that is related to specifically.