In most of my programs that use the Canvas API, I take in the course_id from the command line in numeric form (i.e., the string: 11). One of my colleagues said that he does not like to remember the course numbers but would rather use the course code or a nickname. So this motivated me to see if one could use the dashboard information for this.
The first thing I discovered was that it seems the dashboard API is not documented - or perhaps I just could not find it. So I watched a Canvas session in the browser and found the API is:
GET /api/v1/dashboard/dashboard_cards
So I made a test program to get all of my cards and make a spreadsheet of them, see my-dashboard.py at GitHub - gqmaguirejr/Canvas-tools: Some tools for use with the Canvas LMS.
After looking at the cards and their information it was really easy to see how to use this information to make it so that you can convert the "course_id" that is actually a nickname, short name, or original name (or a prefix or substring of it).
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">course_id_from_assetString</SPAN><SPAN class="punctuation token">(</SPAN>card<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">global</SPAN> Verbose_Flag<BR /><BR /> course_id<SPAN class="operator token">=</SPAN>card<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'assetString'</SPAN><SPAN class="punctuation token">]</SPAN><BR /> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">if</SPAN> course_id<SPAN class="punctuation token">.</SPAN>startswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'course_'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'course_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">if</SPAN> Verbose_Flag<SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"course_id_from_assetString:: course_id={}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">return</SPAN> course_id<BR /> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error missing assetString for card {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>card<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">return</SPAN> None<BR /><BR /><SPAN class="comment token"># check if the course_id is all digits, matches course code, or matches a short_name</SPAN><BR /><SPAN class="keyword token">def</SPAN> <SPAN class="token function">process_course_id_from_commandLine</SPAN><SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> course_id<SPAN class="punctuation token">.</SPAN>isdigit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN><BR /> cards<SPAN class="operator token">=</SPAN>list_dashboard_cards<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">for</SPAN> c <SPAN class="keyword token">in</SPAN> cards<SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="comment token"># look to see if the string is a course_code</SPAN><BR /> <SPAN class="keyword token">if</SPAN> course_id <SPAN class="operator token">==</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'courseCode'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /> <SPAN class="comment token"># check for matched against shortName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> course_id <SPAN class="operator token">==</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /> <SPAN class="comment token"># look for the string at start of the shortName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>startswith<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"picked the course {} based on the starting match"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /> <SPAN class="comment token"># look for the substring in the shortName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"picked the course {} based on partial match"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /><BR /> <SPAN class="comment token"># check for matched against originalName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> course_id <SPAN class="operator token">==</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'originalName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /> <SPAN class="comment token"># look for the string at start of the shortName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'originalName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>startswith<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"picked the course {} based on the starting match"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /> <SPAN class="comment token"># look for the substring in the shortName</SPAN><BR /> <SPAN class="keyword token">if</SPAN> c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'originalName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN>course_id<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><BR /> course_id<SPAN class="operator token">=</SPAN>course_id_from_assetString<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"picked the course {} based on partial match"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'shortName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">break</SPAN><BR /><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"processing course: {0} with course_id={1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'originalName'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> course_id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">return</SPAN> course_id<BR /><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Now, hopefully, there will be a happy user as in the main program to process the first argument of the command line as a course_id you simply say:
course_id<SPAN class="operator token">=</SPAN>process_course_id_from_commandLine<SPAN class="punctuation token">(</SPAN>remainder<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> course_id<SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Unable to recognize a course_id, course code, or short name for a course in {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>remainder<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">return</SPAN><BR /><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Of course, there are probably some gotchas - but it should work better than having to look up the numeric values.