本稿ではTypeScriptでConnpassイベントの参加者を取得する方法を説明する。
get-connpass-applicantsモジュールをインストールする
yarn add @suin/get-connpass-applicants
このモジュールは、ConnpassイベントのURLを渡すと、そのイベントの申込者情報を返してくれるもの。
実装
上記のモジュールを使って、任意のConnpassイベントの参加者を取得してみる。
main.ts
import { getConnpassApplicants, Applicants } from '@suin/get-connpass-applicants'
(async () => {
const url = 'https://yyts.connpass.com/event/144568/'
const applicants = await getConnpassApplicants(url)
console.log(JSON.stringify(applicants, null, ' '))
})()
実行結果
{
"participants": [
{
"url": "https://connpass.com/user/OkinaKahiro/",
"name": "kahirokunn",
"participationType": "現地参加"
},
{
"url": "https://connpass.com/user/suin/",
"name": "suin",
"participationType": "主催者・スタッフ枠"
},
{
"url": "https://connpass.com/user/picric/",
"name": "kakiuchi",
"participationType": "現地参加"
},
{
"url": "https://connpass.com/user/zima_gen/",
"name": "zima",
"participationType": "現地参加"
}
],
"waitlist": [],
"cancelled": []
}
participantsByParticipationType
プロパティを参照すれば、申込み枠ごとにグルーピングした参加者リストも取得できる。戻り値はES6のMap
で返ってくる。
console.log(applicants.participantsByParticipationType)
実行結果
Map {
'現地参加' => [ Applicant {
url: 'https://connpass.com/user/OkinaKahiro/',
name: 'kahirokunn',
participationType: '現地参加' },
Applicant {
url: 'https://connpass.com/user/picric/',
name: 'kakiuchi',
participationType: '現地参加' },
Applicant {
url: 'https://connpass.com/user/zima_gen/',
name: 'zima',
participationType: '現地参加' } ],
'主催者・スタッフ枠' => [ Applicant {
url: 'https://connpass.com/user/suin/',
name: 'suin',
participationType: '主催者・スタッフ枠' } ] }