Exemple #1
0
def weixin():
    data = request.get("data")
    if data:
        return WXBot.receive(data)
    else:
        qs = request.get("qs")
        bot = WXBot()
        return bot.check_token(qs)
Exemple #2
0
 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)
Exemple #3
0
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})
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
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})
Exemple #8
0
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!")
Exemple #9
0
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)