Esempio n. 1
0
def auto_post_img(msg, delay=90):
    user = db.session.query(User).filter(User.openid == msg.source).one()
    query = db.session.query(Message).filter(
        Message.author_id == user.id).order_by(Message.time_update.desc())
    random_token = tools.generate_token('10')
    executor.submit(tools.save_img, msg.image, 'msg_img_' + random_token)

    def empty_img_message(user, url, query):
        user.create_message(" ")
        message = query.first()
        message.add_images(url)

    try:
        message = query.first()
        time_gap = (
            tools.timestamp_2_time(tools.generate_timestamp()) -
            tools.timestamp_2_time(message.time_update)).total_seconds()
    except:
        time_gap = 9999
    if time_gap > delay:
        empty_img_message(user, 'msg_img_' + random_token, query)
        reply = create_reply('已发送图片动态', msg)  # todo: 其他功能
    else:
        if message.images.count() < 4:
            message.add_images('msg_img_' + random_token)
            reply = create_reply('动态更新', msg)  # todo: 其他功能
        else:
            empty_img_message(user, 'msg_img_' + random_token, query)
            reply = create_reply('已发送图片动态', msg)  # todo: 其他功能
    return reply
Esempio n. 2
0
def _handle_channel(find_channel, body):
    channel_list = []
    for i in find_channel:
        if i[-1] == '#':
            channel_list.append(i)
        else:
            channel_list.append(i[:-1])
    channel_list = list(set(channel_list))
    channel_list.sort(key=lambda x: len(x), reverse=True)
    channel_dict = {}
    for i in channel_list:
        channel_dict[i] = tools.generate_token('5')
        body = body.replace(i, channel_dict[i])
    for i in channel_dict.keys():
        href = '#' + i.replace('#', '')
        a_tag = '<a class="link" href="{}">{}</a>'.format(href, i)
        body = body.replace(channel_dict[i], a_tag)
    return body
Esempio n. 3
0
def _handle_user(find_user, body):
    user_list = []
    for i in find_user:
        user_list.append(i[:-1])
    user_list = list(set(user_list))
    user_list.sort(key=lambda x: len(x), reverse=True)
    user_dict = {}
    for i in user_list:
        user_dict[i] = tools.generate_token('5')
        body = body.replace(i, user_dict[i])
    for i in user_dict.keys():
        username = i.replace('@', '')
        try:
            user = db.session.query(User).filter(
                User.username == username).one()
            href = '@' + str(user.id)
            a_tag = '<a class="link" href="{}">{}</a>'.format(href, i)
        except:
            a_tag = i
        body = body.replace(user_dict[i], a_tag)
    return body
Esempio n. 4
0
def generate_token():
    token = Token(secret=tools.generate_token(5),
                  create_time=tools.generate_timestamp(),
                  status=0)
    token.save()
    return token.secret
Esempio n. 5
0
 def generate_login_key(self):
     token = Token(secret=tools.generate_token('30'))
     token.save()
     token.bind(self.id)
     return token