From 7da40e76ac94a8dcaa65b1d82bb3f7f87ff19eed Mon Sep 17 00:00:00 2001 From: siulkilulki Date: Wed, 16 May 2018 20:33:32 +0200 Subject: [PATCH] Fix find_hours regex. Fix app.py (adapt to addtion of utterances) change header of website in index.html get_utterances.py run with paramter intead of in script filename --- extractor/find_hours.py | 2 +- get_utterances.py | 3 ++- webapp/app.py | 29 ++++++++++++++++------------- webapp/templates/index.html | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/extractor/find_hours.py b/extractor/find_hours.py index b1dd27f..361e6d9 100755 --- a/extractor/find_hours.py +++ b/extractor/find_hours.py @@ -2,7 +2,7 @@ import re from colorama import Fore, Back, Style hour_regex = re.compile( - '(0[6-9]|1\d|2[0-2])[:.](oo|[0-5]\d)|6|7|8|9|1\d|2[0-2]') + '(0?[6-9]|1\d|2[0-2])[:.](oo|[0-5]\d)|6|7|8|9|1\d|2[0-2]') def borders_ok(text, start, end): diff --git a/get_utterances.py b/get_utterances.py index 5a4cb22..cb2fab4 100755 --- a/get_utterances.py +++ b/get_utterances.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import sys import urllib import logging import redis @@ -169,7 +170,7 @@ def main(): utterances = load_parishes('./parishwebsites/text-data', extracted_by_rules) print(len(utterances)) - with open('utterances.pkl', 'wb') as f: + with open(sys.argv[1], 'wb') as f: pickle.dump(utterances, f, pickle.HIGHEST_PROTOCOL) # r.set('foo', 'bar') # print(r.get('foo')) diff --git a/webapp/app.py b/webapp/app.py index 8d07e95..55b4374 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -25,6 +25,7 @@ utterances = load_utterances( '/home/siulkilulki/gitrepos/mass-scraper/utterances.pkl') UTT_SCORES = 'utterance-scores' +COOKIE_NAME = 'cookie-hash' # log(utterances[0:2]) #initialize_redis_db @@ -38,7 +39,9 @@ else: if status != 'filled': log('filling status') for i in range(len(utterances)): - r.zadd(UTT_SCORES, 0, str(i)) + if r.zscore(UTT_SCORES, str(i)) == None: + log(i) + r.zadd(UTT_SCORES, 0, str(i)) r.set('status', 'filled') status = 'filled' @@ -62,7 +65,7 @@ def get_next_response(cookie_hash): hour=hour, right_context=right_context) if cookie_hash: - resp.set_cookie('cookie-hash', cookie_hash, max_age=60 * 60 * 24 * 90) + resp.set_cookie(COOKIE_NAME, cookie_hash, max_age=60 * 60 * 24 * 90) return resp @@ -84,19 +87,18 @@ def get_response_by_index(index, cookie_hash): hour=hour, right_context=right_context) if cookie_hash: - resp.set_cookie('cookie-hash', cookie_hash, max_age=60 * 60 * 24 * 90) + resp.set_cookie(COOKIE_NAME, cookie_hash, max_age=60 * 60 * 24 * 90) return resp def annotate_redis(yesno, index): - cookie_hash = request.cookies.get('cookie-hash') + cookie_hash = request.cookies.get(COOKIE_NAME) + log('annotate: {}'.format(cookie_hash)) if not cookie_hash: return None annotation = r.get('{}:{}'.format( cookie_hash, index)) # previous annotation of utterance by that user if annotation: - log('!!!!!!!!!!!') - log(annotation) str_index = int(annotation.decode('utf-8').split(':')[1]) log(str_index) r.setrange(index, str_index, yesno) #sets str_index to yesno value @@ -114,30 +116,31 @@ def annotate_redis(yesno, index): def set_cookie(js_hash): # dodawać nowe js_hash do listy z key bedacym cookie_hash old_cookie_hash = None - cookie_hash = request.cookies.get('cookie-hash') + cookie_hash = request.cookies.get(COOKIE_NAME) + log(js_hash) + log('set cookie: {}'.format(cookie_hash)) if not cookie_hash: old_cookie_hash = r.get(js_hash) if not old_cookie_hash: cookie_hash = str( int.from_bytes(os.urandom(4), byteorder='little')) - r.set(js_hash, cookie_hash, nx=True) + r.set(js_hash, cookie_hash) else: - cookie_hash = str(old_cookie_hash) + cookie_hash = old_cookie_hash.decode('utf-8') log('old_cookie: {}, cookie: {}'.format(old_cookie_hash, cookie_hash)) return cookie_hash -def http_post(request): +def http_post(): index = str(request.form.get('index')) action = request.form['action'] js_hash = request.form['hash'] log(request.form) - log(f'action: {action}') if action == 'get': cookie_hash = set_cookie(js_hash) return get_next_response(cookie_hash) elif action == 'undo': - cookie_hash = request.cookies.get('cookie-hash') + cookie_hash = request.cookies.get(COOKIE_NAME) if cookie_hash: last_action = r.lpop(cookie_hash) if last_action: @@ -161,7 +164,7 @@ def http_get(): @app.route("/", methods=['GET', 'POST']) def root(): if request.method == 'POST': - return http_post(request) + return http_post() else: return http_get() diff --git a/webapp/templates/index.html b/webapp/templates/index.html index 0b577d6..af6dc17 100644 --- a/webapp/templates/index.html +++ b/webapp/templates/index.html @@ -19,7 +19,7 @@
-

Czy zaznaczono godzinę mszy świętej?

+

Czy zaznaczono godzinę rozpoczęcia mszy świętej?