-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
188 lines (161 loc) · 5.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from flask import Flask, render_template, session,request,url_for,redirect,jsonify
from flask_pymongo import PyMongo
from flask_socketio import SocketIO, join_room
from config import *
from random import randint
from bson.objectid import ObjectId
import requests
import os,shutil,os.path
from pdf2image import convert_from_path
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in {'pdf'}
#To run python file without re-running the flask command
def before_request():
app.jinja_env.cache = {}
app.before_request(before_request)
#Mongo setup
app.config["MONGO_URI"] = MONGO_URI
print(MONGO_URI)
mongo = PyMongo(app)
print(mongo)
room='999'
def save(email,name):
print(mongo.db)
auth=mongo.db.auth
pin=randint(100000, 999999)
myquery = { "email": email }
mydoc = auth.find(myquery)
for x in mydoc:
room=x['_id']
try:
x = room
myquery = { "_id": room }
newvalues = { "$set": { "pin": pin } }
auth.update_one(myquery, newvalues)
mongo.db.attendance.update_one(myquery,{"$set": { "attendance":[], "room":room }})
except:
auth.insert_one({"email": email,"pin":pin})
mydoc = auth.find(myquery)
for x in mydoc:
room=x['_id']
mongo.db.attendance.insert_one({"attendance":[], "room":room})
response = requests.get("https://talk9api.herokuapp.com/auth?email="+str(email)+"&room="+str(room)+"&name="+str(name)+"&pin="+str(pin))
return room
def check(room,pin):
auth=mongo.db.auth
myquery={"_id": ObjectId(room) }
mydoc = auth.find_one(myquery)
pin_real=mydoc['pin']
print(pin_real)
print(pin)
return str(pin)==str(pin_real)
@app.route('/', methods =["GET", "POST"])
def home():
if request.method == "POST":
name = request.form.get("name")
name=name.strip().replace(' ','-')
email = request.form.get("email")
email=email.strip()
print(mongo.db)
room=save(email,name)
filename='./static/slides/'+str(room)
try:
shutil.rmtree(filename)
except:
pass
os.mkdir(filename)
os.mkdir(filename+'/pdf')
os.mkdir(filename+'/img')
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
shutil.rmtree(filename+'/img')
shutil.copytree("./static/slides/demo", filename+'/img')
if file and allowed_file(file.filename):
file.save(filename+'/pdf/raw.pdf')
images = convert_from_path(filename+'/pdf/raw.pdf')
for i in range(len(images)):
images[i].save(filename+'/img/'+str(i)+'.jpg','JPEG')
return redirect(url_for('success'))
return render_template('home.html')
@app.route('/len', methods =["GET", "POST"])
def query():
room=request.args.get('room')
path='./static/slides/'+str(room)+'/img'
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
return jsonify({'n':num_files})
@app.route('/join', methods =["GET", "POST"])
def join():
global room
print(request.args)
if request.method == "POST" :
if request.form.get('room') is not None:
room=request.form.get('room')
name = request.form.get("name")
name=name.strip().replace(' ','-')
link='/console_panel?id=s&room='+room+'&name='+str(name)
myquery = { "room": ObjectId(room) }
mydoc = mongo.db.attendance.find_one(myquery)
old=mydoc['attendance']
if not name in old:
old.append(name)
mydoc['attendance']=old
mongo.db.attendance.update_one(myquery,{"$set": { "attendance":old }})
return redirect(link)
return render_template('join.html')
@app.route('/nAuth')
def nAuth():
return render_template('nAuth.html')
@app.route('/success')
def success():
return render_template('success.html')
@app.route('/console_panel')
def console():
i=request.args.get('id')
room=request.args.get('room')
if i == 't':
pin=request.args.get('pin')
if check(room,pin):
return render_template('t_console.html')
else:
return redirect(url_for('nAuth'))
if i == 's':
return render_template('s_console.html')
@socketio.on('joined')
def on_join(data):
username = data['name']
room = data['room']
join_room(room)
myquery = { "room": ObjectId(room) }
mydoc = mongo.db.attendance.find_one(myquery)
socketio.emit('new_message',username + ' has entered the room.', to=room)
if mydoc is not None:
socketio.emit('attendance_update',mydoc['attendance'],to=room)
print(mydoc, room)
@socketio.on('drawing')
def handle_my_custom_event(data):
room=data['room']
data=data['data']
socketio.emit('drawing', data, to=room)
@socketio.on('page')
def handle_my_custom_event(data):
room=data['room']
data=data['data']
socketio.emit('page', data, to=room)
@socketio.on("message")
def handleMessage(data):
room=data['room']
data=data['data']
socketio.emit("new_message", data, to=room)
@socketio.on('radio')
def radio(blob):
room=blob['room']
blob=blob['blob']
socketio.emit('voice', blob, to=room)
if __name__ == '__main__':
socketio.run(app, debug=True)