-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRS_Sync.py
145 lines (124 loc) · 4.95 KB
/
LRS_Sync.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
import uuid, json, sys, time
from tincan import RemoteLRS
from socket import gethostbyname, gaierror
import requests
#Statement classclass Statement:
class Statement:
def __init__(self,statement):
self.statement = statement
self.id = statement['id']
if 'name' in statement['actor']:
self.actor = statement['actor']['name']
else:
self.actor = 'Unknown User'
self.verb = statement['verb']['id']
self.object = statement['object']['definition']['name']['en-US']
#Collect LRS data from config file
def collect_local_lrs_credentials():
try:
from LRS_config import local_lrs_credentials as lc
except ImportError:
print ("Error: LRS_config.py does not exist or cannot be read")
sys.exit()
local_lrs = RemoteLRS(
endpoint=lc["endpoint"],
username=lc["username"],
password=lc["password"],
)
return local_lrs, lc
def collect_remote_lrs_credentials():
try:
from LRS_config import remote_lrs_credentials as rc
except ImportError:
print ("Error: LRS_config.py does not exist or cannot be read")
sys.exit()
remote_lrs = RemoteLRS(
endpoint=rc["endpoint"],
username=rc["username"],
password=rc["password"],
)
return remote_lrs, rc
#Collect all statements currently in remote LRS to avoid duplicates
def collect_remote_statements(remote_lrs, rc):
print ("Checking statements in remote LRS...")
if rc["LRS"] == "learninglocker":
try:
response = remote_lrs.query_statements({"format":"exact"})
except gaierror:
print ("Error connecting to remote LRS at %s. Please check your internet connection." %(rc[endpoint]))
sys.exit()
if not response.success:
print ("\nCould not connect to remote LRS at %s.\nPlease check config file for correct details and try again...\nExiting..." %(rc["endpoint"]))
sys.exit()
remote_data = json.loads(response.data)
elif rc["LRS"] == "wordpress":
url = rc["endpoint"] + "/statements/"
un = rc["username"]
pw = rc["password"]
try:
r = requests.get(url, auth = (un,pw))
r.raise_for_status()
remote_data = r.json()
except requests.exceptions.RequestException as e:
print (e)
sys.exit()
remote_statement_ids = ()
for statement in remote_data["statements"]:
remote_statement_ids += (statement["id"],)
return remote_statement_ids
def collect_local_statements(local_lrs,lc):
print ("Connecting to local LRS...")
if lc["LRS"] == "learninglocker":
querySettings = {"format":"exact"}
try:
from LRS_config import date
except ImportError:
date = None
if date is not None:
querySettings["since"] = date + "T00:00:00.000000+00:00"
response = local_lrs.query_statements(querySettings)
if not response.success:
print ("\nCould not connect to local LRS at %s.\nPlease check config file for correct details and try again...\nExiting..." %(lc["endpoint"]))
sys.exit()
return json.loads(response.data)
elif lc["LRS"] == "wordpress":
url = lc["endpoint"] + "/statements/"
un = lc["username"]
pw = lc["password"]
try:
r = requests.get(url, auth = (un,pw))
r.raise_for_status()
if len(r.json()["statements"]) <= 1:
print ("Could not find any statements in local LRS at %s.\nPlease check config file for correct details and try again...\nExiting..." %(lc["endpoint"]))
sys.exit()
return r.json()
except requests.exceptions.RequestException as e:
print (e)
sys.exit()
else:
print (lc["LRS"] + " is not a valid LRS type.")
sys.exit()
#return response.data
def store_statements(local_statements, remote_statement_ids, remote_lrs):
saved_statements = 0
skipped_statements = 0
for statement in local_statements["statements"]:
s = Statement(statement)
print (s.actor, s.verb, s.object)
if statement["id"] in remote_statement_ids:
print ("skip")
skipped_statements += 1
else:
response = remote_lrs.save_statement(statement)
if not response:
raise ValueError("statements failed to save")
print ("...saved")
saved_statements += 1
time.sleep(.2)
print ("\nDone! %i statements saved and %i statements skipped. \n" %(saved_statements, skipped_statements))
if __name__ == "__main__":
local_lrs, lc = collect_local_lrs_credentials()
remote_lrs, rc = collect_remote_lrs_credentials()
remote_statement_ids = collect_remote_statements(remote_lrs, rc)
local_statements = collect_local_statements(local_lrs, lc)
store_statements(local_statements, remote_statement_ids, remote_lrs)