Last active
February 9, 2025 03:20
-
-
Save harlanji/6210b94b32e1ae4cfa5e1db5f73e83be to your computer and use it in GitHub Desktop.
Fetching content from Patreon API in Python
This file contains hidden or 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
import os | |
import patreon | |
import requests | |
from dotenv import load_dotenv | |
load_dotenv() | |
access_token = os.environ.get('PATREON_ACCESS_TOKEN') | |
patreon_api = patreon.API(access_token) | |
patreon_user = patreon_api.fetch_user() | |
headers = {'Authorization': f'Bearer {access_token}'} | |
# My Campaign | |
campaign = patreon_user.data().relationship('campaign') | |
campaign_id = campaign.json_data.get('id') | |
# Posts | |
# filtering does not seem to work. | |
# https://jsonapi.org/format/#fetching-filtering | |
url = f'https://www.patreon.com/api/oauth2/v2/campaigns/{campaign_id}/posts?filter[post][is_public]=False' | |
posts_resp = requests.get(url, headers=headers) | |
posts_page = posts_resp.json() | |
next_token = posts_page['meta']['pagination']['cursors']['next'] | |
total_count = posts_page['meta']['pagination']['total'] | |
posts = posts_page['data'] | |
# Post | |
post_id = posts[0]['id'] | |
url = f'https://www.patreon.com/api/oauth2/v2/posts/{post_id}?fields[post]=published_at,title,content,embed_data,is_public,tiers,url' | |
post_resp = requests.get(url, headers=headers) | |
post_resource = post_resp.json() | |
post = post_resource['data'] | |
# Following | |
#campaigns = patreon_api.fetch_campaign() | |
#campgin_id = campaigns.data()[0].json_data.get('id') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment