Get the date/time a badge was awarded?

2019-08-18 02:42发布

问题:

Using this, it is possible to take the badges from a specific user of Stack Overflow:

library(stackr) 
badges <- stack_users(9371451, "badges", num_pages=100000, pagesize=100)

How can I add a parameter to take also the timestamp that the badge was awarded to the user? And if possible, for which answer?

回答1:

It is possible-ish with the Stack Exchange API, but not with the stackr library you are using.

The /users/{ids}/badges route returns a list of badge objects, which only has these possible properties:

    award_count     integer
    badge_id        integer, refers to a badge
    badge_type      one of named, or tag_based
    description     string 
    link            string 
    name            string
    rank            one of gold, silver, or bronze
    user            shallow_user 

So you can't get the timestamp or triggering post there.

However, you can get this information (mostly) from the /notifications route, which can return results like:

{ "items": [ {
      "site": {"site_url": "https://webmasters.stackexchange.com"},
      "is_unread": false,
      "creation_date": 1520234766,
      "notification_type": "badge_earned",
      "body":   `You've earned the \"Notable Question\" badge for 
                <a href=\"http://webmasters.stackexchange.com/questions/65822\">
                How to bulk delete email accounts from cPanel / my hosting account?</a>.`
                //  Manually wrapped for this post
    },
    etc.

But, important:

  1. /notifications requires authentication and only works for a logged-in (via the API) user.
  2. That stackr library does not support authentication. (See your previous question in a bit.)
  3. /notifications returns all of a given user's Stack Exchange sites, so you will have to filter out the ones you are not interested in.
  4. /notifications returns several kinds of notices, so you will have to filter out the ones that are not badge related.
  5. /notifications does not return badge details like rank, so you will still need to call /users/{ids}/badges and marry the results.
  6. For higher rep users, it is possible that you would exhaust your API quota before being able to fetch all of that user's notifications.


回答2:

you can use users/{ids}/timeline. See description page:

Returns a subset of the actions the users in {ids} have taken on the site.

This method returns users' posts, edits, and earned badges in the order they were accomplished.

library(stackr)
df_timeline <- stackr:::stack_GET("users/9371451/timeline", num_pages = 10000)

The ::: is necessary because the function stack_GET is an internal command