-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
53 lines (36 loc) · 1.31 KB
/
server.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
import copy
import urllib
from bottle import *
from pymongo import Connection
from dom43 import parser
Flats = None
@route('/static/<filename>')
def static_handler(filename):
return static_file(filename, root='static')
@route('/scripts/<path_tail:path>')
def scripts_handle(path_tail):
url = 'http://www.dom43.ru/estate_base/scripts/' + path_tail + '?' + request.query_string
print(url)
response.content_type = 'text/plain'
return urllib.request.urlopen(url)
@route('/search')
def search_handler():
entries = parser.parse_page(request.query_string)
global Flats
numbers_to_indices = { entry['number']: index for index, entry in enumerate(entries) }
selection = Flats.find({'number': {'$in': list(numbers_to_indices.keys())}}, fields=['number'])
for i in selection:
entries[numbers_to_indices[i['number']]]['old'] = True
new_entries = [ e for e in entries if not e.get('old') ]
if new_entries:
Flats.insert(copy.deepcopy(new_entries))
next_offset = int(request.query.get('start', 0)) + len(entries)
return { 'entries': entries, 'next_offset': next_offset }
@route('/')
def main_handler():
return static_file('main.html', root='templates')
with Connection('localhost', safe=True) as conn:
db = conn.estate
Flats = db.flats
Flats.ensure_index('number', unique=True)
run(host='localhost', port=80, debug=True)