Enrollments API giving a different enrollment count on every run

Neil47
Community Explorer

Hello,

I run a python script to get a list of all enrollments for a course with 3800+ enrollments using the api/v1/courses/{course_id}/enrollments API endpoint.

But on every run, I am getting a different number of enrollments which are never accurate. The script gave us consistent and correct output until last week. It also gives us the desired output for courses with less than 500 enrollments.

Why does this happen and is there a fix for it?

 

import csv
import time
import logging
import datetime
import requests


def create_log():
    timestamp = datetime.datetime.now().strftime("%d-%m-%y_%H%M%S")
    logfile = f"koc_scriptlog_{timestamp}.log"
    logging.basicConfig(
        filename=logfile,
        filemode="w",
        level=logging.DEBUG,
        format="%(asctime)s - %(message)s",
    )


def main():

    create_log()
    course_id = 3010
    BASE_URL = f"https://companyname.org/api/v1/courses/{course_id}/enrollments"
    TOKEN = {"Authorization": "Bearer tokenkey"}
    page = "next"
    count = 0
    t_count = 0

    while page == "next":

        # Sleep for 5sec after 100 calls
        if t_count % 100 == 0:
            time.sleep(5)
        t_count += 1

        res = requests.get(BASE_URL, headers=TOKEN)
        if res.status_code == 200:
            count += 1
            print(f"{count}. ", end=" ")
            page_links = res.links
            logging.info(f"Call {count}. {res.url} | {page_links}")
            if "next" in page_links:
                if "url" in page_links.get("next"):
                    new_url = res.links["next"]["url"]
                    print(new_url)
                    BASE_URL = new_url
                    page = "next"
                    responses_json = res.json()
                    temp_list = []
                    for res_json in responses_json:
                        r = [
                            res_json["user_id"],
                            res_json["user"]["name"],
                            res_json["user"]["login_id"],
                            res_json["enrollment_state"],
                            res_json["role"],
                            res_json["user"]["sis_user_id"],
                        ]
                        temp_list.append(r)
                        logging.info(r)
                    with open(
                        "output.csv", mode="a", newline="", encoding="utf-8"
                    ) as csvfile:
                        canvas_writer = csv.writer(
                            csvfile, delimiter=",", quotechar='"'
                        )
                        canvas_writer.writerows(temp_list)
            else:
                print("Next page not found")
                page = "last"


main()

 

 

Labels (1)
0 Likes