import requests, re, csv, json, funcy, sys def dates(s): #print(s) m = re.match(r'(\d\d\d\d)\-(\d\d)\-(\d\d)',s) if m: s = m.group(2) + "/" + m.group(3) #print(s) return s # "Course Code","Start Date","End Date",Term,Delivery,CRN,Status,"Course Name","Course Description","Units/Credit hours","Instructor Last Name","Instructor First Name",Campus/College,"Meeting Days and Times","Pass/No Pass available?","Class Capacity","Available Seats","Waitlist Capacity","Current Waitlist Length","Meeting Locations","Course Notes",ZTC # ACCT103,2021-06-14,2021-07-23,"Summer 2021",Online,80386,Active,"General Office Accounting","This course is designed to prepare students for entry-level office accounting positions. Emphasis is on practical accounting applications. This course has the option of a letter grade or pass/no pass. ADVISORY: Eligible for Mathematics 430."," 3.00","Valenzuela Roque",Karla,"Gavilan College"," ",T," 30"," 18"," 20"," 0",,, def parse_www_csv_sched(): old_keys = [ "CRN","Course Code","Units/Credit hours","Course Name","Meeting Days and Times","Class Capacity","Available Seats","Waitlist Capacity","Current Waitlist Length","Instructor Last Name","Start Date","Meeting Locations","ZTC","Delivery","Campus/College","Status","Course Description","Pass/No Pass available?","Course Notes" ] # "Instructor First Name","End Date","Term", new_keys = [ "crn", "code","cred", "name", "days", "cap", "rem", "wl_cap", "wl_act", "teacher", "date", "loc", "ztc", "type", "site","status","desc","pnp","note" ] # "time","act","wl_rem", "partofday", url = "https://gavilan.edu/_files/php/current_schedule.csv" sched_txt = requests.get(url).text.splitlines() sched = {"Fall 2021":[], "Spring 2022":[], "Winter 2022":[], "Summer 2021":[]} shortsems = {"Fall 2021":"fa21", "Spring 2022":"sp22", "Winter 2022":"wi22", "Summer 2021":"su21","Summer 2022":"su22","Fall 2022":"fa22"} for row in csv.DictReader(sched_txt): d = dict(row) for (old_key,new_key) in zip(old_keys,new_keys): d[new_key] = d.pop(old_key).strip() d['teacher'] = d.pop('Instructor First Name').strip() + " " + d['teacher'] d['date'] = dates(d['date']) + '-' + dates(d.pop('End Date').strip()) d['term'] = shortsems[d.pop('Term')] if d['cred'] == ".00": d['cred'] = "0" if d['type'] == "Online": d["loc"] = "ONLINE" d["site"] = "Online" d["type"] = "online" #d.pop('Instructor First Name').strip() + " " + d['teacher'] #d["code"] = d.pop("Course Code") #d["crn"] = d.pop("CRN") sched[row['Term']].append(d) #print(row) print( json.dumps(sched,indent=2)) for k,v in sched.items(): print("%s: %i" % (k,len(v))) for v in sched["Fall 2021"]: print("%s\t %s\t %s\t %s" % ( v['code'], v['days'], v['type'], v['loc'] )) #print("%s\t %s\t %s\t %s" % ( v['Course Code'], v['Meeting Days and Times'], v['Delivery'], v['Meeting Locations'] )) def parse_json_test_sched(): j2 = open('cache/classes_json.json','r').readlines() for L in j2: o3 = json.loads(L) print(json.dumps(o3,indent=2)) if __name__ == "__main__": print ('') options = { 1: ['fetch and parse the csv on www.', parse_www_csv_sched], 2: ['parse the test json file.', parse_json_test_sched ], } if len(sys.argv) > 1 and re.search(r'^\d+',sys.argv[1]): resp = int(sys.argv[1]) print("\n\nPerforming: %s\n\n" % options[resp][0]) else: print ('') for key in options: print(str(key) + '.\t' + options[key][0]) print('') resp = input('Choose: ') # Call the function in the options dict options[ int(resp)][1]()