forked from kippnorcal/google_classroom
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
174 lines (147 loc) · 5.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from datetime import datetime, timedelta
import logging
import os
import pickle
import sys
import traceback
from googleapiclient.discovery import build
from google.oauth2 import service_account
import pandas as pd
from api import (
Announcements,
Courses,
CourseWork,
CourseAliases,
GuardianInvites,
Guardians,
Invitations,
OrgUnits,
Students,
StudentSubmissions,
StudentUsage,
Teachers,
Topics,
)
from config import Config, db_generator
from mailer import Mailer
def configure_logging(config):
logging.basicConfig(
handlers=[
logging.FileHandler(filename="data/app.log", mode="w+"),
logging.StreamHandler(sys.stdout),
],
level=logging.DEBUG if config.DEBUG else logging.INFO,
format="%(asctime)s | %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %I:%M:%S%p %Z",
)
logging.getLogger("google_auth_oauthlib").setLevel(logging.ERROR)
logging.getLogger("googleapiclient").setLevel(logging.ERROR)
logging.getLogger("google").setLevel(logging.ERROR)
logging.getLogger("pybigquery").setLevel(logging.ERROR)
logging.getLogger("pandas_gbq").setLevel(logging.ERROR)
def get_credentials(config):
"""Generate service account credentials object"""
SCOPES = [
"https://www.googleapis.com/auth/classroom.announcements",
"https://www.googleapis.com/auth/admin.directory.orgunit",
"https://www.googleapis.com/auth/admin.reports.usage.readonly",
"https://www.googleapis.com/auth/classroom.courses",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.guardianlinks.students",
"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.rosters",
"https://www.googleapis.com/auth/classroom.student-submissions.students.readonly",
"https://www.googleapis.com/auth/classroom.topics",
]
return service_account.Credentials.from_service_account_file(
"service.json", scopes=SCOPES, subject=config.ACCOUNT_EMAIL
)
def main(config):
configure_logging(config)
creds = get_credentials(config)
classroom_service = build("classroom", "v1", credentials=creds)
admin_reports_service = build("admin", "reports_v1", credentials=creds)
admin_directory_service = build("admin", "directory_v1", credentials=creds)
sql = db_generator(config)
# Get usage
if config.PULL_USAGE:
# First get student org unit
orgUnits = OrgUnits(admin_directory_service, sql, config)
orgUnits.batch_pull_data()
result = orgUnits.return_all_data()
org_unit_id = None if result.empty else result.iloc[0].loc["orgUnitId"]
# Then get usage, loading data from after the last available day.
usage = StudentUsage(admin_reports_service, sql, config, org_unit_id)
last_date = usage.get_last_date()
if last_date:
start_date = last_date + timedelta(days=1)
else:
start_date = datetime.strptime(config.SCHOOL_YEAR_START, "%Y-%m-%d")
date_range = pd.date_range(start=start_date, end=datetime.today()).strftime(
"%Y-%m-%d"
)
usage.batch_pull_data(dates=date_range, overwrite=False)
# Get guardians
if config.PULL_GUARDIANS:
Guardians(classroom_service, sql, config).batch_pull_data()
# Get guardian invites
if config.PULL_GUARDIAN_INVITES:
GuardianInvites(classroom_service, sql, config).batch_pull_data()
# Get courses
if config.PULL_COURSES:
Courses(classroom_service, sql, config).batch_pull_data()
# Get list of course ids
if (
config.PULL_TOPICS
or config.PULL_COURSEWORK
or config.PULL_STUDENTS
or config.PULL_TEACHERS
or config.PULL_SUBMISSIONS
or config.PULL_ALIASES
or config.PULL_INVITATIONS
or config.PULL_ANNOUNCEMENTS
):
courses = Courses(classroom_service, sql, config).return_all_data()
course_ids = courses.id.unique()
# Get course aliases
if config.PULL_ALIASES:
CourseAliases(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course invitations
if config.PULL_INVITATIONS:
Invitations(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course announcements
if config.PULL_ANNOUNCEMENTS:
Announcements(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course aliases
if config.PULL_ALIASES:
CourseAliases(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course invitations
if config.PULL_INVITATIONS:
Invitations(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course announcements
if config.PULL_ANNOUNCEMENTS:
Announcements(classroom_service, sql, config).batch_pull_data(course_ids)
# Get course topics
if config.PULL_TOPICS:
Topics(classroom_service, sql, config).batch_pull_data(course_ids)
# Get CourseWork
if config.PULL_COURSEWORK:
CourseWork(classroom_service, sql, config).batch_pull_data(course_ids)
# Get students and insert into database
if config.PULL_STUDENTS:
Students(classroom_service, sql, config).batch_pull_data(course_ids)
# Get teachers and insert into database
if config.PULL_TEACHERS:
Teachers(classroom_service, sql, config).batch_pull_data(course_ids)
# Get student coursework submissions
if config.PULL_SUBMISSIONS:
StudentSubmissions(classroom_service, sql, config).batch_pull_data(course_ids)
if __name__ == "__main__":
try:
main(Config)
error_message = None
except Exception as e:
logging.exception(e)
error_message = traceback.format_exc()
if not Config.DISABLE_MAILER:
Mailer(Config, "Google Classroom Connector").notify(error_message=error_message)