From 491fd2d25b1d016bc8c46afe67ce38bf3595a0b4 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Sat, 14 Jan 2017 16:02:28 +0100 Subject: [PATCH] adurl --- __init__.py | 4 ++-- bot/bot.py | 15 +++++++++------ compiler/controller.py | 8 +++++++- compiler/views.py | 7 +++---- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/__init__.py b/__init__.py index 5e93575..c0aec54 100644 --- a/__init__.py +++ b/__init__.py @@ -25,8 +25,8 @@ from .compiler import start_workers #start_workers(1,1,1) from src.bot import bot -if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true": - bot.message_loop() +#if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true": +bot.message_loop() diff --git a/bot/bot.py b/bot/bot.py index 33d505a..46eddf7 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -40,7 +40,7 @@ def query_test_url(url): print(json.dumps(url)) return {"text": url.url, "callback_data":"/urls/"+str(url.id)+"/test"} -def handle_urls(handler, cmd): +def handle_urls(handler, cmd,args=[]): curls=CrawlUrl.query.all() #sent=handler.sender.sendMessage(json.dumps(curls)) kb_que= IKM2(map(query_que_url,curls)) @@ -62,6 +62,8 @@ def handle_urls(handler, cmd): sent=handler.sender.sendMessage("Which url shoud I test?", reply_markup=kb_test) handler._edit_msg_ident = telepot.message_identifier(sent) handler._editor = telepot.helper.Editor(handler.bot, sent) + elif len(cmd) >= 3 and cmd[2] == "add": + sent=handler.sender.sendMessage("I added tested url: "+ json.dumps(compiler_controller.url_add(args[0],args[1]))[0:399], reply_markup=None) else: sent=handler.sender.sendMessage("What do you want to do?", reply_markup=kb_url) handler._edit_msg_ident = telepot.message_identifier(sent) @@ -70,12 +72,13 @@ def handle_urls(handler, cmd): def execute_command(handler,cmd,msg=None, args=[]): if cmd[1]=='urls': if is_admin(msg["from"]["id"]): - handle_urls(handler,cmd) + handle_urls(handler,cmd,args) else: handler.sender.sendMessage("Not allowed for "+json.dumps(msg["from"])) elif cmd[1] =='articles': handler.sender.sendMessage(json.dumps({"args": args, "cmd": cmd})) - handler.sender.sendMessage(json.dumps(articles_controller.search(args[0]))) + for a in articles_controller.search(args[0]): + handler.sender.sendMessage(json.dumps(a)) elif cmd[1] =='startworkers': if is_admin(msg["from"]["id"]): handler.sender.sendMessage(compiler_controller.start_workers()) @@ -87,9 +90,9 @@ def handle(handler,msg): content_type,chat_type,chat_id = telepot.glance(msg) if msg.has_key('text'): if msg['text'][0]=='/': - cmd = msg['text'].split("/") - args=cmd[-1].split(" ")[1:] - cmd[-1]=cmd[-1].split(" ")[0] + args=msg['text'].split(" ") + cmd = args[0].split("/") + args=args[1:] execute_command(handler, cmd, msg,args) if msg.has_key('data'): lg.debug(msg['data']) diff --git a/compiler/controller.py b/compiler/controller.py index 6d79718..dc7d25e 100644 --- a/compiler/controller.py +++ b/compiler/controller.py @@ -16,7 +16,13 @@ def urls_que(id): cu=CrawlUrl.query.get(id) mworker.queue_url(cu.tpe, cu.url) return cu - + +def url_add(url,tpe): + u=CrawlUrl.find_or_create(tpe, url) + db_session2.add(u) + db_session2.commit() + return u + def start_workers(): mworker.start_workers(1,1,1) return "started workers" diff --git a/compiler/views.py b/compiler/views.py index b67b728..16aab69 100644 --- a/compiler/views.py +++ b/compiler/views.py @@ -15,7 +15,8 @@ import mworker from compiler import do_compile from fetching import fetch_page -from .controller import urls_test, start_workers, urls_que +from .controller import urls_test, start_workers, urls_que, url_add + #flask.json.JSONEncoder.default = lambda self,obj: ((obj.__json__()) if isinstance(obj, (Article,CrawlUrl)) else None) @compiler_pages.route("/") @@ -125,9 +126,7 @@ def add_urls(): # Finde oder Erzeuge Url in der Datenbank url=CrawlUrlSchema().load(js["url"]) clogger.info(url) - url=CrawlUrl.find_or_create(url.data["tpe"], url.data["url"]) - db_session2.add(url) - db_session2.commit() + url=url_add(url.data["url"],url.data["tpe"]) return jsonify(url=url, kk=js) @compiler_pages.route("/urls/",methods=['DELETE'])