-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
167 lines (142 loc) · 6.38 KB
/
app.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
from flask import Flask, redirect, render_template, abort, Response, \
stream_with_context
from functools import wraps
import ConfigParser
import os
from pycaption import CaptionConverter, SRTReader, WebVTTWriter, \
CaptionReadNoCaptions
import requests
from sqlalchemy import create_engine, MetaData, Table, select
from sqlalchemy.sql import func
from courses import courses
import heatmaps
app = Flask(__name__)
app.debug = True
config = ConfigParser.RawConfigParser()
filedir = os.path.dirname(__file__)
config.read(os.path.join(filedir, 'moca.cfg'))
db_prefix = 'mysql://%s:%s@%s/' % (
config.get('Database', 'username'),
config.get('Database', 'password'),
config.get('Database', 'host'))
engines = {course_id: create_engine(db_prefix + course_id, echo=True, pool_recycle=3600) for course_id in courses}
metadata = {course_id: MetaData(engine) for course_id, engine in engines.iteritems()}
def validate_course(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'course_id' in kwargs and kwargs['course_id'] not in courses:
abort(404)
return f(*args, **kwargs)
return decorated_function
@app.route('/')
def route_home():
return render_template('animation.html')
@app.route('/home')
def animation():
return render_template('home.html')
@app.route('/<course_id>')
@validate_course
def route_course(course_id):
connection = engines[course_id].connect()
lecture_metadata = Table('lecture_metadata', metadata[course_id], autoload=True)
# TODO: Sort properly by x.y lecture number so 1.10 doesn't come before 1.2
ls = (select([lecture_metadata.c.id, lecture_metadata.c.title])
.where((lecture_metadata.c.parent_id == -1) &
(lecture_metadata.c.deleted == 0))
.order_by(lecture_metadata.c.title))
lectures_result = connection.execute(ls)
moca_topics = Table('moca_topics', metadata[course_id], autoload=True)
ts = select([moca_topics]).order_by(moca_topics.c.difficulty.desc())
topics_result = connection.execute(ts)
connection.close()
lectures = [{'id': lecture[0], 'title': lecture[1]} for lecture in lectures_result]
hardest_topics = [topic for topic in topics_result]
easiest_topics = ['easiest', 'easier', 'easy']
return render_template(
'course.html',
course_name=courses[course_id],
course_id=course_id,
hardest_topics=hardest_topics,
easiest_topics=easiest_topics,
lectures=lectures)
@app.route('/<course_id>/lecture/<int:lecture_id>')
@validate_course
def route_lecture(course_id, lecture_id):
connection = engines[course_id].connect()
lecture_metadata = Table('lecture_metadata', metadata[course_id], autoload=True)
ls = (select([lecture_metadata.c.id, lecture_metadata.c.title, lecture_metadata.c.source_video, lecture_metadata.c.video_id])
.where((lecture_metadata.c.id == lecture_id) &
(lecture_metadata.c.parent_id == -1) &
(lecture_metadata.c.deleted == 0))
.limit(1))
lecture_result = list(connection.execute(ls))
moca_topics = Table('moca_topics', metadata[course_id], autoload=True)
moca_topic_coverage = Table('moca_topic_coverage', metadata[course_id], autoload=True)
cs = (select([moca_topic_coverage, moca_topics])
.select_from(moca_topic_coverage.join(moca_topics))
.where(moca_topic_coverage.c.lecture_id == lecture_id)
.order_by(moca_topic_coverage.c.minute))
coverage_result = [dict(c) for c in connection.execute(cs)]
connection.close()
if len(lecture_result) == 0:
abort(404)
lecture = lecture_result[0]
lecture_data = {
'id': lecture['id'],
'title': lecture['title'],
'video_url_webm': 'http://d396qusza40orc.cloudfront.net/' + course_id + '/recoded_videos%2F' + lecture['source_video'].replace('.mp4', '.' + lecture['video_id'] + '.webm'),
'video_url_mp4': 'http://d396qusza40orc.cloudfront.net/' + course_id + '/recoded_videos%2F' + lecture['source_video'].replace('.mp4', '.' + lecture['video_id'] + '.mp4'),
'subtitles_url': '/%s/lecture/%d/subtitles' % (course_id, lecture_id),
}
return render_template(
'lecture.html',
course_id=course_id,
lecture=lecture_data,
heatmap=heatmaps.get_heatmap(course_id, lecture_id),
coverage=coverage_result)
@app.route('/<course_id>/lecture/<int:lecture_id>/subtitles')
@validate_course
def route_subtitles(course_id, lecture_id):
subtitles_url = (
'https://class.coursera.org/%s-001/lecture/subtitles?q=%d_en' %
(course_id, lecture_id))
r = requests.get(subtitles_url)
try:
converter = CaptionConverter()
converter.read(r.text, SRTReader())
subtitles = converter.write(WebVTTWriter())
except CaptionReadNoCaptions:
subtitles = ''
return Response(subtitles, content_type='text/vtt')
@app.route('/<course_id>/topic/<int:topic_id>')
@validate_course
def route_topic(course_id, topic_id):
connection = engines[course_id].connect()
moca_topics = Table('moca_topics', metadata[course_id], autoload=True)
ts = select([moca_topics]).where(moca_topics.c.id == topic_id)
topic_result = list(connection.execute(ts))
moca_topic_words = Table('moca_topic_words', metadata[course_id], autoload=True)
ws = (select([moca_topic_words])
.where(moca_topic_words.c.topic_id == topic_id)
.order_by(moca_topic_words.c.phi.desc()))
words_result = [dict(w) for w in connection.execute(ws)]
lecture_metadata = Table('lecture_metadata', metadata[course_id], autoload=True)
moca_topic_coverage = Table('moca_topic_coverage', metadata[course_id], autoload=True)
ls = (select([lecture_metadata.c.id, lecture_metadata.c.title, func.count().label('num_minutes')])
.select_from(lecture_metadata.join(moca_topic_coverage))
.where(moca_topic_coverage.c.topic_id == topic_id)
.group_by(lecture_metadata.c.id)
.order_by('num_minutes DESC'))
lectures_result = [dict(l) for l in connection.execute(ls)]
connection.close()
if len(topic_result) == 0:
abort(404)
topic = topic_result[0]
return render_template(
'topic.html',
course_id=course_id,
topic=topic,
words=words_result,
lectures=lectures_result)
if __name__ == '__main__':
app.run()