def make_request(query, modifiers): resultsarr = [] query = append_modifiers(query, modifiers) # build query url u = URL + "key=" + API_KEY + "&cx=" + ENGINE_ID + "&q=" + query # make request r = requests.get(u, PARAMS) # pull json from request res = r.json() # pull "items" from results results = res["items"] if "items" in res else None if results is not None: for item in results: title = item["title"] if "title" in item else "No Title" link = item["link"] if "link" in item else "No Link Found" dlink = item[ "displayLink"] if "displayLink" in item else "No Display Link" snip = item["snippet"] if "snippet" in item else "No Snippet" mime = item["mime"] if "mime" in item else None ff = item[ "fileFormat"] if "fileFormat" in item else "No File Format" rjson = DigiJsonBuilder.create_search_result( title, link, dlink, snip, mime, ff) resultsarr.append(rjson) return resultsarr
def get_course(user_auth, user_email, course_id, ser=None): service = create_service( user_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) if ser == None else ser course = service.courses().get(id=course_id).execute() coursejson = DigiJsonBuilder.create_course(course['name'], course_id, [user_email], announcements=[], coursework=[]) otherfromgoogle = { 'section': course['section'] if 'section' in course else None, 'room': course['room'] if 'room' in course else None, 'courseState': course['courseState'] if 'courseState' else None, 'teacherGroupEmail': course['teacherGroupEmail'] if 'teacherGroupEmail' in course else None, 'courseDescription': course['descriptionHeading'] if 'descriptionHeading' in course else None } coursejson.update(otherfromgoogle) mats = _get_materials_from_json(course['courseMaterialSets'] if 'courseMaterialSets' in course else None) coursejson.update({'materialSets': mats}) return coursejson
def get_course_list(user_auth, user_email): courselist = [] service = create_service(user_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) res = service.courses().list().execute() #print(res.toString()) if 'courses' in res: for course in res['courses']: cw = get_coursework_list(user_auth, user_email, course['id'], ser=service) annou = get_announcements(user_auth, user_email, course['id'], ser=service) coursejson = DigiJsonBuilder.create_course(course['name'], course['id'], [user_email], announcements=annou, coursework=cw) courselist.append(coursejson) return courselist
def submit_queries(queries, modifiers): resultsArr = [] for query in queries: r = make_request(query, modifiers) results = DigiJsonBuilder.create_results(query, False, len(r), r) resultsArr.append(results) return resultsArr
def _get_materials_from_json(mat): if mat is not None: cm = [] if 'materials' in mat: df = mat['driveFile'] if "driveFile" in mat else None yt = mat['youtubeVideo'] if "youtubeVideo" in mat else None lnk = mat['link'] if "link" in mat else None form = mat['form'] if "form" in mat else None # at this point we arent storing these files material = DigiJsonBuilder.create_material(df, yt, lnk, form, None) cm.append(material) return cm # this will be similar to get materials loop return None
def get_announcements(user_auth, user_email, course_id, ser=None): service = create_service(user_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) if ser == None else ser gannoulist = service.courses().announcements().list(courseId=course_id, announcementStates='PUBLISHED').execute() anlist = [] mode = None if 'announcements' in gannoulist: for gan in gannoulist["announcements"]: anid = gan['id'] antext = gan['text'] if 'text' in gan else None anmats = _get_materials_from_json(gan) if 'materials' in gan else None anctime = gan['creationTime'] if 'creationTime' in gan else None anstu = [user_email] anou = DigiJsonBuilder.create_announcement(anid, antext, anmats, anctime, mode, anstu) anlist.append(anou) return anlist
def get_coursework(user_auth, user_email, course_id, coursework_id, ser=None): service = create_service(user_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) if ser == None else ser cw = service.courses().courseWork().get(courseId=course_id, id=coursework_id).execute() cwid = cw['id'] title = cw['title'] desc = cw['description'] if 'description' in cw else None mats = get_materials(user_auth, user_email, course_id, cwid, service) ctime = cw['creationTime'] ddate = cw['dueDate'] if 'dueDate' in cw else None dtime = cw['dueTime'] if 'dueTime' in cw else None wtype = cw['workType'] if 'workType' in cw else None dets = cw['multipleChoiceQuestion'] if 'multipleChoiceQuestion' in cw else None pts = cw['maxPoints'] if 'maxPoints' in cw else None cwjson = DigiJsonBuilder.create_coursework(cwid, title, desc, mats, ctime, ddate, dtime, wtype, None, [user_email], dets) if pts != None: cwjson.update({"maxPoints": pts}) return cwjson
def get_drive_list(user_auth, user_email): # do auth and create service gdrive_auth = get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # get list of drive objects (this is like, mydrive, shared drives, etc. no files) # with all list getters, there is a list_next(prev_req, prev_resp) that can be used, gonna need a while loop # for those in the future but for now well just do one page of results gdrive_list = service.drives().list() mydrive = get_file_list(user_auth, user_email, 'my-drive') drivelist = [mydrive] for drive in gdrive_list['drives']: driveobj = DigiJsonBuilder.create_drive( drive['id'], get_file_list(user_auth, drive['id']), drive['capabilities']) driveobj = _get_drive_permissions(driveobj, drive) drivelist.append(driveobj) # convert to digijson return drivelist
def get_file_list(user_auth, user_email, driveid): filelist = [] # get list of files in the specified drive gdrive_auth = user_auth # get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # see get_drive_list if driveid == 'my-drive': gdrive_list = service.files().list(fields="files(" + _FILE_FIELDS + ")").execute() else: gdrive_list = service.files().list(fields="files(" + _FILE_FIELDS + ")", driveId=driveid, supportsAllDrives=True, includeItemsFromAllDrives=True, corpora='drive').execute() filemeta = {} for file in gdrive_list['files']: if not file['trashed']: try: filemeta = DigiJsonBuilder.create_file( name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=file['size']) except KeyError as k: print(k) try: filemeta.update({'mimeType': file['mimeType']}) filemeta.update({'capabilities': file['capabilities']}) except KeyError as k: print(k) filelist.append(filemeta) ''' for file in gdrive_list['files']: if not file['trashed']: try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=file['size']) except KeyError as k: if k == 'parents': try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=None, classpath=None, size=file['size']) except KeyError as k1: if k1 == 'size': filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=None, classpath=None, size=None) if k == 'size': try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=None) except KeyError as k1: # throw an error return k1, k else: return k try: filemeta.update({'mimeType': file['mimeType']}) filemeta.update({'capabilities': file['capabilities']}) except KeyError as k: if k == 'mimeType': try: filemeta.update({'capabilities': file['capabilities']}) except KeyError as k1: # throw an error? or just log? return None if k == 'capabilities': filemeta.update({'capabilities': None}) # should probably do some logging here... filelist.append(filemeta) ''' # convert to digijson return filelist