Home > database >  How to count somekind of results of a request?
How to count somekind of results of a request?

Time:12-06

I am pursuing a MSc in Data Science and in the subject of Python I have the statement below:

Call 100 times the following URL and count how many calls have three or less participants.

The API is: http://www.boredapi.com/api/activity/

After I understood the statement I build up the function below:

import requests

total_calls = 100
call_0 = 0

def calls(total_calls, call_0):
    while total_calls > call_0:
          activity = ('http://www.boredapi.com/api/activity/')
          call_n = requests.get(activity)
          print(call_n.text)
          call_0  = 1

          if total_calls - call_0 < 0:
            print(call_0)
          elif total_calls - call_0 == 0:
            break
    return output_call

output_call = calls(total_calls, call_0)
output_call

I am stuck up because I don't know how to count how many times "output_call" have <= 3 participants.

If I run for example 9 times instead of 100 there is a result:

{"activity":"Hold a yard sale","type":"social","participants":1,"price":0,"link":"","key":"1432113","accessibility":0.1}
{"activity":"Meditate for five minutes","type":"relaxation","participants":1,"price":0,"link":"","key":"3699502","accessibility":0.05}
{"activity":"Draw and color a Mandala","type":"relaxation","participants":1,"price":0.05,"link":"https://en.wikipedia.org/wiki/Mandala","key":"4614092","accessibility":0.1}
{"activity":"Go to a local thrift shop","type":"recreational","participants":1,"price":0.1,"link":"","key":"8503795","accessibility":0.2}
{"activity":"Organize your basement","type":"busywork","participants":1,"price":0,"link":"","key":"8203595","accessibility":0.9}
{"activity":"Back up important computer files","type":"busywork","participants":1,"price":0.2,"link":"","key":"9081214","accessibility":0.2}
{"activity":"Fix something that's broken in your house","type":"diy","participants":1,"price":0.1,"link":"","key":"6925988","accessibility":0.3}
{"activity":"Clean out your closet and donate the clothes you've outgrown","type":"charity","participants":1,"price":0,"link":"","key":"9026787","accessibility":0.1}
{"activity":"Go to the gym","type":"recreational","participants":1,"price":0.2,"link":"","key":"4387026","accessibility":0.1}
{}

CodePudding user response:

You could proceed like that.

Design your calls() function to return the number of calls you're interested in.

Then initialize a counter to 0 and increment it when number of participants lte 3.

range() is the builtin Python function you use very often to loop n times.

From the request result you'd better ask for JSON instead of text, which gives you a Python dictionary (equivalent to its JSON counterpart).

Access the value number of participants by using the participant key.

activity = "http://www.boredapi.com/api/activity/"
total_calls = 100

def calls(total_calls: int) -> int:
    counter = 0
    for _ in range(total_calls):
        r = requests.get(activity)
        if r.ok:
            if r.json()["participants"] <= 3:
                counter  = 1
    return counter

calls(total_calls)

CodePudding user response:

Following should work fine for you:

import requests, json

total_calls = 100
call_0 = 0


def calls(total_calls, call_0):
    less_than_3_count = 0
    while total_calls > call_0:
        # Check break condition in the beginning 
        if total_calls - call_0 == 0:
            break
        
        activity = 'http://www.boredapi.com/api/activity/'
        response = requests.get(activity)
        call_0  = 1
        print(call_0, response.text)
        json_object = json.loads(response.text)
        if json_object['participants'] <= 3:
            less_than_3_count  = 1
    
    return less_than_3_count


output_call = calls(total_calls, call_0)
output_call

CodePudding user response:

import requests


def calls(total_calls):
  output_calls = 0
  activity = 'http://www.boredapi.com/api/activity/'
  while total_calls > 0:
    call_n = requests.get(activity).json()
    if call_n['participants'] <= 3:
      output_calls  = 1
    total_calls -= 1
  return output_calls

output_call = calls(total_calls=100)  # change the total call value here
print(output_call)

Assuming you will handle exceptions for requests.

CodePudding user response:

Here is the simplified and pythonic version;

import requests
from pprint import pp


def calls(count=100):
    bigger_then_3 = 0
    bigger_then_3_calls = []
    for cal in range(count):
        resp = requests.get("http://www.boredapi.com/api/activity/").json()
        if resp["participants"] <= 3:
            bigger_then_3_calls.append(resp)
            bigger_then_3  = 1
    return bigger_then_3, bigger_then_3_calls


if __name__ == '__main__':
    bigger_count, bigger_calls = calls(10)
    print(f"Bigger count: {bigger_count}")
    pp(bigger_calls)
  • Related