Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import { writeJSON } from "fs-extra"; import currentPatrons from "../../patrons.json"; Iif (!process.env.PATREON_ACCESS_TOKEN) { console.log("Env variable PATREON_ACCESS_TOKEN is not set"); process.exit(); } Iif (!process.env.PATREON_CAMPAIGN_ID) { console.log("Env variable PATREON_CAMPAIGN_ID is not set"); process.exit(); } const ACCESS_TOKEN = process.env.PATREON_ACCESS_TOKEN; const CAMPAIGN_ID = process.env.PATREON_CAMPAIGN_ID; const SILVER_TIER_ID = "22845707"; const GOLD_TIER_ID = "22845730"; const BASE_URL = "https://www.patreon.com/api/oauth2/v2"; interface PatreonMember { id: string; relationships: { currently_entitled_tiers: { data: { id: string }[]; }; user: { data: { id: string }; }; }; } export interface PatreonUser { type: "user"; id: string; attributes: { full_name: string; }; } interface PatreonTier { type: "tier"; id: string; attributes: { title: string; }; } interface LatestPatrons { goldTier: PatreonUser[]; silverTier: PatreonUser[]; } export interface Patrons { goldTier: PatreonUser[]; silverTier: PatreonUser[]; pastPatrons: PatreonUser[]; } interface PatreonMembersAPIResponse { data: PatreonMember[]; included: Array<PatreonUser | PatreonTier>; links?: { next?: string; }; } const caseInsensitiveSort = (a: PatreonUser, b: PatreonUser) => a.attributes.full_name .toLowerCase() .localeCompare(b.attributes.full_name.toLowerCase()); const fetchPatreonAPI = async (url: string) => { const headers = { Authorization: `Bearer ${ACCESS_TOKEN}`, "Content-Type": "application/json", }; const response = await fetch(url, { headers }); Iif (!response.ok) { throw new Error(`Error from Patreon API: ${response.statusText}`); } return response.json(); }; const onlyUnique = ( value: PatreonUser, index: number, array: PatreonUser[] ): boolean => array.findIndex((p) => p.id === value.id) === index; const fetchPatrons = async (): Promise<LatestPatrons> => { const usersLookup: Record<string, PatreonUser> = {}; const tiersLookup: Record<string, PatreonTier> = {}; const goldTierUserIds = new Set<string>(); const silverTierUserIds = new Set<string>(); const initialEndpoint = `${BASE_URL}/campaigns/${CAMPAIGN_ID}/members?include=user,currently_entitled_tiers&fields%5Buser%5D=full_name&fields%5Btier%5D=title`; const fetchPage = async (url: string) => { console.log("Fetching: " + url); const res = (await fetchPatreonAPI(url)) as PatreonMembersAPIResponse; res.data.forEach((member) => { if ( member.relationships.currently_entitled_tiers.data.some( (tier) => tier.id === GOLD_TIER_ID ) ) { console.log("GOLD TIER: " + member.relationships.user.data.id); goldTierUserIds.add(member.relationships.user.data.id); } else Iif ( member.relationships.currently_entitled_tiers.data.some( (tier) => tier.id === SILVER_TIER_ID ) ) { console.log("SILVER TIER: " + member.relationships.user.data.id); silverTierUserIds.add(member.relationships.user.data.id); } }); res.included.forEach((entry) => { if (entry.type === "user") { usersLookup[entry.id] = entry; } else Iif (entry.type === "tier") { tiersLookup[entry.id] = entry; } }); Iif (res.links?.next) { await fetchPage(res.links.next); } }; const toUser = (id: string): PatreonUser => { return usersLookup[id]; }; await fetchPage(initialEndpoint); return { goldTier: Array.from(goldTierUserIds) .map(toUser) .filter(onlyUnique) .sort(caseInsensitiveSort), silverTier: Array.from(silverTierUserIds) .map(toUser) .filter(onlyUnique) .sort(caseInsensitiveSort), }; }; const mergePatrons = ( newPatrons: LatestPatrons, prevPatrons: Patrons ): Patrons => { const currentAllTierPatronIds = [ ...newPatrons.goldTier, ...newPatrons.silverTier, ].map((p) => p.id); return { goldTier: newPatrons.goldTier, silverTier: newPatrons.silverTier, pastPatrons: [ ...prevPatrons.goldTier, ...prevPatrons.silverTier, ...prevPatrons.pastPatrons, ] .filter((value) => !currentAllTierPatronIds.includes(value.id)) .filter(onlyUnique) .sort(caseInsensitiveSort), }; }; const main = async () => { const newPatrons = await fetchPatrons(); const mergedPatrons = mergePatrons(newPatrons, currentPatrons as Patrons); writeJSON("./patrons.json", mergedPatrons, { spaces: 2, }); }; main(); export {}; |