christopherkenny / bskyr

Interact with Bluesky Social
http://christophertkenny.com/bskyr/
Other
9 stars 1 forks source link

rate limits? #2

Open iamvishnurajan opened 8 months ago

iamvishnurajan commented 8 months ago

Hi,

Thank you for building this as I like being able to work in R. I was wondering if there is a way to get the current rate limits? I seem to be running into some kind of limit when trying to delete records on bsky via bskyr but I am unable to figure out which one.

Thanks much

christopherkenny commented 8 months ago

Thanks for using the package @iamvishnurajan. Could you provide any context for how you're doing this? For example, are you looping through calls to bs_delete_record() and letting it re-authenticate each time? What error do you receive?

CrumpLab commented 8 months ago

I have hit a similar issue. My case is adding people to a list.

I have a loop like this, which adds a single user to a list.

for(i in 1:x){
      bs_create_record(
        collection =  'app.bsky.graph.listitem',
        record = list(
          '$type' = 'app.bsky.graph.listitem',
          'subject' = follows$did[i],
          'list' = '{my list here}',
          'createdAt' = bs_created_at()
           )
        )
      Sys.sleep(1)
}

I had run set_bluesky_user() and set_bluesky_pass() once at the beginning of the session. I tried various settings for Sys.sleep() to slow the requests. Yesterday I got through about 100+ iterations before the following error:

Error in `httr2::req_perform()`:
! HTTP 429 Too Many Requests.
Backtrace:
  1. bskyr::bs_get_follows("mattcrumplab.bsky.social", limit = 500)
  9. bskyr::bs_auth(user, pass)
 13. httr2::req_perform(req)

Based on this rate limit information on this page I was surprised that I was hitting limits, but I'm very new this api and haven't read all the docs: https://docs.bsky.app/blog/rate-limits-pds-v3.

Interestingly after not touching it since yesterday evening, I am still getting this 429 error this morning.

Thanks again for the package and for any insights on the rate limit issue.

CrumpLab commented 8 months ago

I was able to work out my issue thanks to your comment about "re-authenticating every time".

In my loop I guess bs_create_record() was calling bs_auth() and re-authenticating every time. This caused me to hit the createSession limit, and be blocked out for 24 hours.

In case it is is useful for others, this ended up working like a charm.

#run once to start session
my_auth <- bs_auth(user,pass) # save authentication in a variable

for(i in 1:x){
      bs_create_record(
        collection =  'app.bsky.graph.listitem',
        record = list(
          '$type' = 'app.bsky.graph.listitem',
          'subject' = follows$did[i],
          'list' = '{my list here}',
          'createdAt' = bs_created_at(),
           user = "myusername",
           pass = "mypasscode",
           auth = my_auth #use the existing auth var, don't call bs_auth()
           )
        )
      Sys.sleep(1)
}

Apologies for jumping in on this issue. I also think it would be great to have a verbose option for looking at rate limit returns.

Thanks again for your work on this package!

iamvishnurajan commented 8 months ago

Hi, thanks for the tip and no problem at all to jump in here. I also was doing a similar for loop with a sys.sleep(), but when I get some time to work on my code more next week, I'll try a similar authentication method.

In my case, I was able to rerun after 5-10 minutes, so perhaps mine may be a different issue. I found I could delete about 30 records before it rate limited me.

iamvishnurajan commented 8 months ago

Hi, just to close this, adding a similar auth statement in my code worked for me also. I was able to delete more than 30 records without a problem now using the following.

for (pd in 1:nrow(posts_delete)){
  bs_delete_record(collection="app.bsky.feed.post",
                   rkey=posts_delete$record[pd],
                   auth=my_auth)
  Sys.sleep(0.5)
}