Ejemplo n.º 1
0
def create_groups(argvs):
    """
    创建组,如果组已经存在,则增加组用户
    :param argvs:
    :return:
    """
    #获取文件
    yfile = argvs[argvs.index('-f')+1]
    d = format_yaml(yfile)
    #
    for k,v in d.items():
        #判断组是否已经存在
        g = session.query(Group).filter(Group.name == str(k)).first()
        #如果不存在,新建组
        if not g:
            insert_group = Group(name=str(k))
            session.add(insert_group)
            session.commit()
            g = session.query(Group).filter(Group.name == str(k)).first()
            print ('\033[1;34mSuccess\033[0m')
        else:
            for user in v['user']:
                u = session.query(User).filter(User.username == user).first()
                #如果程序账号存在,则建立用户组关系
                if u:
                    engine.execute(User2Group.insert(),{'user_id':u.id,'group_id':g.id})
                    print ('\033[1;34mSuccess\033[0m')
    session.close()
Ejemplo n.º 2
0
def import_lib(name, url, title_tag, body_tag):
    newlib = Urllib(website_name=name,
                    website_url=url,
                    website_title_tag=title_tag,
                    website_body_tag=body_tag)
    session.add(newlib)
    session.commit()
Ejemplo n.º 3
0
    async def add(self, ctx, *channels: str):
        """
        Add channel follow
        """
        user_id = ctx.message.author.id
        username = ctx.message.author.name

        for channel in channels:
            follows = check_follows_exist(user_id, channel)
            if follows:
                await embed_message(
                    ctx,
                    message_type='Error',
                    message=f'Channel {channel} is already saved in database'
                )
            else:
                channel_id = await retrieve_twitch_channel_id(ctx, channel)
                if channel_id:
                    try:
                        new_follow = Follows(
                            user_id=user_id, username=username, channel=channel, channel_id=channel_id)
                        session.add(new_follow)
                        session.commit()
                        await embed_message(
                            ctx,
                            message_type='Success',
                            message=f'Channel {channel} has been saved to database'
                        )
                    except exc.OperationalError:
                        session.rollback()
                        await embed_message(
                            ctx,
                            message_type='Error',
                            message=f'Cannot save channel {channel} to database'
                        )
Ejemplo n.º 4
0
    async def remove_all(self, ctx):
        """
        Remove all channel follows
        """
        user_id = ctx.message.author.id

        follows = check_follows_exist(user_id, channel=None)

        if follows:
            try:
                for follow in follows:
                    session.delete(follow)
                    session.commit()
                await embed_message(
                    ctx,
                    message_type='Success',
                    message=f'Removed {len(follows)} channel follows from database'
                )
            except exc.OperationalError:
                session.rollback()
                await embed_message(
                    ctx,
                    message_type='Error',
                    message='Cannot remove channel follows in database'
                )
        else:
            return await embed_message(
                ctx,
                message_type='Error',
                message='No channels saved in database'
            )
Ejemplo n.º 5
0
    async def remove(self, ctx, *channels: str):
        """
        Remove channel follows
        """
        user_id = ctx.message.author.id

        for channel in channels:
            follows = check_follows_exist(user_id, channel)
            if follows:
                try:
                    for follow in follows:
                        session.delete(follow)
                        session.commit()
                        await embed_message(
                            ctx,
                            message_type='Success',
                            message=f'Channel {channel} has been removed from database'
                        )
                except exc.OperationalError:
                    session.rollback()
                    await embed_message(
                        ctx,
                        message_type='Error',
                        message=f'Cannot remove channel {channel} in database'
                    )
            else:
                await embed_message(
                    ctx,
                    message_type='Error',
                    message=f'Channel {channel} is not saved in database'
                )
Ejemplo n.º 6
0
    async def _import(self, ctx, twitch_username: str):
        """
        Import channel follows from twitch username
        """
        user_id = ctx.message.author.id
        username = ctx.message.author.name
        twitch_user_id = await retrieve_twitch_channel_id(ctx, twitch_username)

        if twitch_user_id:
            data = await twitch_api_call(ctx, endpoint='users/follows?from_id=', channel=twitch_user_id, params='')
            if data:
                objects = []
                for follow in data['data']:
                    channel_id = follow['to_id']
                    channel_name = await retrieve_twitch_channel_name(ctx, channel_id)
                    follows = check_follows_exist(user_id, channel_name)
                    if follows:
                        pass
                    else:
                        objects.append(
                            Follows(
                                user_id=user_id,
                                username=username,
                                channel=channel_name,
                                channel_id=channel_id
                            )
                        )
                if len(objects) == 0:
                    await embed_message(
                        ctx,
                        message_type='Error',
                        message='Channel follows already saved in database'
                    )
                else:
                    try:
                        session.add_all(objects)
                        session.commit()
                        await embed_message(
                            ctx,
                            message_type='Success',
                            message=f'Imported {len(objects)} channel follows to database'
                        )
                    except exc.OperationalError:
                        session.rollback()
                        await embed_message(
                            ctx,
                            message_type='Error',
                            message='Cannot save channel follows to database'
                        )
Ejemplo n.º 7
0
def create_hostusers(argvs):
    """
    创建主机账号,并与主机进行关联
    :param argvs:
    :return:
    """
    #获取文件
    yfile = argvs[argvs.index('-f')+1]
    d = format_yaml(yfile)
    for k,v in d.items():
        #print(k,v)
        for host in v['ip_addr']:
            h = session.query(Host).filter(Host.ip_addr == host).first()
            if h:
                #判断是否有账号已经在主机上
                unique = session.query(HostUser).filter(and_(HostUser.host_id==h.id,\
                                                             HostUser.username==v['username'])).first()
                #如果在指定主机上没有,则新建
                if not unique:
                    insert_hostuser = HostUser(host_id=h.id,
                                               auth_type='ssh-passwd',
                                               username=str(v['username']),
                                               password=str(v['password']))
                    #print (h.id,v['username'],v['password'])
                    session.add(insert_hostuser)
                    session.commit()
                    print ('\033[1;34mSuccess\033[0m')
                #如果存在重复账号,则更新密码
                else:
                    print ('\033[1;33mUsername [%s] at [%s] has exists.Update password\33[0m'%(v['username'],
                                                                                h.ip_addr))
                    session.query(HostUser).filter(and_(HostUser.host_id==h.id,\
                                                             HostUser.username==v['username'])
                                                   ).update({'password':str(v['password'])})
                    print ('\033[1;34mSuccess\033[0m')
            else:
                print ('\033[1;33mHost [%s] is not exists.\33[0m'%host)
        session.close()
Ejemplo n.º 8
0
def create_hosts(argvs):
    """
    创建主机
    :param argvs:
    :return:
    """
    #获取文件
    yfile = argvs[argvs.index('-f')+1]
    d = format_yaml(yfile)
    #
    for k,v in d.items():
        #判断主机是否存在
        h = session.query(Host).filter(Host.hostname == str(k)).first()
        #如果不存在,新建主机信息
        if not h:
            insert_host = Host(hostname=k,ip_addr=v['ip_addr'],port=v['port'])
            session.add(insert_host)
            session.commit()
            print ('\033[1;34mSuccess\033[0m')
        #如果主机名已经存在,打印报错
        else:
            print ('\033[1;33mHostname [%s] has exists.\33[0m'%v['ip_addr'])
    session.close()
Ejemplo n.º 9
0
def create_users(argvs):
    """
    创建新用户
    :param argvs:
    :return:
    """
    #获取文件
    yfile = argvs[argvs.index('-f')+1]
    d = format_yaml(yfile)
    #循环创建新用户,如果用户已经存在,更新密码
    for k,v in d.items():
        u = session.query(User).filter(User.username == str(k)).first()
        if u:
            print ('---\033[1;33muser [%s] has exists,Update password.\33[0m---'%str(k))
            #更新密码
            session.query(User).filter(User.username == str(k)).update({'password':str(v['password'])})
        else:
            insert_user = User(username=str(k),password=str(v['password']))
            #新建用户
            session.add(insert_user)
            session.commit()
            print ('\033[1;34mSuccess\033[0m')
    session.close()
Ejemplo n.º 10
0
def insert_post(title, body, original_link):
    newpost = Post(title=title, content=body, original_link=original_link)
    session.add(newpost)
    session.commit()
Ejemplo n.º 11
0
def delete_all_post():
    selected = session.query(Post).all()
    for i in selected:
        session.delete(i)
    session.commit()
Ejemplo n.º 12
0
import sys
sys.path.append('/home/vagrant/personal/new_simi')

import requests

from core.models import session
from core.models.subjects import Location

COUNT = 100
loc_list_url = 'https://api.douban.com/v2/loc/list'

for start in range(0, 909, 100):
    play_load = {'count': COUNT, 'start': start}
    response = requests.get(loc_list_url, params=play_load)
    for loc in response.json()['locs']:
        location = Location()
        location.parent_uid = loc['parent']
        location.name = loc['name']
        location.loc_id = int(loc['id'])
        location.loc_uid = loc['uid']
        session.add(location)
        session.commit()