diff --git a/courses.py b/courses.py index 99ddaa0..57c18b6 100644 --- a/courses.py +++ b/courses.py @@ -1,7 +1,7 @@ - -import json, re, requests, codecs, sys, time, funcy, os +# test +import json, re, requests, codecs, sys, time, funcy import pandas as pd -#from tabulate import tabulate +from tabulate import tabulate from dateutil import parser from datetime import datetime from util import print_table @@ -249,7 +249,9 @@ def scrape_bookstore(): # Input: xxxx_sched.json. Output: xxxx_latestarts.txt def list_latestarts(): #term = input("Name of current semester file? (ex: sp18) ") - term = "sp23" # sems[0] + term = "sp23" # sems[0] + + dept_ignore = "JFT JLE CWE".split(" ") term_in = "cache/" + term + "_sched.json" term_out = "cache/" + term + "_latestarts.txt" @@ -260,12 +262,13 @@ def list_latestarts(): #print sched by_date = {} for C in sched: + dept = C['code'].split(" ") + dept = dept[0] + if dept in dept_ignore: + continue + print(dept) parts = C['date'].split("-") start = parts[0] - codes = C['code'].split(' ') - dept = codes[0] - if dept in ['JLE','JFT','CWE']: - continue if re.search('TBA',start): continue try: startd = parser.parse(start) @@ -279,11 +282,11 @@ def list_latestarts(): #print "Start: " + str(X) if len(by_date[X]) < 200: prettydate = X.strftime("%A, %B %d") - print(prettydate + ": " + str(len(by_date[X])) + " courses") + #print(prettydate + ": " + str(len(by_date[X])) + " courses") outfile.write(prettydate + ": " + str(len(by_date[X])) + " courses" + "\n") for Y in by_date[X]: #print "\t" + Y['code'] + " " + Y['crn'] + "\t" + Y['teacher'] - print(Y) + #print(Y) #outfile.write("\t" + Y['code'] + " " + Y['crn'] + "\t" + Y['teacher'] + "\t" + Y['type'] +"\n") outfile.write("\t" + Y['code'] + " " + Y['crn'] + "\t" + Y['teacher'] + "\t" + Y['type'] + "\t" + "\n") @@ -332,20 +335,14 @@ def users_in_depts_live(depts=[], termid='171'): def course_enrollment(id=''): - print("Getting enrollments for course id %s" % str(id)) if not id: id = input('Course id? ') t = url + '/api/v1/courses/%s/enrollments?role[]=StudentEnrollment' % str(id) - print(t) emts = fetch(t,0) - print(emts) + #print(emts) emt_by_id = {} for E in emts: - print(E) - try: - emt_by_id[E['user_id']] = E - except Exception as exp: - print("Skipped that class with this exception: %s" % str(exp)) + emt_by_id[E['user_id']] = E ff = codecs.open('cache/courses/%s.json' % str(id), 'w', 'utf-8') ff.write(json.dumps(emt_by_id, indent=2)) print( " %i results" % len(emts) ) @@ -510,18 +507,11 @@ GROUP BY c.code ORDER BY c.state, c.code""" % (S['id'],S['id']) outp2.write("\n\n---------\nNOT PUBLISHED\n\n" + json.dumps(notpub, indent=2)) # Fetch all courses in a given term -def getCoursesInTerm(term=0,get_fresh=1,show=0,active=0): # a list - if not term: - term = getTerms(1,1) - ff = 'cache/courses_in_term_%s.json' % str(term) - if not get_fresh: - if os.path.isfile(ff): - return json.loads( codecs.open(ff,'r','utf-8').read() ) - else: - print(" -> couldn't find cached classes at: %s" % ff) - +def getCoursesInTerm(term=0,show=1,active=0): # a list # https://gavilan.instructure.com:443/api/v1/accounts/1/courses?published=true&enrollment_term_id=11 names = [] + if not term: + term = getTerms(1,1) if active: active = "published=true&" else: @@ -541,7 +531,6 @@ def getCoursesInTerm(term=0,get_fresh=1,show=0,active=0): # a list names.append(a['name']) info.append( [a['id'], a['name'], a['workflow_state'] ] ) if show: print_table(info) - codecs.open(ff, 'w', 'utf-8').write(json.dumps(results,indent=2)) return results @@ -713,32 +702,18 @@ def all_semester_course_sanity_check(): codecs.open('cache/courses_in_term_178.json','w','utf-8').write(json.dumps(c,indent=2)) output = codecs.open('cache/courses_w_sections.csv','w','utf-8') output.write( ",".join(['what','id','parent_course_id','sis_course_id','name']) + "\n" ) - output2 = codecs.open('cache/courses_checker.csv','w','utf-8') - output2.write( ",".join(['id','sis_course_id','name','state','students']) + "\n" ) i = 0 for course in c: - u2 = url + '/api/v1/courses/%s?include[]=total_students' % str(course['id']) - course['info'] = fetch(u2) - #print(json.dumps(course['info'],indent=2)) - ts = '?' - try: - ts = course['info']['total_students'] - except Exception as e: - pass - info = [ 'course', course['id'], '', course['sis_course_id'], course['name'], course['workflow_state'], ts ] + info = [ 'course', course['id'], '', course['sis_course_id'], course['name'], ] info = list(map(str,info)) - info2 = [ course['id'], course['sis_course_id'], course['name'], course['workflow_state'], ts ] - info2 = list(map(str,info2)) - output2.write( ",".join(info2) + "\n" ) - output2.flush() - print(info2) + print(info) output.write( ",".join(info) + "\n" ) - #uu = url + '/api/v1/courses/%s/sections' % str(course['id']) - #course['sections'] = fetch(uu) - #s_info = [ [ 'section', y['id'], y['course_id'], y['sis_course_id'], y['name'], y['total_students'] ] for y in course['sections'] ] - #for row in s_info: - # print(row) - # output.write( ",".join( map(str,row) ) + "\n" ) + uu = url + '/api/v1/courses/%s/sections' % str(course['id']) + course['sections'] = fetch(uu) + s_info = [ [ 'section', y['id'], y['course_id'], y['sis_course_id'], y['name']] for y in course['sections'] ] + for row in s_info: + print(row) + output.write( ",".join( map(str,row) ) + "\n" ) output.flush() i += 1 if i % 5 == 0: @@ -864,9 +839,9 @@ def unenroll_student(courseid,enrolid): def enroll_stem_students_live(): - the_term = '178' + the_term = '176' do_removes = 0 - depts = "MATH BIO CHEM CSIS PHYS PSCI GEOG ASTR ECOL ENVS ENGR".split(" ") + depts = "MATH BIO CHEM CSIS PHYS GEOG ASTR ECOL ENVS ENGR".split(" ") users_to_enroll = users_in_depts_live(depts, the_term) # term id stem_enrollments = course_enrollment(stem_course_id) # by user_id @@ -1043,21 +1018,21 @@ def enroll_orientation_students(): s = cursor.fetchall() if s: s = s[0] - print(" + Enrolling: %s" % s[0]) + print("Enrolling: %s" % s[0]) t = url + '/api/v1/courses/%s/enrollments' % ori_shell_id data = { 'enrollment[user_id]': j, 'enrollment[type]':'StudentEnrollment', 'enrollment[enrollment_state]': 'active' } - #print(data) + print(data) r3 = requests.post(t, headers=header, params=data) eee += 1 - #print(r3.text) - time.sleep(0.600) + print(r3.text) + time.sleep(0.200) except Exception as e: - print(" - Something went wrong with id %s, %s, %s" % (j, str(s), str(e))) + print("Something went wrong with id %s, %s, %s" % (j, str(s), str(e))) return (eee,uuu) def enroll_o_s_students(): - #full_reload() + full_reload() (es,us) = enroll_stem_students_live() (eo, uo) = enroll_orientation_students() @@ -1150,18 +1125,20 @@ def instructor_list_to_activate_evals(): def add_evals(section=0): # show or hide? - hidden = True + hidden = False #s = [ x.strip() for x in codecs.open('cache/sp21_eval_sections.txt','r').readlines()] #s = [ x.split(',')[4].split('::') for x in codecs.open('cache/fa22_eval_sections.csv','r').readlines()] - s = [ x.strip() for x in codecs.open('cache/fa22_eval_sections.csv','r').readlines()] - print(s) + #s = [ x.strip() for x in codecs.open('cache/fa22_eval_sections.csv','r').readlines()] + s = [ x.strip() for x in codecs.open('cache/sp23_eval_sections.csv','r').readlines()] s = list(funcy.flatten(s)) s.sort() + print(s) xyz = input('hit return to continue') #c = getCoursesInTerm(168,0,1) #c = getCoursesInTerm(174,0,1) # sp22 - c = getCoursesInTerm(176,0,1) # fa22 + #c = getCoursesInTerm(176,0,1) # fa22 + c = getCoursesInTerm(178,0,1) # sp23 print(c) ids = [] courses = {} @@ -1173,7 +1150,7 @@ def add_evals(section=0): courses[str(C['id'])] = C ids.append(str(C['id'])) - ask = 0 + ask = 1 data = {'position':2, 'hidden':hidden} for i in ids: @@ -1183,7 +1160,7 @@ def add_evals(section=0): if a == 'q': return u2 = "https://gavilan.instructure.com:443/api/v1/courses/%s/tabs/context_external_tool_1953" % i r3 = requests.put(u2, headers=header, params=data) - print(r3) + print(r3.text) time.sleep(0.600) @@ -1376,7 +1353,6 @@ def set_ext_tools(): if __name__ == "__main__": options = { 1: ['Cross check schedule with ztc responses',make_ztc_list] , - 30: ['List latestart classes', list_latestarts ], 2: ['Add announcements to homepage', change_course_ann_homepage], 3: ['Cross-list classes', xlist ], 4: ['List students who passed quiz X', get_quiz_passers], @@ -1399,12 +1375,13 @@ if __name__ == "__main__": 21: ['Add announcements to homepage', change_course_ann_homepage], 22: ['Get a course info by id',getCourses], 23: ['Reset course conclude date',update_course_conclude], - #24: ['Add course evals to whole semester',instructor_list_to_activate_evals], + #24: ['Add course evalse to whole semester',instructor_list_to_activate_evals], 25: ['ext tools',get_ext_tools], 26: ['set ext tools',set_ext_tools], 27: ['Fine tune term dates and winter session', course_dates_terms], 28: ['Cross list a semester from file', semester_cross_lister], 29: ['Check all courses & their sections in semester', all_semester_course_sanity_check], + 30: ['List latestart classes', list_latestarts ], # TODO wanted: group shell for each GP (guided pathway) as a basic student services gateway.... # } @@ -1423,4 +1400,4 @@ if __name__ == "__main__": resp = input('Choose: ') # Call the function in the options dict - options[ int(resp)][1]() + options[ int(resp)][1]() \ No newline at end of file