-
Notifications
You must be signed in to change notification settings - Fork 1
/
ks_content_linking.py
executable file
·223 lines (187 loc) · 6.42 KB
/
ks_content_linking.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
import argparse
from sys import exit
import json
import psycopg2
from utils import eprint
# Figure out how to connect to PSQL from
# http://www.postgresqltutorial.com/postgresql-python/connect/
def read_cmd():
"""Reading command line options."""
desc = "Program for linking CS-Khan content for EMA reputation system."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-p','--password', dest = 'password', required = False, help = 'Postgres password')
parser.add_argument('-s','--schema', dest='schema', default = None, help = 'Link given schema.')
parser.add_argument('-a','--all', dest = 'all', action = 'store_true', help = 'Print all available schemas')
# TODO: Add verbose parameter
return parser.parse_args()
# Currently, article type does not seem to work.
CONTENT_TYPES = ['video']
SCHEMA_TITLE_ID_MAP = {
'organicka-chemie': 24,
'fyzikalni-chemie': 20,
'obecna-chemie': 2
}
EMA_OPTIONAL_DATA = {
# TODO: What about subtitles videos?
'jazyk': '5-cs',
'autor': 'Khanova škola',
'dostupnost': '7-ANO', # OER
'typ': {
'video': '8-VI',
'exercise': '8-IC',
'article': '8-CL'
},
'licence': {
'cc-by-nc-nd': '1-CCBYNCND30',
'cc-by-nc-sa': '1-CCBYNCSA30',
'cc-by-sa': '1-CCBYSA30',
'yt-standard': '1-OST'
},
# TODO: Update these for KS schemas
'stupen_vzdelavani': {
'basic-geo': '2-Z',
'algebra-basics': '2-Z',
'organicka-chemie': '2-G',
'fyzikalni-chemie': '2-G'
},
'vzdelavaci_obor': {
'math': '9-03',
'chem': '9-08'
},
'rocnik': {
'early-math': '3-Z13',
'organicka-chemie': '3-SS',
},
'gramotnost': {
'math': '4-MA',
'music': '4-NU',
'chem': '4-PR',
'astro': '4-PR'
}
}
def ema_print_schema_content(schema, content):
ema_content = []
unique_content_ids = set()
content_type = 'video'
schema_subject_map = {
'organicka-chemie': 'chem',
'obecna_chemie': 'chem',
'fyzikalni_chemie': 'chem',
'rychlokurz_chemie': 'chem'
}
subject = schema_subject_map[schema]
for v in content:
if v['id'] in unique_content_ids:
eprint("Found in previous schemas, skipping: ")
continue
else:
unique_content_ids.add(v['id'])
try:
item = {
# Key items
'id': v['id'],
'url': v['url'],
'nazev': v['title'],
'popis': v['description'],
# Fixed items
'autor': EMA_OPTIONAL_DATA['autor'],
'jazyk': EMA_OPTIONAL_DATA['jazyk'],
'dostupnost': EMA_OPTIONAL_DATA['dostupnost'],
'licence': EMA_OPTIONAL_DATA['licence']['cc-by-nc-sa'],
# Optional fields
'typ': EMA_OPTIONAL_DATA['typ'][content_type],
# TODO: Uncomment this in final version
'stupen_vzdelavani': EMA_OPTIONAL_DATA['stupen_vzdelavani'][schema],
'vzdelavaci_obor': EMA_OPTIONAL_DATA['vzdelavaci_obor'][subject],
'gramotnost': EMA_OPTIONAL_DATA['gramotnost'][subject],
}
ema_content.append(item)
except:
eprint('Key error!')
eprint(v)
raise
with open('ks_%s_%s.json' % (schema.replace('-', '_').lower(), content_type), 'w', encoding = 'utf-8') as out:
out.write(json.dumps(ema_content, ensure_ascii=False))
print("Number of EMA %s in %s = %d" % (content_type, schema, len(ema_content)))
def connect_ks(psswd):
db_name = 'khanovaskola'
conn = psycopg2.connect(host="localhost", database = db_name, user="postgres", password = psswd)
cur = conn.cursor()
print('PostgreSQL database version:')
cur.execute('SELECT version()')
db_version = cur.fetchone()
print(db_version)
cur.close()
return conn
def print_schemas(connection):
cur = connection.cursor()
schema_id = 2
sql = "SELECT id, title FROM schemas ORDER BY id"
cur.execute(sql)
print("Number of schemas is %s" % (cur.rowcount))
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
def get_schema_content(connection, schema_id):
if type(schema_id) is not int or schema_id < 1:
print("ERROR: Invalid schema id")
print(schema_id)
sys.exit(1)
cur = connection.cursor()
# TODO: Check which JOINS to use to avoid duplicates
sql = """SELECT sch.id, bl.id, ct.id, ct.title, ct.description
FROM "contents" AS ct
JOIN content_block_bridges AS cbb ON ct.id = cbb.content_id
JOIN blocks AS bl ON bl.id = cbb.block_id
JOIN block_schema_bridges AS bsb ON bsb.block_id = bl.id
JOIN schemas AS sch ON sch.id = bsb.schema_id
WHERE ct.type = 'video' AND ct.hidden = 'f' AND sch.id = %d
ORDER BY ct.id""" % (schema_id)
cur.execute(sql)
if cur.rowcount < 1:
print("ERROR: 0 rows in schema id %d" % (schema_id))
sys.exit(1)
else:
print("Found %d videos in schema id %d" % (cur.rowcount, schema_id))
rows = cur.fetchall()
cur.close()
return rows
if __name__ == '__main__':
opts = read_cmd()
psswd = opts.password
schema_title = opts.schema
if opts.schema is None:
eprint("Please specify schema via -s cmd parameter")
exit(1)
if schema_title not in SCHEMA_TITLE_ID_MAP.keys():
eprint('Invalid schema %s' % schema_title)
exit(1)
schema_id = SCHEMA_TITLE_ID_MAP[schema_title]
chem_schemas = ['organicka-chemie', 'fyzikalni-chemie', 'obecna-chemie']
bio_schemas = []
fyz_schemas = []
all_schemas = chem_schemas + bio_schemas + fyz_schemas
with open("psql.psswd", "r") as f:
psswd = f.read()
psswd = psswd[:-1]
conn = connect_ks(psswd)
#print_schemas(conn)
rows = get_schema_content(conn, schema_id)
videos = []
for row in rows:
# TODO: Make prettier URL, include full schema/block path
url = 'https://khanovaskola.cz/video/%s/%s/%s' % (row[0], row[1], row[2])
video = {
'id': row[2],
'title': row[3],
'description': row[4],
'url': url
}
videos.append(video)
print(row)
ema_print_schema_content(schema_title, videos)
if conn is not None:
conn.close()
print('Database connection closed.')