Last active
November 2, 2023 08:51
-
-
Save Luc1412/7c268fbb136fbc2716ec7c2a42addc9f to your computer and use it in GitHub Desktop.
Get Patreon data based on discord user id
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
# Campaign ID can be fetched trough the API (List all Campaigns: GET https://www.patreon.com/api/oauth2/v2/campaigns) | |
# access_token can be created on https://www.patreon.com/portal/registration/register-clients | |
# For more data, add fields to either fields[member] or fields[user]. All fields are listed on https://docs.patreon.com/#apiv2-resources | |
async def fetch_patreons(self, campaign_id, access_token): | |
patreons = {} | |
url = f'https://www.patreon.com/api/oauth2/v2/campaigns/{campaign_id}/members' | |
params = { | |
'include': 'user', | |
'fields[member]': | |
'lifetime_support_cents,patron_status,pledge_relationship_start,currently_entitled_amount_cents', | |
'fields[user]': 'social_connections' | |
} | |
headers = {'Authorization': f'Bearer {access_token}'} | |
while True: | |
async with self.bot.session.get(url, params=params, headers=headers) as response: | |
if response.status != 200: | |
return None | |
patreon_data = await response.json() | |
patreon_members = {} | |
for patreon_member in patreon_data['data']: | |
patreon_user_id = patreon_member['relationships']['user']['data']['id'] | |
patreon_members[patreon_user_id] = patreon_member['attributes'] | |
for patreon_user in patreon_data['included']: | |
if patreon_user.get('type') != 'user': | |
continue | |
patreon_user_data = patreon_user['attributes'] | |
if not patreon_user_data.get('social_connections'): | |
continue | |
discord_data = patreon_user['attributes']['social_connections']['discord'] | |
if not discord_data: | |
continue | |
discord_user_id = int(discord_data['user_id']) | |
member = patreon_members.get(patreon_user['id']) | |
if not member: | |
continue | |
patreons[discord_user_id] = member | |
pagination_data = patreon_data['meta']['pagination'] | |
if not pagination_data.get('cursors'): | |
break | |
next_cursor_id = pagination_data['cursors']['next'] | |
params['page[cursor]'] = next_cursor_id | |
return patreons | |
# Returns DiscordUserID: PatreonData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment