patreon-python icon indicating copy to clipboard operation
patreon-python copied to clipboard

Paging through list of pledges: extract_cursor -> "Provided cursor path did not result in a link"

Open sebastienvercammen opened this issue 6 years ago • 0 comments

Using: https://docs.patreon.com/?python#paging-through-a-list-of-pledges

import patreon

access_token = '...' # your Creator Access Token
api_client = patreon.API(access_token)

# Get the campaign ID
campaign_response = api_client.fetch_campaign()
campaign_id = campaign_response.data()[0].id()

# Fetch all pledges
all_pledges = []
cursor = None
while True:
    pledges_response = api_client.fetch_page_of_pledges(campaign_id, 25, cursor=cursor)
    pledges += pledges_response.data()
    cursor = api_client.extract_cursor(pledges_response)
    if not cursor:
        break

Error:

Traceback (most recent call last):
  File ".\start.py", line 45, in <module>
    main()
  File ".\start.py", line 28, in main
    get_all_data(ACCESS_TOKEN)
  File "patreonapi.py", line 117, in get_all_data
    get_pledge_data(api_client, campaign.patreon_id)
  File "patreonapi.py", line 90, in get_pledge_data
    cursor = api_client.extract_cursor(pledges_response)
  File "C:\Python27\lib\site-packages\patreon\api.py", line 74, in extract_cursor
    'Provided cursor path did not result in a link', current_dict
Exception: ('Provided cursor path did not result in a link', u'https://www.patreon.com/api/oauth2/api/campaigns/<removed>/pledges?page%5Bcount%5D=10&sort=created&page%5Bcursor%5D=2017-12-03T18%3A06%3A42.344287%2B00%3A00')

Related to #19, which was apparently solved in #20, reverted in #21 and a next mention of six in #22 .

Environment:

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32

PS C:\Users\Seb> [Environment]::OSVersion
Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.17134.0 Microsoft Windows NT 10.0.17134.0

Workaround as described in #19 still works:

Replace:

cursor = api_client.extract_cursor(pledges_response)

With:

from urlparse import urlparse, parse_qs

# Temporary workaround until Patreon fixes the issue.
next_link = pledges_response.json_data.get('links', {}).get('next')

if not next_link:
    break

query = urlparse(next_link).query
parsed_qs = parse_qs(query)
cursor = parsed_qs['page[cursor]'][0]

sebastienvercammen avatar Oct 31 '18 13:10 sebastienvercammen