Created
August 19, 2020 22:49
-
-
Save jaclynsaunders/3e6d8ca943d1b9215cfb66f9f7476bc4 to your computer and use it in GitHub Desktop.
emails-pull-canvas-zoom-breakouts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "emails-pull-canvas-zoom-breakouts", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jaclynsaunders/3e6d8ca943d1b9215cfb66f9f7476bc4/emails-pull-canvas-zoom-breakouts.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CGuba4cJam2m", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import pandas as pd\n", | |
"import requests\n", | |
"import json" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "pOuSz4Z4rvyR", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Pull student e-mails by student groups in Canvas course to upload to zoom for breakout sessions\n", | |
"\n", | |
"To get OAuth Token from Canvas: https://community.canvaslms.com/t5/Admin-Guide/How-do-I-manage-API-access-tokens-as-an-admin/ta-p/89\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TMibHPHsazcg", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#Enter your Course Admin OAuth token from Canvas here: \n", | |
"key = \"my-super-long-authorization-token-12345\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Q5j7Uh-LfLiV", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#You will need to update the url to reflect your institution canvas site and specific course number (using \"example.edu\" and course number \"12345\")\n", | |
"response = requests.get(\"https://canvas.example.edu/api/v1/courses/12345/group_categories\", \\\n", | |
" headers={'Authorization': f'Bearer {key}'})" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "z2YcD2c73NxG", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Check to make sure everything is setup correctly with your query. If everything went smoothly \"200\" should print out" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XsaOxQ4_fc99", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "72e3f183-72dd-468a-ddb6-017cb981bf30" | |
}, | |
"source": [ | |
"#response = 200 ==> good request, data returned\n", | |
"#response = 401 ==> bad authorization, check to make sure token still works\n", | |
"#response = 404 ==> bad url request, web location no good\n", | |
"response.status_code" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"200" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 15 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "hkdsOfjQflzV", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#The data returned from your query\n", | |
"data = response.json()" | |
], | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "5klY9rILg2ss", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"df_groupList = pd.DataFrame.from_dict(data)\n", | |
"df_groupList" | |
], | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "is9E_JLwhxRe", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"df = pd.DataFrame()\n", | |
"for i in df_groupList[\"id\"].unique():\n", | |
" print(i) \n", | |
" #update url as appropriate for your institution and course version\n", | |
" response2 = requests.get(f\"https://canvas.example.edu/api/v1/group_categories/{i}/groups?per_page=100\", \\\n", | |
" headers={'Authorization': f'Bearer {key}'})\n", | |
" print(response2.status_code) #make sure request was good\n", | |
" data_out = response2.json()\n", | |
" df_out = pd.DataFrame.from_dict(data_out)\n", | |
" df = df.append(df_out)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "EW37bA-Eksbf", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"df.head()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TxyvTUzEiGS1", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"groupsKey = df[[\"id\", \"name\"]]\n", | |
"#groupsKey" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "u2O66hf2kJAI", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"df_names = pd.DataFrame()\n", | |
"for i in df[\"id\"].unique():\n", | |
" print(i)\n", | |
" ### Update url as appropriate\n", | |
" response3 = requests.get(f\"https://canvas.example.edu/api/v1/groups/{i}/users\", \\\n", | |
" headers={'Authorization': f'Bearer {key}'})\n", | |
" print(response3.status_code) #make sure the request was good\n", | |
" data_out = response3.json()\n", | |
" df_out = pd.DataFrame.from_dict(data_out)\n", | |
" df_out[\"main group\"] = i\n", | |
" df_names = df_names.append(df_out)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sDDIXA2kmFbI", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"df_names_groups = pd.merge(df_names, groupsKey, how=\"left\", left_on=\"main group\", right_on=\"id\")\n", | |
"print(len(df_names), len(df_names_groups)) #check merge - both should be same length\n", | |
"##Update e-mail info\n", | |
"df_names_groups[\"e-mail\"] = df_names_groups[\"login_id\"].astype(\"str\") + \"@example.edu\"\n", | |
"df_names_groups.head()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fOgnwPYLmMem", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"zoom_out = df_names_groups[[\"name_y\", \"e-mail\"]]\n", | |
"zoom_out" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ViziiK0ooSsN", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#If running from coloab, export to your google drive\n", | |
"from google.colab import drive\n", | |
"drive.mount('drive')\n", | |
"zoom_out.to_csv('zoom_groups_data.csv')\n", | |
"!cp zoom_groups_data.csv \"drive/My Drive/\"\n", | |
"df_names_groups.to_csv(\"groups_data.csv\")\n", | |
"!cp groups_data.csv \"drive/My Drive/\"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment