def translate(): data = request.get("data") if not data: return show_reponse(code=Status.other, message="param error") q = data.get("q") result = BaiduT(q).run() return show_reponse(data={"list": result})
def start_crawl(): try: automation = Automation() art_status = automation.start() return show_reponse(code=Status.success, message=art_status.value) except: return show_reponse(code=Status.other)
def delete_news(): data = request.get("data") if not data: return show_reponse(code=Status.other, message="param error") article_id = data.get("id") sql = "DELETE FROM news WHERE id = %s" try: db_helper.execute_commit(sql, article_id) return show_reponse(code=Status.success) except Exception as e: return show_reponse(code=Status.other)
def application(environ, start_response): url = environ.get("PATH_INFO") method = environ.get("REQUEST_METHOD") qs = environ.get("QUERY_STRING") length = environ.get("CONTENT_LENGTH", "0") length = 0 if length == "" else int(length) data = environ["wsgi.input"].read(length) headers = {"method": method, "X-Token": environ.get("HTTP_X_TOKEN")} global request request["headers"] = headers request["qs"] = parse_qs(qs) if method.lower() == "post" and data: try: request["data"] = json.loads(data.decode()) except Exception as e: # xml,form request["data"] = data.decode() status = "200 OK" headers = [ ("Content-type", "application/json; charset=utf-8"), ("Access-Control-Allow-Headers", "*"), ("Access-Control-Allow-Origin", "*"), ("Access-Control-Max-Age", "600"), # cache preflight request 10min ] start_response(status, headers) try: data = ROUTE[url]() if type(data) is str: return [data.encode("utf-8")] return [json.dumps(data).encode("utf-8")] except Exception as e: err = show_reponse(code=Status.other, message=f"{e}") return [json.dumps(err).encode("utf-8")]
def graphql_entry(): data = request.get("data") query = data.get("query") context = data.get("context") variables = data.get("variables") operation_name = data.get("operation_name") result = schema.execute( query, context=context, variables=variables, operation_name=operation_name, ) if result.errors: (first_err, ) = result.errors logger.error(first_err) return show_reponse(code=Status.other, message=f"{first_err}") return show_reponse(data=result.data)
def login(): data = request.get("data") if not data: return show_reponse(code=Status.no_auth) username = data.get("username") password = data.get("password") query = "SELECT id FROM users WHERE username = %s and password = %s" user = db_helper.fetchone(query, (username, make_password(password, TOKEN_SALT))) if not user: return show_reponse(code=Status.no_auth) payload = { "id": str(head(user)), "username": username, "exp": time.time() + TOKEN_EXP, } token = jwt.encode(payload, TOKEN_SALT, algorithm="HS256") return show_reponse(data={"token": token, "username": username})
def get_news_by_id(): data = request.get("data") if not data: return show_reponse(code=Status.other, message="param error") article_id = data.get("id") sql = "SELECT title, source, image_url, transcript, date, audio_url FROM news WHERE id = %s" article = db_helper.fetchone(sql, article_id) if article: title, source, image_url, transcript, date, audio_url = article detail = dict( title=title, transcript=transcript, src=FILE_SERVER_URL + "/audio/" + audio_url, source=source, cover=FILE_SERVER_URL + "/image/" + image_url, date=date.strftime("%Y-%m-%d"), ) return show_reponse(data=detail) return show_reponse(code=Status.other, message="News is not exist!")
def get_wx_ticket(): qs = request.get("qs") url = "" if qs and qs.get("url"): url = head(qs.get("url")) else: raise ValueError("url params is required!") wx = WXSign() ret = wx.sign(url) return show_reponse(data=ret)
def send_dd_message(): data = request.get("data") err_code = Status.other err_message = "News is not exist!" if not data: return show_reponse(code=err_code, message="Param Error") article_id = data.get("id") sql = "SELECT date, title, image_url FROM news WHERE id = %s" article = db_helper.fetchone(sql, article_id) if article: date, title, image_url = article dd_bot = DDBot() template = DDBot.get_template( title=date.strftime("%d-%m-%Y"), content=title, pic_url=f"{FILE_SERVER_URL}/image/{image_url}", msg_url=f"{WEB_APP_URL}/detail/{article_id}", ) dd_info = dd_bot.send(template) code = dd_info.get("errcode") err_message = dd_info.get("errmsg") if code == 0: err_code = Status.success return show_reponse(code=err_code, message=err_message)
def get_news(): # date desc sql = "SELECT id, title, image_url, date FROM news ORDER BY date DESC" articles = db_helper.execute_sql(sql) data = [] for article in articles: id, title, image_url, date = article data.append( dict( id=id, title=title, cover=FILE_SERVER_URL + "/image/" + image_url, date=date.strftime("%Y-%m-%d"), ), ) return show_reponse(data={"list": data})
def get_news(): # date desc sql = ( "SELECT id, title, summary, transcript, audio_url, image_url, source, date FROM news " "ORDER BY date desc") articles = db_helper.execute_sql(sql) data = [] for article in articles: id, title, summary, transcript, audio_url, image_url, source, date = article data.append( dict( id=id, title=title, summary=summary, transcript=transcript, src=FILE_SERVER_URL + "/audio/" + audio_url, cover=FILE_SERVER_URL + "/image/" + image_url, source=source, date=date.strftime("%Y-%m-%d"), ), ) return show_reponse(data={"list": data})
def index(): return show_reponse(data="this is index page")