/
graphoman_notes.py
55 lines (47 loc) · 1.8 KB
/
graphoman_notes.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
import re
import pymongo
from pymongo import MongoClient
import asyncio
import aiohttp_mako
from aiohttp import web
from pathlib import Path
client = MongoClient('185.188.183.228', 27017)
db = client['graphoman']
notesCollection = db['notes']
@aiohttp_mako.template('notes.html')
async def listMyNotes(request):
notes = notesCollection.find().sort('numOfWords', -1)
notesResult = ""
for note in notes:
notesResult += "<p>" + note['note'] + "</p><hr/>"
return {'notes': notesResult}
@aiohttp_mako.template('index.html')
async def addNoteRequestHandlerGET(request):
return {}
@aiohttp_mako.template('index.html')
async def addNoteRequestHandlerPOST(request):
post = await request.post()
note = post.get('note')
def get_number_of_unique_words_in_array(l):
ulist = []
[ulist.append(x) for x in l if x not in ulist and len(x) > 0]
return len(ulist)
numberOfWordsInNote = get_number_of_unique_words_in_array(
re.sub('[^A-Za-z ]+', '', note.lower()).split(' ')
)
notesCollection.insert_one({'note':note, 'numOfWords':numberOfWordsInNote})
return {}
async def init(loop):
app = web.Application(loop=loop)
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])
lookup.put_string('index.html', Path("index.html").read_text())
lookup.put_string('notes.html', Path("notes.html").read_text())
app.router.add_route('GET', '/', addNoteRequestHandlerGET)
app.router.add_route('POST', '/', addNoteRequestHandlerPOST)
app.router.add_route('GET', '/notes', listMyNotes)
return app
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init(loop))
web.run_app(app, host='185.188.183.228', port=9000)