4

I want to count the number of answers for some specific users within a certain time range.I can't find the answers for those, though. The following script doesn't return anything:

from stackapi import StackAPI
from pprint import pprint
user_list = [12153576, 11148139]
SITE = StackAPI('stackoverflow')
answers = SITE.fetch('answers',fromdate=1591488000, todate=1591747200)
user_ids = SITE.fetch("users/{ids}", ids=user_list)
for user in user_ids['items']:
    for ans in answers['items']:
        if user['user_id'] == ans['owner']['user_id']:
            pprint(ans['owner']['display_name'])

Answers do exist, however, for example: user 12153576's answer and user 11148139's answer.

zabop
  • 6,750
  • 3
  • 39
  • 84
A_Suh
  • 3,727
  • 6
  • 22

2 Answers2

4

You should use the answers on users (aka users/{ids}/answers) as you mentioned. After passing the fromdate parameter to the request, you should get the length of the items returned.

Currently, there is a bug with Stack Exchange and total will always be 0. You can use len, though instead of a counter and a for loop, since those make the script inefficient.

from stackapi import StackAPI

user_list = [12153576, 11148139]
fromdate = 1591488000
SITE = StackAPI('stackoverflow')

answers = SITE.fetch('users/{ids}/answers', ids = user_list, fromdate = fromdate)
print(len(answers['items']))
double-beep
  • 5,031
  • 17
  • 33
  • 41
1

Came up with following solution

from stackapi import StackAPI
from pprint import pprint
from datetime import datetime, timedelta
user_list = [12153576, 11148139]
fromdate=1591488000
cnt = 0    
SITE = StackAPI('stackoverflow')

answers = SITE.fetch('users/{ids}/answers', ids=user_list)
for ans in answers['items']:
    if (ans['creation_date']) > fromdate:
        cnt = cnt + 1
print("cnt:", cnt)
A_Suh
  • 3,727
  • 6
  • 22