Python tweak to pull Unpublished Courses Report

Jump to solution
shane_ohara
Community Champion

Hi - I am seeking some python support to tweak code that I found out on Github for pulling some reports. The code is at canvas/pull_provisioning_report.py at master · unsupported/canvas · GitHub​, but I really am looking to get the 'unpublished_courses_csv' report dumped to me. I am not a coder by any stretch and am attempting to learn, however my tweaks never get the intended report. Any and all help will be widely thanked.

Shane

Labels (1)
1 Solution
dgrobani
Community Champion

Shane,

I did some quick & dirty tweaking and pruning to that code while converting it to Python 3 and got the report with what's below.

Make sure you replace your token, Canvas subdomain, and enrollment term in lines 8, 10, and 12, and let me know if you run into any issues.

Cheers,

Daniel

#!/usr/bin/env python

# working as of 4/19/2016

import requests

import time

import re

# Change this to match your access token

TOKEN = "<token_here>"

# Change this to match the domain you use to access Canvas.

CANVAS_DOMAIN = "<domain>.instructure.com"

# Change this to the term to pull for, otherwise this will pull for all terms.

ENROLLMENT_TERM = "<term id>"

###################################################################################

#### DON'T CHANGE anything after this unless you know what you are doing. #########

BASE_DOMAIN = "https://%s/api/v1/%%s/" % CANVAS_DOMAIN

BASE_START_URI = BASE_DOMAIN % "accounts/self/reports/%s"

BASE_FILE_URI = BASE_DOMAIN % "files/%s"

headers = {"Authorization": "Bearer %s" % TOKEN}

report_parameters = {"parameters[enrollment_term_id]": ENROLLMENT_TERM}

# Step 1: Start the report

start_report_url = BASE_START_URI % 'unpublished_courses_csv'

print("running the report...")

start_report_response = requests.post(start_report_url, headers=headers, params=report_parameters)

print(start_report_response.text)

# Use the id from that output to check the progress of the report.

status_url = start_report_url + "%s" % start_report_response.json()['id']

status_response = requests.get(status_url, headers=headers)

status_response_json = status_response.json()

# Step 2: Wait for the report to be finished

while status_response_json['progress'] < 100:

    status_response = requests.get(status_url, headers=headers)

    status_response_json = status_response.json()

    time.sleep(4)

    print('report progress', status_response_json['progress'])

# Once "progress" is 100 then parse out the number between "files" and "download" and use this to request the files

file_id_pattern = re.compile('files/(\d+)/download')

# Step 3: Download the file

file_info_url = status_response_json['attachment']['url']

file_response = requests.get(file_info_url, headers=headers, stream=True)

# Step 4: Save the file

with open(status_response_json['attachment']['filename'], 'w+b') as filename:

    filename.write(file_response.content)

View solution in original post