在PHP Codeigniter的给定时间段内,通过跟踪API高效地关注Pinterest用户

时间:2017-08-18 07:59:24

标签: php api codeigniter curl pinterest

Pinterest API如下: 限制每个用户访问令牌每小时1000次点击 https://api.pinterest.com/v1/me/following/users/?access_token=XXXXXXXX&user=XXXXXXX

+-------------+---------------------+------------------+
|   userId    | usertoFollowPerHour | maxUserAvailable |
+-------------+---------------------+------------------+
| 1           |                 100 |              1000|
| 2           |                 200 |              9000|
| 3           |                 210 |               100|
| 4           |                 300 |              1100|
| 5           |                 300 |               900|
| .           |                     |                  |
| .           |                     |                  |
| .           |                     |                  |
| n           |                    n|                 n|
+-------------+---------------------+------------------+

现在我必须关注特定用户ID的用户,以便:     我可以在限制范围内关注用户     Pinterest不会阻止我的个人资料

目前,我这样做是:     每隔3小时就会命中以下函数的URL的cron     功能是:

  1. 计算用户在一个循环中跟随Eg:

        1. let currentTime = 1:00
            and endTime = 2:00
            user to follow = 300
            timeRemaining = currentTime-endTime => 60 minutes
            300/60 = 5 => ceil(5)=5
        2. it will fetch five user in one iteration from Pinterest
        3. than it will follow them one by one in second loop 
        4. after that again it will calculate until userToFllow become 0
            300-5=295 =>  ceil(295/timeRemaining(let 59)) = 5
        5. again 1 for next user
    

    OUTPUT

  2. 根据上述逻辑,我们的一些用户被禁止/阻止可能是由于Pinterest reason

    如果可能,请建议我使用解决方案回答,是否应该使用多线程或更多功能来执行此异步。如果只能由CodeIgniter完成,我会很高兴

2 个答案:

答案 0 :(得分:1)

So the follow this logic in your api:

Each app (with a unique app ID) is allowed 1000 calls per hour for each unique user token. The 60-minute window is a sliding window based on when you make your first request. If you hit your rate limit, you’ll only have to wait a max of 1 hour to get a few more requests.

Every API response returns a header that gives you an update about rate limiting. X-Ratelimit-Limit is the rate limit for that specific request, and X-Ratelimit-Remaining is the number of requests you have left in the 60-minute window.

X-Ratelimit-Limit: 1000
X-Ratelimit-Remaining: 890

Add this condition in your api response header and checking how many remaining .

If you exceed your rate limit for a given endpoint, you’ll get a 429 “Too many requests” error code.

Check for this condition response and trigger email to yourself if the http Code is 429 or log into the log file.

PS: Did not try this but sure it will help you to solve your problem in same way.

Source: Pintrest Documentation

答案 1 :(得分:0)

我通过在两个关注API调用之间为同一用户的Pinterest帐户添加 delay 解决了上述帐户禁止问题:

  • 上述方法每分钟跟踪一次5个用户,这将导致 机器人活动(如果所有关注者仅属于一个帐户)。
  • 如果我们将每小时最多 300个关注作为安全限制,则我们可以 在一分钟内关注每个帐户5个用户,其中间隔为12秒 或介于8到12之间的随机数,以获得更人性化的操作。
  • 我们有12秒的时间间隔可以处理其他帐户的队列,因此 我们有效地使用资源
  • 我们必须每个帐户限制最多50K跟踪,因为这是标准限制。

一分钟之内有五个跟随这样

队列:A1,A1,A2,A3,A1

A1-A1-A2-A3-A1-----------------------wait till the end of the minute-------

现在

队列:A1,A1,A2,A3,A1

A1-A2-A3---------A1-----------A1-----wait till the end of the minute-------

A 代表帐户

- 代表秒数

这只是避免禁令的基本实施概述。