Get the link of question and answer at user level using Stack Exchange API

别来无恙 提交于 2020-06-28 09:22:01

问题


Based on my previous question, I am able to get the list of all unaccepted answers within a date range at the user level using Stack Exchange API with the following curl command :

curl "https://api.stackexchange.com/2.2/users/10348758/answers?page=1&pagesize=100&fromdate=1588291200&todate=1592179200&order=desc&sort=activity&site=stackoverflow&access_token=my-access-token&key=my-key" | gunzip | jq '.items[] | select(.is_accepted == false)'

The JSON response for the above command is something like this :

 {
  "owner": {
    "reputation": 1595,
    "user_id": 10348758,
    "user_type": "registered",
    "profile_image": "https://www.gravatar.com/avatar/01ccf806a00d7deb1bf15323bc3edbe8?s=128&d=identicon&r=PG&f=1",
    "display_name": "Bhavya",
    "link": "https://stackoverflow.com/users/10348758/bhavya"
  },
  "is_accepted": false,
  "score": 0,
  "last_activity_date": 1592102481,
  "creation_date": 1592102481,
  "answer_id": 62367766,
  "question_id": 62367395,
  "content_license": "CC BY-SA 4.0"
}

In the above response, I am getting answer_id and question_id, the links to these answers and questions are not included in the response.

By including &filter=withbody, in the curl command I am able to get the body of answer.The modified curl command is :

curl "https://api.stackexchange.com/2.2/users/10348758/answers?page=1&pagesize=100&fromdate=1588291200&todate=1592179200&order=desc&sort=activity&site=stackoverflow&filter=withbody&access_token=my-access-token&key=my-key" | gunzip | jq '.items[] | select(.is_accepted == false)'

Is there any way, I can get the links of the answer and question included in the response? I referred to these custom filters documentation, but I am not able to understand how to apply them.

Can anyone help me with this?


回答1:


How about this answer?

Your current situation is as follows.

  1. The retrieved values from your URL includes both answer_id and question_id. In this case, the URLs for answer and question can be created like https://stackoverflow.com/a/{answer_id} and https://stackoverflow.com/q/{question_id}, respectively. I have mentioned at here.
  2. In your curl command, jq is used like jq '.items[] | select(.is_accepted == false)'.

From above situation, in this answer, I would like to propose to add the URLs of answer and question to the retrieved values by adding parameter to jq.

Modified jq command:

In this case, the modified jq command is as follows.

jq '[.items[] | select(.is_accepted == false) |= . + {"answerUrl": ("https://stackoverflow.com/a/" + (.answer_id|tostring)), "questionUrl": ("https://stackoverflow.com/q/" + (.question_id|tostring))}]'

Modified curl command:

When your curl command is modified, it becomes as follows.

curl "https://api.stackexchange.com/2.2/users/10348758/answers?page=1&pagesize=100&fromdate=1588291200&todate=1592179200&order=desc&sort=activity&site=stackoverflow&filter=withbody&access_token=my-access-token&key=my-key" | gunzip | jq '[.items[] | select(.is_accepted == false) |= . + {"answerUrl": ("https://stackoverflow.com/a/" + (.answer_id|tostring)), "questionUrl": ("https://stackoverflow.com/q/" + (.question_id|tostring))}]'

Result:

When above modified curl command is run, the following result is retrieved. You can see the values of answerUrl and questionUrl in the result.

[
  {
    "owner": {###},
    "is_accepted": false,
    "score": ###,
    "last_activity_date": ###,
    "creation_date": ###,
    "answer_id": ###,
    "question_id": ###,
    "content_license": "###",
    "answerUrl": "https://stackoverflow.com/a/###",  <--- Added
    "questionUrl": "https://stackoverflow.com/q/###"  <--- Added
  },
  ,
  ,
  ,
]

References:

  • Usage of /answers/{ids}
  • jq

Added:

When you want to include the title to the retrieved values, please use the custom filter as follows.

Modified jq command:

curl "https://api.stackexchange.com/2.2/users/10348758/answers?page=1&pagesize=100&fromdate=1588291200&todate=1592179200&order=desc&sort=activity&site=stackoverflow&access_token=my-access-token&key=my-key&filter=%21T%2AhPUiv_%28m%28rgrhfg_" | gunzip | jq '[.items[] | select(.is_accepted == false)]'
  • In this case, the filter of filter=%21T%2AhPUiv_%28m%28rgrhfg_ is added as the query parameter. By this, the title is added. In this custom filter, title,link,.share_link are included. By this, the title, answer and question links are included in the returned values. So the command of jq can be simpler.

Reference:

  • Custom Filters


来源:https://stackoverflow.com/questions/62606821/get-the-link-of-question-and-answer-at-user-level-using-stack-exchange-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!