From 86f1728f5b5e85e928b5c7d9238d344ed9bf7886 Mon Sep 17 00:00:00 2001 From: mohamedamara1 Date: Thu, 5 Nov 2020 13:47:15 +0100 Subject: [PATCH] stable --- automating.py | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 automating.py diff --git a/automating.py b/automating.py new file mode 100644 index 0000000..ff750ce --- /dev/null +++ b/automating.py @@ -0,0 +1,177 @@ +from __future__ import print_function +import pickle +import os.path +from googleapiclient.discovery import build +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request +from googleapiclient.http import MediaIoBaseDownload +import io +import os +import os.path +from os import path +import pprint + +def main(): + + service = get_classroom_service() + courses = service.courses().list(pageSize=19).execute() + + downd_files=list() + + for course in courses['courses']: + + course_name = course['name'] + course_id = course['id'] + + if not (path.exists(course_name)): + os.mkdir('./' + course_name) + else: + print("{} Already exists ".format(course_name)) + + anoncs = service.courses().announcements().list( + courseId=course_id).execute() + work = service.courses().courseWork().list( + courseId=course_id).execute() + + downd_files = downd_files + download_annonc_files(anoncs, course_name) + downd_files = downd_files + download_works_files(work, course_name) + pprint.pprint(downd_files) + +def get_classroom_service(): + + SCOPES = [ + 'https://www.googleapis.com/auth/classroom.courses.readonly', + 'https://www.googleapis.com/auth/classroom.announcements.readonly', + 'https://www.googleapis.com/auth/classroom.student-submissions.me.readonly' + ] + """Shows basic usage of the Classroom API. + Prints the names of the first 10 courses the user has access to. + """ + creds = None + # The file token.pickle stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if os.path.exists('token.pickle'): + with open('token.pickle', 'rb') as token: + creds = pickle.load(token) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', SCOPES) + creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('token.pickle', 'wb') as token: + pickle.dump(creds, token) + + service = build('classroom', 'v1', credentials=creds) + return service + + +def download_file(file_id, file_name, course_name): + + SCOPES = ['https://www.googleapis.com/auth/drive'] + """Shows basic usage of the Drive v3 API. + Prints the names and ids of the first 10 files the user has access to. + """ + creds = None + # The file token.pickle stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if os.path.exists('token.pickledrive'): + with open('token.pickledrive', 'rb') as token: + creds = pickle.load(token) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credential-drive.json', SCOPES) + creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('token.pickledrive', 'wb') as token: + pickle.dump(creds, token) + + service = build('drive', 'v3', credentials=creds) + + request = service.files().get_media(fileId=file_id) + + fh = io.BytesIO() + downloader = MediaIoBaseDownload(fh, request) + done = False + + + while done is False: + status, done = downloader.next_chunk() + print("Download %d%%." % int(status.progress() * 100)) + + fh.seek(0) + + with open(os.path.join('./', course_name, file_name), 'wb') as f: + f.write(fh.read()) + f.close() + + + +def download_annonc_files(announcements, course_name): + annonc_list = announcements.keys() + downloaded = list() + if (len(annonc_list) != 0): + for announcement in announcements['announcements']: + try: #if this announcements contain a file then do this + for val in announcement['materials']: + file_id = val['driveFile']['driveFile']['id'] + file_name = val['driveFile']['driveFile']['title'] + extension = ( + os.path.splitext(file_name) + )[1] #the extension exists in second elemnts of returned tuple + path_str = os.path.join('./', course_name, file_name) + + if (valid(extension[1:]) and not (path.exists(path_str))): + print(file_name) + download_file(file_id, file_name, course_name) + downloaded.append("Annonoucemet : "+course_name +' : ' + file_name) + else: + print(file_name, "already exists") + except KeyError as e: + continue + return downloaded + +def download_works_files(works, course_name): + works_list = works.keys() + downloaded = list() + if (len(works_list) != 0): + for work in works['courseWork']: + try: #if this announcements contain a file then do this + for val in work['materials']: + file_id = val['driveFile']['driveFile']['id'] + file_name = val['driveFile']['driveFile']['title'] + + extension = ( + os.path.splitext(file_name) + )[1] #the extension exists in second elemnts of returned tuple + path_str = os.path.join('./', course_name, file_name) + if ((valid(extension[1:])) + and not (path.exists(path_str))): + print(file_name) + download_file(file_id, file_name, course_name) + downloaded.append("Devoir : "+course_name +' : ' + file_name) + else: + print(file_name, "already exists") + except KeyError as e: + continue + return downloaded + + +def valid(ch): + return ch in [ + 'pdf', 'docx', 'pptx', 'png', 'jpg', 'html', 'css', 'js', 'java', + 'class', 'txt', 'r', 'm', ' sql', 'doc', 'mp3' + ] + + +if __name__ == '__main__': + main()