1
0
mirror of https://github.com/balkian/gists.git synced 2024-11-01 08:01:44 +00:00
gists/repos/59fd9019f415fe8dd8f802c456be2b13/pybossa.py
J. Fernando Sánchez 24ac858c70 Add 'repos/59fd9019f415fe8dd8f802c456be2b13/' from commit '316b6f4f0390d8ec14872cc17a9f40ba7fc8f798'
git-subtree-dir: repos/59fd9019f415fe8dd8f802c456be2b13
git-subtree-mainline: 7d714103f6
git-subtree-split: 316b6f4f03
2021-10-30 15:12:29 +02:00

72 lines
2.0 KiB
Python

# coding: utf-8
import requests
import sys
HOST = 'http://localhost'
def get_count_all(user_name, host=HOST):
user = get_user(user_name, host)
user_id = user['id']
page = 0
total = 0
while True:
r = requests.get(host+'/api/taskrun',
params={'user_id': 2,
'limit': 100,
'offset': total},
headers={'content-type': 'application/json'})
if r.status_code != 200:
raise Exception('Error in call')
delta = len(r.json())
if not delta:
break
total += delta
return total
def get_user_name(user_id, host=HOST):
r = requests.get('{}/api/user/{}'.format(host, user_id),
headers={'content-type': 'application/json'})
if r.status_code != 200:
raise Exception(r)
return r.json()['name']
def get_user(user_name, host=HOST):
r = requests.get('{}/account/{}/'.format(host, user_name),
headers={'content-type': 'application/json'})
if r.status_code != 200:
raise Exception(r)
return r.json()['user']
def get_all_users(host=HOST):
r = requests.get('{}/account/'.format(host),
headers={'content-type': 'application/json'})
if r.status_code != 200:
raise Exception(r)
return [u['name'] for u in r.json()['accounts']]
def get_count(user_name, host=HOST):
user = get_user(user_name)
return user['n_answers']
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Get the number of responses.')
parser.add_argument('--host', type=str,
default=HOST, help='Pybossa hostname')
parser.add_argument('user_name', type=str, nargs='?',
help='User name')
args = parser.parse_args()
if args.user_name:
print(get_count(args.user_name, host=args.host))
else:
for user in get_all_users(args.host):
print(user, get_count(user, args.host))