Getting Pledge History (Python or generic request)

It’s been nearly a year, but here’s how I extract a pledge history. I think it was a formatting issue with my specific request.

Code
 def get_pledge_history(self):
        access_token = self.creator_access_token
        api_client = patreon.API(access_token)
        campaign_id = api_client.get_campaigns(page_size=1).json_data['data'][0]['id']
        total_members = 1
        cursor = None
        members = []
        pledge_data = []
        while len(members) < total_members:
            suffix = "campaigns/{}/members?include=pledge_history&fields[pledge-event]=" \
                     "date%2Ctype%2Cpayment_status%2Ctier_title&fields[member]=email".format(campaign_id)
            if cursor:
                suffix += ("&page[cursor]={}".format(cursor))
            response = requests.get(
                "https://www.patreon.com/api/oauth2/v2/{}".format(suffix),
                headers={
                    'Authorization': "Bearer {}".format(access_token),
                    'User-Agent': "Patreon CGT",
                }
            )

            campaign = response.json()
            members += campaign['data']
            try:
                pledge_data += campaign['included']
            except KeyError:
                print("No members found")
            pagination = campaign['meta']['pagination']
            try:
                total_members = pagination['total']
                print("Downloading members {}/{}".format(len(members), total_members), end="\r", flush=True)
                cursor = pagination['cursors']['next']
            except KeyError as e:
                break

Edit: Formatting

1 Like