forked from adityadutta/MHacks12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
202 lines (165 loc) · 7.16 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import pprint
from twilio.rest import Client
from twilio.twiml.messaging_response import MessagingResponse
from firebase_admin import credentials
import firebase_admin
from database import DatabaseManager, Note
from flask import Flask, render_template, request, flash, redirect
import requests
import getsearch
from forms import AddNoteForm, SuperNoteForm
import os
import translation
from Google_Key_Test import NoteAnalysis
credential_path = "TranslationKey.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'
lang = "en"
app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'
if (not len(firebase_admin._apps)):
cred = credentials.Certificate(
'static/mhacks12-22906-firebase-adminsdk-lbt7e-d7c9a27bb5.json')
default_app = firebase_admin.initialize_app(
cred, {'databaseURL': 'https://mhacks12-22906.firebaseio.com/'})
previous = ""
account_sid = '[contact [email protected] for twilio SID]'
auth_token = '[contact [email protected] for twilio private API key]'
dm = DatabaseManager("super_notes")
notesManager = DatabaseManager("notes")
sn_translated = "Oops! Something's wrong with Twilio servers!"
@app.route('/')
def main():
return render_template('index.html')
@app.route("/index")
def index():
data = get_supernote("Physics")
user = "Bill"
return render_template('index.html', data=data, user=user)
#####TWILIO######
@app.route("/sms", methods=['GET', 'POST'])
def sms_ahoy_reply():
global lang
global sn_translated
courses = ["physics", "computer science"] # WHERE THE COURSES ARE STORED
client = Client(account_sid, auth_token)
resp = MessagingResponse()
premes = ""
messages = client.messages.list(limit=1)
for record in messages:
premes = record.body
print(".1.1.1.1.1.1.1")
print(premes)
print(".1.1.1.1.1.1.1")
if(translation.createTranslation(premes, "English").lower() in courses):
super_notes_list = dm.find_notes_by_course_name(
translation.createTranslation(premes, language="en").lower())
print("----------")
print(lang)
print("----------")
for note in super_notes_list:
sn_translated = translation.createTranslation(note['note'], lang)
print(note['note'])
sn_translated = sn_translated.replace(''', '\'')
resp.message(sn_translated[:1550] + ".")
return str(resp)
lang = str(premes)
resp.message(translation.createTranslation(
"Enter a course you wish to learn about.", language=lang))
return str(resp)
def get_supernote(course):
cl = dm.find_notes_by_course_name(course)
return cl
@app.route("/results")
def results():
return render_template('results.html')
@app.route('/search', methods=['POST'])
def pass_val():
search = request.form.get('search')
language = 'en' if request.form.get('language-choice') is " " or request.form.get('language-choice') is None else request.form.get('language-choice')
print(language)
print(search + language)
s = translation.createTranslation(search, "EN")
s = s.replace(''', '\'')
print(s)
key = dm.get_last_super_note_key(s)
print(key)
super_note = dm.get_note_key(dm.get_last_super_note_key(s)) #dm.find_notes_by_course_name(s)
pprint.pprint(super_note)
sn_translated = translation.createTranslation(super_note['note'], language)
sn_translated = sn_translated.replace(''', '\'')
title_translated = translation.createTranslation(
super_note['course_name'], language)
title_translated = title_translated.replace(''', '\'')
pprint.pprint(sn_translated)
# sn_translated = sn_translated.replace('\n', '<br/><br/> - ')
print(sn_translated)
return render_template('results.html', note=sn_translated, title=title_translated, upvotes=super_note["upvotes"], key = key, note_type = "super_note")
@app.route('/add_note')
def add_note():
form = AddNoteForm()
return render_template('add_note.html', title='Add New Note', form=form)
@app.route('/add_note', methods=['POST'])
def add_new_note():
form = AddNoteForm()
if form.validate_on_submit():
course_name = form.course_name.data
n_translated = translation.createTranslation(form.note.data, "EN")
n_translated = n_translated.replace(''', '\'')
new_note = Note(form.course_key.data,
course_name.lower(), n_translated)
note_key = notesManager.add_note_to_db(new_note)
flash('Note Added: With course key: {} and Course Name: {} . Share your notes with your friends or save it for future use: {}'.format(
form.course_key.data, form.course_name.data, note_key))
auto_gen_super_note(course_name.lower())
return redirect('/index')
return render_template('add_note.html', title='Add New Note', form=form)
@app.route('/super_note', methods=['POST'])
def generate_super_note():
form = SuperNoteForm()
sn = ""
if form.validate_on_submit():
na = NoteAnalysis(form.key1.data, form.key2.data)
sn = na.run_quickstart()
return render_template('results.html', note=sn)
@app.route('/super_note')
def super_note():
form = SuperNoteForm()
return render_template('super_note.html', title='Generate Super Note', form=form)
@app.route('/search_key')
def search_key_page():
return render_template('search_key.html')
@app.route('/search_key', methods=['POST'])
def search_key():
key = request.form.get('key')
language = 'en' if request.form.get('language-choice') is " " or request.form.get('language-choice') is None else request.form.get('language-choice')
print(key + language)
note_data = notesManager.get_note_key(key)
pprint.pprint(note_data)
sn_translated = translation.createTranslation(note_data["note"], language)
pprint.pprint(sn_translated)
return render_template('results.html', note=sn_translated, upvotes=note_data["upvotes"], key = key, note_type = "note")
def auto_gen_super_note(course_name):
last_super_note_key = dm.get_last_super_note_key(course_name)
last_course_note_key = notesManager.get_last_course_note_key(course_name)
na = NoteAnalysis(last_super_note_key, last_course_note_key)
sn = na.run_quickstart()
@app.route("/upvote/<key>/<note_type>")
def upvote(key, note_type):
if note_type == "super_note":
up = 0 if dm.get_note_key(key)["upvotes"] is None else dm.get_note_key(key)["upvotes"]
dm.update_data(key, "upvotes", up + 1)
else:
up = 0 if notesManager.get_note_key(key)["upvotes"] is None else notesManager.get_note_key(key)["upvotes"]
notesManager.update_data(key, "upvotes", up + 1)
return redirect('/index')
@app.route("/downvote/<key>/<note_type>")
def downvote(key, note_type):
if note_type == "super_note":
up = 0 if dm.get_note_key(key)["upvotes"] is None else dm.get_note_key(key)["upvotes"]
dm.update_data(key, "upvotes", up - 1)
else:
up = 0 if notesManager.get_note_key(key)["upvotes"] is None else notesManager.get_note_key(key)["upvotes"]
notesManager.update_data(key, "upvotes", up - 1)
return redirect('/index')