def weixin(): data = request.get("data") if data: return WXBot.receive(data) else: qs = request.get("qs") bot = WXBot() return bot.check_token(qs)
def wrapper(*args, **kargs): headers = request.get("headers") token = headers.get("X-Token") if not token: raise ValueError("No token param") if check_user_authed(token): return f(*args, **kargs)
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 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 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 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 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)