Created
August 16, 2023 12:34
-
-
Save kkeeth/f759108c8f245e6e8fc366812501ffe0 to your computer and use it in GitHub Desktop.
connpass の特定の organization の開催勉強会のデータ集計
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connpass API のリファレンス | |
// https://connpass.com/about/api/ | |
const axios = require("axios"); | |
const fs = require("fs"); | |
const ORGANIZATION_ID = "1657"; // ここに organization の ID を入力.1657 はゆめみ社のもの | |
const RESULTS_PER_PAGE = 100; // Connpass API の最大結果数 | |
async function fetchEventsByOrganization(organizationName, start = 1) { | |
const endpoint = `https://connpass.com/api/v1/event/?series_id=${organizationName}&count=${RESULTS_PER_PAGE}&start=${start}`; | |
const response = await axios.get(endpoint); | |
return response.data; | |
} | |
async function getAllEventsForOrganization(organizationNickname) { | |
let start = 1; | |
let allEvents = []; | |
let continueFetching = true; | |
while (continueFetching) { | |
const data = await fetchEventsByOrganization(organizationNickname, start); | |
allEvents = allEvents.concat(data.events); | |
if (data.events.length < RESULTS_PER_PAGE) { | |
continueFetching = false; | |
} else { | |
start += RESULTS_PER_PAGE; | |
} | |
} | |
return allEvents; | |
} | |
async function getStatsForOrganization(organizationName) { | |
const events = await getAllEventsForOrganization(organizationName); | |
const totalParticipants = events.reduce( | |
(sum, event) => sum + event.accepted, | |
0, | |
); | |
const totalWaitlisted = events.reduce((sum, event) => sum + event.waiting, 0); | |
const participantsList = events | |
.map((event) => ({ | |
title: event.title, | |
date: formatDate(event.started_at), | |
participants: event.accepted, | |
waitings: event.waiting, | |
url: event.event_url, | |
hashTag: event.hash_tag, | |
})) | |
.sort((a, b) => b.participants - a.participants); | |
const hashTagGroups = groupByHashTags(events); | |
return { | |
totalEvents: events.length, | |
totalParticipants, | |
totalWaitlisted, | |
participantsList, | |
hashTagGroups, | |
}; | |
} | |
getStatsForOrganization(ORGANIZATION_ID) | |
.then((stats) => { | |
fs.writeFileSync("events_stats.json", JSON.stringify(stats, null, 2)); | |
console.log("Events stats saved to events_stats.json"); | |
}) | |
.catch((error) => { | |
console.error("Error fetching stats:", error); | |
}); | |
function formatDate(dateString) { | |
const date = new Date(dateString); | |
return `${date.getFullYear()}年${ | |
date.getMonth() + 1 | |
}月${date.getDate()}日 ${date.getHours()}時${date.getMinutes()}分`; | |
} | |
function groupByHashTags(events) { | |
const hashTagGroups = {}; | |
events.forEach((event) => { | |
if (event.hash_tag) { | |
if (!hashTagGroups[event.hash_tag]) { | |
hashTagGroups[event.hash_tag] = { | |
events: [], | |
totalEvents: 0, | |
totalParticipants: 0, | |
}; | |
} | |
hashTagGroups[event.hash_tag].events.push({ | |
title: event.title, | |
participants: event.accepted, | |
waitlisted: event.waiting, | |
startDate: formatDate(event.started_at), | |
}); | |
hashTagGroups[event.hash_tag].totalEvents += 1; | |
hashTagGroups[event.hash_tag].totalParticipants += event.accepted; | |
} | |
}); | |
return hashTagGroups; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment