예제 #1
0
class ModelSetting(db.Model):
    __tablename__ = 'plugin_%s_setting' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), unique=True, nullable=False)
    value = db.Column(db.String, nullable=False)

    def __init__(self, key, value):
        self.key = key
        self.value = value

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        return {x.name: getattr(self, x.name) for x in self.__table__.columns}
예제 #2
0
class ModelScheduler2(db.Model):
    __tablename__ = '%s_scheduler2' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    site_id = db.Column(db.Integer, db.ForeignKey('%s_site.id' % package_name))
    sitename = db.Column(db.String)
    board_id = db.Column(db.String)
    include_scheduler = db.Column(db.Boolean)
    use_proxy = db.Column(db.Boolean)
    use_torrent_info = db.Column(db.Boolean)

    group_id = db.Column(db.Integer,
                         db.ForeignKey('%s_group.id' % package_name))
    bbs = db.relationship('ModelBbs2', backref='scheduler', lazy=True)

    def __init__(self, site_instance):
        self.created_time = datetime.now()
        self.site_id = site_instance.id
        self.sitename = site_instance.name

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        return ret

    @staticmethod
    def get_list(by_dict=False):
        try:
            tmp = db.session.query(ModelScheduler2).all()
            if by_dict:
                tmp = [x.as_dict() for x in tmp]
            return tmp
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #3
0
class ModelItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    def __init__(self):
        self.created_time = datetime.now()

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%Y-%m-%d %H:%M:%S')

        return ret

    @staticmethod
    def get_last_time():
        query = db.session.query(ModelItem)
        query = query.order_by(desc(ModelItem.created_time))
        query = query.limit(1).offset(0)
        lists = query.all()
        count = int(query.count())

        if count is 1:
            result = lists[0].as_dict()
            created_time = result['created_time']
            return created_time

        return 0

    @staticmethod
    def get(id):
        try:
            entity = db.session.query(ModelItem).filter_by(
                id=id).with_for_update().first()
            return entity
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #4
0
파일: model.py 프로젝트: taeminlee/wavve
class ModelSetting(db.Model):
    __tablename__ = 'plugin_%s_setting' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), unique=True, nullable=False)
    value = db.Column(db.String, nullable=False)

    def __init__(self, key, value):
        self.key = key
        self.value = value

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        return {x.name: getattr(self, x.name) for x in self.__table__.columns}

    @staticmethod
    def get(key):
        try:
            return db.session.query(ModelSetting).filter_by(
                key=key).first().value.strip()
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def set(key, value):
        try:
            item = db.session.query(ModelSetting).filter_by(
                key=key).with_for_update().first()
            if item is not None:
                item.value = value.strip()
                db.session.commit()
            else:
                db.session.add(ModelSetting(key, value.strip()))
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())
예제 #5
0
class ModelGroup2(db.Model):
    __tablename__ = '%s_group' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    groupname = db.Column(db.String())
    #schedulers = db.relationship('ModelScheduler2', backref='plugin_%s_scheduler2' % package_name, lazy=True)
    schedulers = db.relationship('ModelScheduler2',
                                 backref='groups',
                                 lazy=True)
    query = db.Column(db.String())

    #schedulers = db.relationship('ModelScheduler2', secondary=group_scheduler2, backref=db.backref('ModelGroup2'))

    def __init__(self):
        self.created_time = datetime.now()

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['schedulers'] = []
        for f in self.schedulers:
            ret['schedulers'].append(f.as_dict())
        return ret

    @staticmethod
    def get_list(by_dict=False):
        try:
            tmp = db.session.query(ModelGroup2).all()
            if by_dict:
                tmp = [x.as_dict() for x in tmp]
            return tmp
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #6
0
class ModelChannel(db.Model):
    __tablename__ = '%s_channel' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    source = db.Column(db.String)
    source_id = db.Column(db.String)
    title = db.Column(db.String)
    icon = db.Column(db.String)
    is_tv = db.Column(db.Boolean)
    current = db.Column(db.String)

    is_include_custom = db.Column(db.Boolean)

    def __init__(self, source, source_id, title, icon, is_tv):
        self.source = source
        self.source_id = source_id
        self.title = title
        self.icon = icon
        self.is_tv = is_tv
        self.is_include_custom = False
        #self.summary = None

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if ret['created_time'] is not None else None
        if self.json is not None:
            ret['json'] = json.loads(ret['json'])
        else:
            ret['json'] = {}
        return ret
예제 #7
0
파일: model.py 프로젝트: wnd2da/offcloud2
class ModelOffcloud2Account(db.Model):
    __tablename__ = '%s_account' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    data = db.Column(db.JSON)

    username = db.Column(db.String)
    remote_type = db.Column(db.String)
    account_id = db.Column(db.String)
    option_id = db.Column(db.String)
    path = db.Column(db.String)

    def __init__(self, data):
        self.created_time = datetime.datetime.now()
        self.username = data['username']
        self.remote_type = data['type']
        self.account_id = data['accountId']
        self.option_id = data['remoteOptionId']
        self.path = data['path']

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        return ret
    
    @staticmethod
    def get_list(by_dict=False):
        try:
            tmp = db.session.query(ModelOffcloud2Account).all()
            if by_dict:
                tmp = [x.as_dict() for x in tmp]
            return tmp
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #8
0
파일: model.py 프로젝트: soju6jan/SJVA3
class ModelRcloneServe(db.Model):
    __tablename__ = '%s_serve' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    json = db.Column(db.JSON)
    name = db.Column(db.String)
    command = db.Column(db.String)
    remote = db.Column(db.String)
    remote_path = db.Column(db.String)
    port = db.Column(db.Integer)
    option = db.Column(db.String)
    auto_start = db.Column(db.Boolean)

    def __init__(self):
        self.current_status = False
        self.created_time = datetime.now()

    def __repr__(self):
        return str(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['command_ui'] = ModelRcloneServe.commands[ret['command']]
        return ret

    commands = {
        'webdav': 'WebDav',
        'ftp': 'FTP',
        'dlna': 'DLNA',
        'sftp': 'SFTP',
        'http': 'HTTP'
    }
예제 #9
0
파일: model.py 프로젝트: wnd2da/manamoa
class ModelManamoaItem(db.Model):
    __tablename__ = 'plugin_%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    manga_id = db.Column(db.String)
    wr_id = db.Column(db.String)
    title = db.Column(db.String)
    main_title = db.Column(db.String)
    total_image_count = db.Column(db.Integer)

    def __init__(self):
        self.created_time = datetime.now()

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        return ret

    @staticmethod
    def save(entity):
        m = ModelManamoaItem()
        m.manga_id = entity.manga_id
        m.wr_id = entity.wr_id
        m.title = entity.title
        m.total_image_count = entity.total_image_count
        m.main_title = entity.maintitle
        db.session.add(m)
        db.session.commit()

    @staticmethod
    def get(wr_id):
        try:
            return db.session.query(ModelManamoaItem).filter_by(
                wr_id=wr_id).first()
        except Exception as e:
            logger.error('Exception:%s %s', e, wr_id)
            logger.error(traceback.format_exc())
예제 #10
0
class ModelGDriveScanFile(db.Model):
    __tablename__ = '%s_file' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    gdrive_name = db.Column(db.String)
    name = db.Column(db.String)
    section_id = db.Column(db.Integer)
    is_file = db.Column(db.Boolean)
    is_add = db.Column(db.Boolean)
    created_time = db.Column(db.DateTime)
    scan_time = db.Column(db.DateTime)

    def __init__(self, gdrive_name, name, section_id, is_file, is_add):
        self.gdrive_name = gdrive_name
        self.name = name
        self.section_id = section_id
        self.is_file = is_file
        self.is_add = is_add
        self.created_time = datetime.now()

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if self.created_time is not None else ''
        ret['scan_time'] = self.scan_time.strftime(
            '%m-%d %H:%M:%S') if self.scan_time is not None else ''
        return ret

    @staticmethod
    def add(gdrive_name, name, section_id, is_file, is_add):
        item = ModelGDriveScanFile(gdrive_name, name, section_id, is_file,
                                   is_add)
        db.session.add(item)
        db.session.commit()
        return item.id
예제 #11
0
class ModelKtvLibrary(db.Model):
    __tablename__ = '%s_library' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    library_type = db.Column(db.Integer)
    library_path = db.Column(db.String)
    rclone_path = db.Column(db.String)
    replace_for_plex_source = db.Column(db.String)
    replace_for_plex_target = db.Column(db.String)
    index = db.Column(db.Integer)

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        return {x.name: getattr(self, x.name) for x in self.__table__.columns}
예제 #12
0
class ModelSubItem(db.Model):
    __tablename__ = '%s_sub_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    # basic info
    name = db.Column(db.String)
    entity_id = db.Column(db.Integer)
    sub_type = db.Column(db.String)
    agent_type = db.Column(db.String)
    target_file_id = db.Column(db.String)
    shortcut_file_id = db.Column(db.String)
    parent_folder_id = db.Column(db.String)

    # for plex
    plex_path = db.Column(db.String)
    plex_section_id = db.Column(db.String)
    plex_metadata_id = db.Column(db.String)

    def __init__(self, name, entity_id, agent_type, sub_type):
        self.created_time = datetime.now()
        self.name = py_unicode(name)
        self.entity_id = entity_id
        self.sub_type = sub_type
        self.agent_type = py_unicode(agent_type)

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S') 
        return ret
    
    def save(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def delete(cls, id):
        db.session.query(cls).filter_by(id=id).delete()
        db.session.commit()

    @classmethod
    def get_by_id(cls, id):
        return db.session.query(cls).filter_by(id=id).first()
    
    @classmethod
    def get_by_target_file_id(cls, target_file_id):
        return db.session.query(cls).filter_by(target_file_id=target_file_id).first()

    @classmethod
    def get_by_plex_path(cls, plex_path):
        return db.session.query(cls).filter_by(plex_path=plex_path).first()

    @classmethod
    def get_by_shortcut_file_id(cls, shortcut_file_id):
        return db.session.query(cls).filter_by(shortcut_file_id=shortcut_file_id).first()

    @classmethod
    def get_entities_by_entity_id(cls, entity_id):
        return db.session.query(cls).filter_by(entity_id=entity_id).all()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def get_item_count(cls, entity_id):
        return db.session.query(cls).filter_by(entity_id=entity_id).count()

    @classmethod
    def get_all_entities_group_by_entity_id(cls):
        return db.session.query(cls).group_by(cls.entity_id).order_by(func.count(cls.id).desc()).all()

    @classmethod
    def web_list(cls, module_name, req):
        try:
            ret = {}
            page = 1
            page_size = ModelSetting.get_int('item_per_page')
            job_id = ''
            search = ''
            category = ''
            genre = 'all'
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            if 'genre' in req.form:
                genre = req.form['genre']
            rule_name = req.form['category'] if 'category' in req.form else 'all'
            status_option = req.form['status_option'] if 'status_option' in req.form else 'all'
            query = cls.make_query(module_name=module_name, genre=genre, search=search, rule_name=rule_name, status_option=status_option)
            count = query.count()
            query = query.limit(page_size).offset((page-1)*page_size)
            logger.debug('cls count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @classmethod
    def make_query(cls, module_name='', genre='all', search='', rule_name='all', status_option='all', order='desc'):
        query = db.session.query(cls)
        if module_name != '':
            if module_name == 'movie': query = query.filter(cls.agent_type == 'movie')
            else: query = query.filter(cls.agent_type.like('%tv'))
        if genre != 'all': query = query.filter(cls.genre == genre)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(cls.title.like('%'+tt.strip()+'%') )
                        conditions.append(cls.name.like('%'+tt.strip()+'%') )
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(cls.title.like('%'+tt.strip()+'%') )
                        conditions.append(cls.name.like('%'+tt.strip()+'%') )
                        query = query.filter(or_(*conditions))
            else:
                query = query.filter(or_(cls.title.like('%'+search+'%'), cls.name.like('%'+search+'%')))

        if rule_name != 'all':
            query = query.filter(cls.rule_name == rule_name)

        if status_option == 'excluded':
            query = query.filter(cls.excluded == True)
        else:
            query = query.filter(cls.excluded == False)
            if status_option != 'all':
                if status_option == 'true': query = query.filter(cls.shortcut_created == True)
                elif status_option == 'onair': query = query.filter(cls.status == 1)
                elif status_option == 'ended': query = query.filter(cls.status == 2)
                else: query = query.filter(cls.shortcut_created == False)

        if order == 'desc':
            query = query.order_by(desc(cls.updated_time))
            #query = query.order_by(desc(cls.id))
        else:
            query = query.order_by(desc(cls.updated_time))
            #query = query.order_by(cls.id)

        return query 
예제 #13
0
파일: model.py 프로젝트: lanian/hdhomerun
class ModelHDHomerunChannel(db.Model):
    __tablename__ = 'plugin_%s_hdhomerun_channel' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    ch_type = db.Column(db.String)  # hdhomerun, custom

    # scan 정보
    scan_vid = db.Column(db.String)
    scan_name = db.Column(db.String)
    scan_frequency = db.Column(db.String)
    scan_program = db.Column(db.String)
    scan_ch = db.Column(db.String)

    # m3u & epg
    for_epg_name = db.Column(db.String)
    group_name = db.Column(db.String)

    use_vid = db.Column(db.Boolean)
    use = db.Column(db.Boolean)
    ch_number = db.Column(db.Integer)
    # custom url
    url = db.Column(db.String)

    match_epg_name = db.Column(db.String)

    def __init__(self):
        # for ui
        # match_epg_name
        self.match_epg_name = ''
        self.created_time = datetime.now()
        self.ch_number = 0
        self.group_name = ''

    def init_data(self, data):
        self.ch_type = 'hdhomerun'
        self.use_vid = False
        tmp = data.split('|')
        self.scan_vid = tmp[0].strip()
        #self.ch_number = tmp[0]
        self.scan_name = tmp[1].strip()
        self.for_epg_name = tmp[1].strip()
        self.scan_frequency = tmp[2].strip()
        self.scan_program = tmp[3].strip()
        self.scan_ch = tmp[4].strip()
        self.scan_modulation = tmp[5].strip()
        self.use = True
        if self.scan_vid == '0' or self.scan_name == '':
            self.use = False

        tmp = ['encrypted', 'no data', '데이터 방송', 'control']
        for t in tmp:
            if self.scan_name.find(t) != -1:
                self.use = False
                break
        self.match_epg()

    def set_url(self, deviceid):
        if self.use_vid:
            self.url = 'http://%s:5004/auto/v%s' % (deviceid, self.scan_vid)
        else:
            self.url = 'http://%s:5004/auto/ch%s-%s' % (
                deviceid, self.scan_frequency, self.scan_program)

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if ret['created_time'] is not None else None
        if self.json is not None:
            ret['json'] = json.loads(ret['json'])
        else:
            ret['json'] = {}
        return ret

    def match_epg(self):
        try:
            import epg
            ret = epg.ModelEpgMakerChannel.get_match_name(self.for_epg_name)
            if ret is not None:
                self.match_epg_name = ret[0]
                self.group_name = ret[1]
                return True
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return False
예제 #14
0
class ModelAvItem(db.Model):
    __tablename__ = '%s_av_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    # basic info
    rule_name = db.Column(db.String)   # from category_rules
    rule_id = db.Column(db.Integer)
    agent_type = db.Column(db.String) # ktv, ftv, movie, av?
    root_folder_id = db.Column(db.String)
    target_folder_id = db.Column(db.String)
    orig_gdrive_path = db.Column(db.String)

    shortcut_created = db.Column(db.Boolean)
    shortcut_count = db.Column(db.Integer)
    shortcut_folder_id = db.Column(db.String)
    shortcut_name = db.Column(db.String)
    gdrive_path = db.Column(db.String)
    local_path = db.Column(db.String)
    plex_path = db.Column(db.String)

    # info from gdrive
    name = db.Column(db.String)
    mime_type = db.Column(db.String)
    folder_id = db.Column(db.String)
    parent_folder_id = db.Column(db.String)

    # info from metadata
    code = db.Column(db.String)
    ui_code = db.Column(db.String)
    label = db.Column(db.String)
    title = db.Column(db.String)
    genre = db.Column(db.String)
    actor = db.Column(db.String)
    site = db.Column(db.String)
    studio = db.Column(db.String)
    country = db.Column(db.String)
    poster_url = db.Column(db.String)
    fanart_url = db.Column(db.String)
    trailer_url = db.Column(db.String)
    year = db.Column(db.Integer)
    plot = db.Column(db.String)
    runtime = db.Column(db.Integer)

    # for plex
    plex_section_id = db.Column(db.String)
    plex_metadata_id = db.Column(db.String)

    excluded = db.Column(db.Boolean)

    def __init__(self, ui_code, folder_id):
        self.created_time = datetime.now()
        self.ui_code = py_unicode(ui_code)
        self.folder_id = py_unicode(folder_id)
        self.shortcut_created = False
        self.shortcut_count = 0
        self.excluded = False

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S') 
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def delete(cls, id):
        db.session.query(cls).filter_by(id=id).delete()
        db.session.commit()

    @classmethod
    def delete_items_by_rule_id(cls, rule_id):
        db.session.query(cls).filter_by(rule_id=rule_id).delete()
        db.session.commit()

    @classmethod
    def get_by_id(cls, id):
        return db.session.query(cls).filter_by(id=id).first()

    @classmethod
    def get_by_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(folder_id=folder_id).first()
    
    @classmethod
    def get_by_name(cls, name):
        return db.session.query(cls).filter_by(name=name).first()

    @classmethod
    def get_by_code(cls, code):
        return db.session.query(cls).filter_by(code=code).first()

    @classmethod
    def get_by_ui_code(cls, ui_code):
        return db.session.query(cls).filter_by(ui_code=ui_code).first()

    @classmethod
    def get_scheduled_entities(cls):
        return db.session.query(cls).filter_by(use_schedule=True).all()
    
    @classmethod
    def get_by_root_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(root_folder_id=folder_id).first()

    @classmethod
    def get_by_target_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(target_folder_id=folder_id).first()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def get_entities_by_rule_id(cls, rule_id):
        return db.session.query(cls).filter_by(rule_id=rule_id).all()

    @classmethod
    def get_item_count(cls, rule_id):
        return db.session.query(cls).filter_by(rule_id=rule_id).count()

    @classmethod
    def get_shortcut_count(cls, rule_id):
        return db.session.query(cls).filter(and_(cls.rule_id==rule_id, cls.shortcut_created==True)).count()

    @classmethod
    def get_shortcut_created_entities(cls, rule_id):
        return db.session.query(cls).filter(and_(cls.rule_id==rule_id, cls.shortcut_created==True)).all()

    @classmethod
    def get_all_entities_group_by_parent(cls, rule_id):
        query = db.session.query(cls)
        return query.filter(cls.rule_id==rule_id).group_by(cls.parent_folder_id).all()

    @classmethod
    def update_all_rows_by_rule_id(cls, rule_id, col_values):
        query = db.session.query(cls)
        query = query.filter(cls.rule_id==rule_id)
        query.update(col_values)
        db.session.commit()

    @classmethod
    def web_list(cls, req):
        try:
            ret = {}
            page = 1
            page_size = ModelSetting.get_int('item_per_page')
            job_id = ''
            search = ''
            category = ''
            order = ''
            if 'page' in req.form: page = int(req.form['page'])
            if 'search_word' in req.form: search = req.form['search_word']
            if 'order' in req.form: order = req.form['order']
            rule_name = req.form['category'] if 'category' in req.form else 'all'
            status_option = req.form['status_option'] if 'status_option' in req.form else 'all'
            query = cls.make_query(search=search, rule_name=rule_name, status_option=status_option, order=order)
            count = query.count()
            query = query.limit(page_size).offset((page-1)*page_size)
            logger.debug('cls count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
    
    @classmethod
    def make_query(cls, search='', rule_name='all', status_option='all', order='desc'):
        query = db.session.query(cls)
        #if agent_type != 'all': query = query.filter(cls.agent_type == agent_type)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(cls.title.like('%'+tt.strip()+'%') )
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query = query.filter(cls.title.like('%'+tt.strip()+'%'))
            else:
                query = query.filter(or_(cls.title.like('%'+search+'%'), cls.name.like('%'+search+'%'), cls.actor.like('%'+search+'%')))

        if rule_name != 'all':
            query = query.filter(cls.rule_name == rule_name)

        if status_option == 'excluded':
            query = query.filter(cls.excluded == True)
        elif status_option == 'duplicated':
            subquery = db.session.query(cls.ui_code).group_by(cls.ui_code).having(func.count(cls.ui_code) > 1).subquery()
            query = query.filter(cls.ui_code.in_(subquery))
        else:
            query = query.filter(cls.excluded == False)
            if status_option != 'all':
                if status_option == 'true': query = query.filter(cls.shortcut_created == True)
                else: query = query.filter(cls.shortcut_created == False)

        if order == 'id_desc': query = query.order_by(desc(cls.id))
        elif order == 'id_asc': query = query.order_by(cls.id)
        elif order == 'code_desc': query = query.order_by(desc(cls.ui_code))
        else: query = query.order_by(cls.ui_code)

        return query 
예제 #15
0
class ModelSubFolderItem(db.Model):
    __tablename__ = '%s_subfolder_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    name = db.Column(db.String)
    rule_id = db.Column(db.Integer)
    folder_id = db.Column(db.String)
    parent_folder_id = db.Column(db.String)

    def __init__(self, name, rule_id, folder_id, parent_folder_id):
        self.created_time = datetime.now()

        self.name = py_unicode(name)
        self.rule_id = rule_id
        self.folder_id = py_unicode(folder_id)
        self.parent_folder_id = py_unicode(parent_folder_id)

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S') 
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def delete(cls, id):
        db.session.query(cls).filter_by(id=id).delete()
        db.session.commit()

    @classmethod
    def get_by_id(cls, id):
        return db.session.query(cls).filter_by(id=id).first()
    
    @classmethod
    def get_by_name(cls, name):
        return db.session.query(cls).filter_by(name=name).first()

    @classmethod
    def get_by_rule_name_parent(cls, rule_id, name, parent_folder_id):
        query = db.session.query(cls)
        query = query.filter(cls.rule_id == rule_id)
        query = query.filter(cls.name == name)
        query = query.filter(cls.parent_folder_id == parent_folder_id)
        return query.first()
 
    @classmethod
    def get_by_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(folder_id=folder_id).first()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def get_entities_by_rule_id(cls, rule_id):
        return db.session.query(cls).filter_by(rule_id=rule_id).all()
예제 #16
0
class ModelItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    status = db.Column(db.String)
    title = db.Column(db.String)
    artist = db.Column(db.String)
    album = db.Column(db.String)
    titleByTag = db.Column(db.String)
    artistByTag = db.Column(db.String)
    albumByTag = db.Column(db.String)
    filePath = db.Column(db.String)
    searchKey = db.Column(db.String)
    statusCd = db.Column(db.String)
    
    #inqueue_time = db.Column(db.DateTime)
    #start_time = db.Column(db.DateTime)
    #end_time = db.Column(db.DateTime)
    #status = db.Column(db.Integer) # 0:생성, 1:요청, 2:실패 11완료 12:파일잇음
    #str_status = db.Column(db.String)

    #title_id = db.Column(db.String)
    #episode_id = db.Column(db.String)
    
    #episode_title = db.Column(db.String)
    #download_count = db.Column(db.Integer)
    

    def __init__(self):
        self.created_time = datetime.now()
    
    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S') 
        #ret['start_time'] = self.start_time.strftime('%m-%d %H:%M:%S') if self.start_time is not None else None
        #ret['end_time'] = self.end_time.strftime('%m-%d %H:%M:%S') if self.end_time is not None else None

        return ret

    @staticmethod
    def save_as_dict(d):
        try:
            
            entity = ModelItem()
            entity.status = unicode(d['status'])
            entity.title = unicode(d['title'])
            entity.artist = unicode(d['artist'])
            entity.album = unicode(d['album'])
            entity.titleByTag = unicode(d['titleByTag'])
            entity.artistByTag = unicode(d['artistByTag'])
            entity.albumByTag = unicode(d['albumByTag'])
            entity.filePath = unicode(d['filePath'])
            entity.searchKey = unicode(d['searchKey'])
            entity.statusCd = unicode(d['statusCd'])
            
            db.session.add(entity)
            db.session.commit()
        except Exception as e:
            logger.error(d)
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
    @staticmethod
    def select(req):
        try:
            class_is = ModelItem
            ret = {}
            page = 1
            page_size = 30
            job_id = ''
            search = ''
            option = req.form['option']
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            query = db.session.query(class_is)
            if search != '':
                query = query.filter(class_is.title.like('%'+search+'%'))
            if option != 'all':
                query = query.filter(class_is.statusCd.like('%'+option+'%'))
            query = query.order_by(desc(class_is.id))
            count = query.count()
            query = query.limit(page_size).offset((page-1)*page_size)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
    
    @staticmethod
    def get(id):
        try:
            entity = db.session.query(ModelItem).filter_by(id=id).with_for_update().first()
            return entity
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
    
    @staticmethod
    def migration():
        try:
            
            from sqlalchemy.orm import sessionmaker
            Session = sessionmaker()
            engine = create_engine(app.config['SQLALCHEMY_BINDS'][package_name])
            Session.configure(bind=engine)

            sess = Session()
            query = sess.execute("SELECT * FROM musicProc_item")
            if "searchKey" not in query.keys():
                logger.debug( "migration !!")
                sess.execute('ALTER TABLE musicProc_item ADD COLUMN searchKey VARCHAR')
            if "statusCd" not in query.keys():
                logger.debug( "migration !!")
                sess.execute('ALTER TABLE musicProc_item ADD COLUMN statusCd VARCHAR')
            
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
    
    @staticmethod
    def delete(id):
        try:
            logger.debug( "delete")
            db.session.query(ModelItem).filter_by(id=id).delete()
            db.session.commit()
            
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #17
0
class ModelItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    deleteTime = db.Column(db.DateTime)
    plexSection = db.Column(db.String)
    filePath = db.Column(db.String)
    status = db.Column(db.String)
    
    def __init__(self):
        self.deleteTime = datetime.now()

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['deleteTime'] = self.deleteTime.strftime('%Y-%m-%d %H:%M:%S')

        return ret

    @staticmethod
    def save_as_dict(item):
        try:
            entity = ModelItem()
            entity.plexSection = unicode(item['section'])
            entity.filePath = unicode(item['file'][0])
            if ModelSetting.get('move_flag') == 'True':
                entity.status = unicode('이동')
                entity.filePath = entity.filePath + ' -> ' + ModelSetting.get('move_path')
            else:
                entity.status = unicode('삭제')
            db.session.add(entity)
            db.session.commit()
        except Exception as e:
            logger.debug(item)
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def web_list(req):
        try:
            ret = {}
            page = 1
            page_size = 30
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            order = req.form['order'] if 'order' in req.form else 'desc'
            query = ModelItem.make_query(search=search, order=order)
            count = query.count()
            query = query.limit(page_size).offset((page-1)*page_size)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def make_query(search, order):
        query = db.session.query(ModelItem)
        if search is not None and search != '':
            if search.find('|') != -1:
                conditions = []
                for tt in search.split('|'):
                    if tt != '': conditions.append(ModelItem.filePath.like('%'+tt.strip()+'%'))
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                for tt in search.split('|'):
                    if tt != '': query = query.filter(ModelItem.filePath.like('%'+tt.strip()+'%'))
            else:
                query = query.filter(ModelItem.filePath.like('%'+search+'%'))

        if order == 'desc':
            query = query.order_by(desc(ModelItem.id))
        else:
            query = query.order_by(ModelItem.id)
        return query
예제 #18
0
class ModelFeed(db.Model):
    __tablename__ = '%s_feed' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    rss_id = db.Column(db.Integer, db.ForeignKey('%s_rss.id' % package_name))
    title = db.Column(db.String)
    link = db.Column(db.String)
    description = db.Column(db.String)
    guid = db.Column(db.String)
    pubDate = db.Column(db.DateTime)

    status = db.Column(
        db.Integer
    )  # 0: 최초상태. 1:요청 실패  10 : 요청 성공  11 : rss만 저장, 12:필터 조건 False

    downloader_item_id = db.Column(db.Integer,
                                   db.ForeignKey('plugin_downloader_item.id'))
    downloader_item = db.relationship('ModelDownloaderItem')

    log = db.Column(db.String)

    torrent_info = db.Column(db.JSON)
    filename = db.Column(db.String)
    dirname = db.Column(db.String)
    filecount = db.Column(db.Integer)

    def __init__(self):
        self.created_time = datetime.datetime.now()
        self.status = 0

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['rss'] = self.rss.as_dict()

        try:
            ret['downloader_item'] = self.downloader_item.as_dict()
        except:
            pass
        return ret

    @staticmethod
    def get_feed_list_by_scheduler(rss):
        try:
            query = db.session.query(ModelFeed) \
                .filter(ModelFeed.rss_id == rss.id ) \
                .filter(ModelFeed.status < 10 ) \
                .filter(ModelFeed.created_time > datetime.datetime.now() + datetime.timedelta(days=-7))
            # \
            #.filter(ModelFeed.oc_status != 'NOSTATUS')
            return query.all()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    """
    def make_torrent_info(self):
        try:
            if self.job.use_tracer and self.torrent_info is None and self.link.startswith('magnet'):
                from torrent_info import Logic as TorrentInfoLogic
                tmp = TorrentInfoLogic.parse_magnet_uri(self.link)
                if tmp is not None:
                    self.torrent_info = tmp
                    flag_modified(self, "torrent_info")
                    info = Util.get_max_size_fileinfo(tmp)
                    self.filename = info['filename']
                    self.dirname = info['dirname']
                    self.filecount = tmp['num_files']
                    return True
        except Exception as e: 
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
        return False
    """

    @staticmethod
    def remove(rss_id):
        try:
            db.session.query(ModelFeed).filter_by(rss_id=rss_id).delete()
            db.session.commit()
            return 'success'
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return 'fail'

    @staticmethod
    def web_list(req):
        try:
            ret = {}
            page = 1
            page_size = 30
            job_id = ''
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            rss_select = 'all'
            if 'rss_select' in req.form:
                rss_select = req.form['rss_select']

            option = 'all'
            if 'option' in req.form:
                option = req.form['option']

            query = ModelFeed.make_query(rss_id=rss_select,
                                         option=option,
                                         search=search)

            count = query.count()
            query = (query.order_by(desc(
                ModelFeed.id)).limit(page_size).offset((page - 1) * page_size))
            logger.debug('ModelFeed count:%s', count)

            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.debug('Exception:%s', e)
            logger.debug(traceback.format_exc())

    """
    @staticmethod
    def api_list(req):
        try:
            job = req.args.get('job')
            option = req.args.get('option')
            search = req.args.get('search')
            count = req.args.get('count')
            if count is None or count == '':
                count = 100

            query = ModelFeed.make_query(job_name=job, option=option, search=search)
            query = (query.order_by(desc(ModelFeed.id))
                        .limit(count)
                )
            lists = query.all()
            return lists
        except Exception as e:
            logger.debug('Exception:%s', e)
            logger.debug(traceback.format_exc())
    """

    @staticmethod
    def make_query(rss_id='all', option='all', search=''):
        try:
            query = db.session.query(ModelFeed)
            job = None
            if rss_id != 'all':
                query = query.filter_by(rss_id=rss_id)

            if search is not None and search != '':
                if search.find('|') != -1:
                    tmp = search.split('|')
                    conditions = []
                    for tt in tmp:
                        if tt != '':
                            conditions.append(
                                ModelFeed.title.like('%' + tt.strip() + '%'))
                    query = query.filter(or_(*conditions))
                elif search.find(',') != -1:
                    tmp = search.split(',')
                    for tt in tmp:
                        if tt != '':
                            query = query.filter(
                                ModelFeed.title.like('%' + tt.strip() + '%'))
                else:
                    query = query.filter(
                        ModelFeed.title.like('%' + search + '%'))

            if option != 'all':
                query = query.filter(ModelFeed.status == int(option))
            return query
        except Exception as e:
            logger.debug('Exception:%s', e)
            logger.debug(traceback.format_exc())
예제 #19
0
class SubModelItem(db.Model):
    __tablename__ = '%s_sub_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    keyword = db.Column(db.String)
    media_path = db.Column(db.String)
    media_name = db.Column(db.String)

    last_search = db.Column(db.DateTime)  # 최근검색시간
    search_cnt = db.Column(db.Integer)  # 시도횟수

    sub_status = db.Column(
        db.Integer
    )  # 0.자막없음, 1:타겟자막없음, 2:자막있음, 3:자막다운완료, 33: 강제이동완료, 99:검색실패, 100:기간만료
    sub_name = db.Column(db.String)
    sub_url = db.Column(db.String)  # 자막파일url

    plex_section_id = db.Column(db.String)
    plex_metakey = db.Column(db.String)

    #def __init__(self, keyword, media_path, media_name):
    def __init__(self, fullpath):
        self.keyword, dirname, name, ext = SubModelItem.parse_fname(fullpath)
        if ext.lower() in ['.smi', '.srt', '.ass']:
            raise Exception('is sub')

        self.created_time = datetime.now()
        #self.media_path = media_path
        self.media_path = fullpath
        self.media_name = name + ext
        self.search_cnt = 0
        self.last_search = datetime.now()
        self.sub_status = 0
        self.sub_url = None
        self.sub_name = None
        self.plex_section_id = None
        self.plex_metakey = None

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['last_search'] = self.last_search.strftime('%m-%d %H:%M:%S')
        ret['media_path'] = os.path.dirname(self.media_path)
        return ret

    def remove(self):
        try:
            db.session.delete(self)
            db.session.commit()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    def sub_status_to_str(self):
        if self.sub_status == 0:
            return '자막없음'
        elif self.sub_status == 1:
            return '타겟자막없음'
        elif self.sub_status == 2:
            return '자막있음'
        elif self.sub_status == 3:
            return '자막다운완료'
        elif self.sub_status == 99:
            return '검색실패'
        elif self.sub_status == 100:
            return '만료'
        else:
            return '--'

    @staticmethod
    def create(fullpath):
        try:
            entity = SubModelItem.get_entity_by_fullpath(fullpath)
            if entity is None:
                entity = SubModelItem(fullpath)
                entity.save()
                return entity
            return None
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def print_entity(entity):
        try:
            logger.debug('------------------------------------------------')
            logger.debug('id              : %s', entity.id)
            logger.debug('created_time    : %s', entity.created_time)
            logger.debug('keyword         : %s', entity.keyword)
            logger.debug('media_path      : %s', entity.media_path)
            logger.debug('media_name      : %s', entity.media_name)
            logger.debug('search_cnt      : %s', entity.search_cnt)
            logger.debug('last_search     : %s', entity.last_search)
            logger.debug('sub_status      : %s(%s)',
                         entity.sub_status_to_str(), entity.sub_status)
            logger.debug(
                'plex_section_id : %s', entity.plex_section_id
                if entity.plex_section_id is not None else 'None')
            logger.debug(
                'plex_metakey    : %s', entity.plex_metakey
                if entity.plex_metakey is not None else 'None')
            logger.debug('------------------------------------------------')
        except:
            return False

    # keyword, 디렉토리, 파일명(확장자제외), 확장자
    @staticmethod
    def parse_fname(path):
        name, ext = os.path.splitext(os.path.basename(path))
        dirname = os.path.dirname(path)
        key = name.split('[')[0].strip()
        if key[-3:-1] == 'cd':
            key = key[:-3]
        return key, dirname, name, ext

    @staticmethod
    def add_subcat_queue(target_filepath):
        try:
            keyword, dname, fname, ext = SubModelItem.parse_fname(
                target_filepath)
            item = SubModelItem(keyword, dname, fname + ext)
            item.save()
            logger.debug('new file added(%s)', target_filepath)
            return True
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return False

    # 동일 키워드 이름이 다른 파일들이 많은경우 보장안됨.
    """
    @staticmethod
    def get_entity(keyword):
        try:
            entity = db.session.query(SubModelItem).filter_by(keyword=keyword).with_for_update().first()
            if entity is not None:
                return entity
        except:
            return None
    """

    @staticmethod
    def get_entity_by_id(data_id):
        try:
            entity = db.session.query(SubModelItem).filter_by(
                id=data_id).with_for_update().first()
            if entity is not None:
                return entity
        except:
            return None

    @staticmethod
    def get_entity_by_fullpath(path):
        try:
            entity = db.session.query(SubModelItem).filter_by(
                media_path=path).with_for_update().first()
            if entity is not None:
                return entity
        except:
            return None

    @staticmethod
    def get_recent_entities():
        try:
            currtime = datetime.now()

            time_period = ModelSetting.get_int('subcat_time_period')
            day_limit = ModelSetting.get_int('subcat_day_limit')
            logger.debug('get recent list: period(%d), limit(%d)', time_period,
                         day_limit)

            query = db.session.query(SubModelItem)

            query = query.filter(SubModelItem.sub_status != 3)  # 자막다운받은경우제외
            query = query.filter(SubModelItem.sub_status != 33)  # 자막강제이동된경우제외
            query = query.filter(SubModelItem.sub_status != 100)  # 만료된경우제외

            ptime = currtime + timedelta(hours=-time_period)
            #ptime = currtime + timedelta(minutes=-10) #임시코드
            query = query.filter(
                or_(SubModelItem.last_search < ptime,
                    SubModelItem.search_cnt == 0))  # 최근검색한 목록 제외
            count = query.count()
            if count == 0: return []
            entities = query.all()

            for entity in entities:
                pday = entity.created_time + timedelta(days=day_limit)
                if currtime > pday:
                    lists.remove(entity)
                    entity.sub_status = 100
                    entity.save()

            return entities

        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return None

    @staticmethod
    def get_all_entities():
        try:
            query = db.session.query(SubModelItem)
            query = query.filter(SubModelItem.sub_status != 3)  # 자막다운받은경우제외
            query = query.filter(SubModelItem.sub_status != 33)  # 자막강제이동된경우제외
            count = query.count()
            entities = query.all()
            return entities

        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return None

    @staticmethod
    def get_all_entities_with_sub():
        try:
            query = db.session.query(SubModelItem)
            query = query.filter(
                SubModelItem.sub_status == 3)  # 자막이 있고 이동되지 않은 경우만

            # TODO: 자막강제이동의 경우::: 일단 남겨둠-- 추후 제거 필요
            if ModelSetting.get_bool('subcat_force_move_flag'):
                force_move_path = ModelSetting.get('subcat_force_move_path')
                if os.path.isdir(force_move_path) is not True:
                    logger.warning('invalid force_move_path(%s)',
                                   force_move_path)
                    return []
                query = query.filter(
                    ~SubModelItem.media_path.like(force_move_path +
                                                  '%'))  # 강제이동미실행된경우만

            count = query.count()
            entities = query.all()
            return entities

        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return None

    @staticmethod
    def web_list(req):
        try:
            ret = {}
            page = 1
            page_size = ModelSetting.get_int('web_page_size')
            job_id = ''
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            option = req.form['option'] if 'option' in req.form else 'all'
            order = req.form['order'] if 'order' in req.form else 'desc'

            query = SubModelItem.make_query(search=search,
                                            option=option,
                                            order=order)
            count = query.count()
            query = query.limit(page_size).offset((page - 1) * page_size)
            logger.debug('SubModelItem count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def make_query(search='', option='all', order='desc'):
        query = db.session.query(SubModelItem)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(
                            SubModelItem.media_name.like('%' + tt.strip() +
                                                         '%'))
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query = query.filter(
                            SubModelItem.media_name.like('%' + tt.strip() +
                                                         '%'))
            else:
                query = query.filter(
                    SubModelItem.media_name.like('%' + search + '%'))

        if option != 'all':
            query = query.filter(SubModelItem.sub_status == option)

        if order == 'desc':
            query = query.order_by(desc(SubModelItem.id))
        else:
            query = query.order_by(SubModelItem.id)

        return query
예제 #20
0
파일: model.py 프로젝트: soju6jan/wavve
class ModelWavveEpisode(db.Model):
    __tablename__ = 'plugin_%s_auto_episode' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    contents_json = db.Column(db.JSON)
    streaming_json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    channelname = db.Column(db.String)

    programid = db.Column(db.String)
    programtitle = db.Column(db.String)

    contentid = db.Column(db.String)
    releasedate = db.Column(db.String)
    episodenumber = db.Column(db.String)
    episodetitle = db.Column(db.String)
    quality = db.Column(db.String)

    vod_type = db.Column(db.String)  #general onair
    image = db.Column(db.String)
    playurl = db.Column(db.String)

    filename = db.Column(db.String)
    duration = db.Column(db.Integer)
    start_time = db.Column(db.DateTime)
    end_time = db.Column(db.DateTime)
    download_time = db.Column(db.Integer)
    completed = db.Column(db.Boolean)
    user_abort = db.Column(db.Boolean)
    pf_abort = db.Column(db.Boolean)
    etc_abort = db.Column(db.Integer)  #ffmpeg 원인 1, 채널, 프로그램
    ffmpeg_status = db.Column(db.Integer)
    temp_path = db.Column(db.String)
    save_path = db.Column(db.String)
    pf = db.Column(db.Integer)
    retry = db.Column(db.Integer)
    filesize = db.Column(db.Integer)
    filesize_str = db.Column(db.String)
    download_speed = db.Column(db.String)
    call = db.Column(db.String)

    def __init__(self, call, info, streaming):
        self.created_time = datetime.now()
        self.completed = False
        self.user_abort = False
        self.pf_abort = False
        self.etc_abort = 0
        self.ffmpeg_status = -1
        self.pf = 0
        self.retry = 0
        self.call = call
        self.set_info(info)
        self.set_streaming(streaming)

    def __repr__(self):
        #return "<Episode(id:%s, episode_code:%s, quality:%s)>" % (self.id, self.episode_code, self.quality)
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if self.created_time is not None else ''
        ret['start_time'] = self.start_time.strftime(
            '%m-%d %H:%M:%S') if self.start_time is not None else ''
        ret['end_time'] = self.end_time.strftime(
            '%m-%d %H:%M:%S') if self.end_time is not None else ''
        return ret

    def set_info(self, data):
        self.contents_json = data
        self.channelname = data['channelname']

        self.programid = data['programid']
        self.programtitle = data['programtitle']

        self.contentid = data['contentid']
        self.releasedate = data['releasedate']
        self.episodenumber = data['episodenumber']
        self.episodetitle = data['episodetitle']
        self.image = 'https://' + data['image']
        self.vod_type = data['type']

    def set_streaming(self, data):
        self.streaming_json = data
        self.playurl = data['playurl']
        import framework.wavve.api as Wavve
        self.filename = Wavve.get_filename(self.contents_json, data['quality'])
        self.quality = data['quality']
예제 #21
0
파일: model.py 프로젝트: soju6jan/wavve
class ModelWavveProgram(db.Model):
    __tablename__ = 'plugin_%s_auto_program' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)

    created_time = db.Column(db.DateTime)
    completed_time = db.Column(db.DateTime)

    episode_code = db.Column(db.String)
    program_id = db.Column(db.String)
    quality = db.Column(db.String)
    program_title = db.Column(db.String)
    episode_number = db.Column(db.String)
    thumbnail = db.Column(db.String)
    programimage = db.Column(db.String)

    completed = db.Column(db.Boolean)

    def __init__(self, data):
        self.episode_code = data['episode_code']
        self.quality = data['quality']
        self.completed = False
        # self.program_title  = data.json_data['programtitle']
        # self.episode_number = data.json_data['episodenumber']
        # self.thumbnail      = data.json_data['image']
        # self.programimage   = data.json_data['programimage']

    def __repr__(self):
        #return "<Episode(id:%s, episode_code:%s, quality:%s)>" % (self.id, self.episode_code, self.quality)
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if self.created_time is not None else ''
        ret['completed_time'] = self.completed_time.strftime(
            '%m-%d %H:%M:%S') if self.completed_time is not None else ''
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def set(key, value):
        try:
            item = db.session.query(ModelWavveProgram).filter_by(
                key=key).with_for_update().first()
            if item is not None:
                item.value = value.strip()
                db.session.commit()
            else:
                db.session.add(ModelWavveProgram(key, value.strip()))
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def get(episode_code, quality):
        try:
            return db.session.query(ModelWavveProgram).filter_by(
                episode_code=episode_code, quality=quality).first()
        except Exception as e:
            logger.error('Exception:%s %s', e, episode_code)
            logger.error(traceback.format_exc())

    @staticmethod
    def get_failed():
        try:
            return db.session.query(ModelWavveProgram).filter_by(
                completed=False).all()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def is_duplicate(episode_code, quality):
        try:
            if ModelWavveProgram.get(episode_code, quality) is not None:
                return True
            return False
        except Exception as e:
            logger.error('Exception:%s %s', e, episode_code)
            logger.error(traceback.format_exc())

    ### only works with completed items.
    @staticmethod
    def delete(episode_code, quality):
        try:
            item = db.session.query(ModelWavveProgram).filter_by(
                episode_code=episode_code, quality=quality).first()
            if item is not None:
                db.session.delete(item)
                db.session.commit()
            else:
                return False
            return True
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return False

    @staticmethod
    def remove_all(is_completed=True):  # to remove_all(True/False)
        try:
            db.session.query(ModelWavveProgram).filter_by(
                completed=is_completed).delete()
            db.session.commit()
            return True
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return False

    @classmethod
    def update(cls, entity):
        item = db.session.query(ModelWavveProgram).filter_by(
            episode_code=entity['episode_code'],
            quality=entity['quality']).with_for_update().first()
        if item is not None:
            item.created_time = datetime.strptime(entity['created_time'],
                                                  '%m-%d %H:%M:%S')
            item.program_id = entity.json_data['programid']
            item.program_title = entity.json_data['programtitle']
            item.episode_number = entity.json_data['episodenumber']
            item.thumbnail = entity.json_data['image']
            item.programimage = entity.json_data['programimage']
            item.completed = entity['completed']
            item.completed_time = datetime.strptime(entity['completed_time'],
                                                    '%m-%d %H:%M:%S')
            db.session.commit()
        else:
            ModelWavveProgram.append(entity)

    @classmethod
    def append(cls, q):
        item = ModelWavveProgram(q)
        item.save()

    ### codes from bot_downloader_ktv
    @staticmethod
    def filelist(req):
        try:
            ret = {}
            page = 1
            # page_size = ModelSetting.get_int('web_page_size')
            page_size = 20
            job_id = ''
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            option = req.form['option']  # all, completed, failed
            order = req.form['order'] if 'order' in req.form else 'desc'

            query = ModelWavveProgram.make_query(search, option, order)
            count = query.count()
            query = query.limit(page_size).offset((page - 1) * page_size)
            logger.debug('ModelWavveProgram count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    ### codes from bot_downloader_ktv
    @staticmethod
    def make_query(search, option, order):
        query = db.session.query(ModelWavveProgram)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                conditions2 = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(
                            ModelWavveProgram.program_title.like('%' +
                                                                 tt.strip() +
                                                                 '%'))
                        conditions2.append(
                            ModelWavveProgram.episode_number.like(tt.strip()))
                query1 = query.filter(or_(*conditions))
                query2 = query.filter(or_(*conditions2))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query1 = query.filter(
                            ModelWavveProgram.program_title.like('%' +
                                                                 tt.strip() +
                                                                 '%'))
                        query2 = query.filter(
                            ModelWavveProgram.episode_number.like(tt.strip()))
            else:
                query1 = query.filter(
                    ModelWavveProgram.program_title.like('%' + search + '%'))
                query2 = query.filter(
                    ModelWavveProgram.episode_number.like(search))

            query = query1.union(query2)

        if option == 'completed':
            query = query.filter(ModelWavveProgram.completed == True)
        elif option == 'failed':
            query = query.filter(ModelWavveProgram.completed == False)

        if order == 'desc':
            query = query.order_by(desc(ModelWavveProgram.id))
        else:
            query = query.order_by(ModelWavveProgram.id)

        return query

    ### codes from bot_downloader_ktv
    ### 작동하는지 확인 안해봄
    @staticmethod
    def itemlist_by_api(req):
        try:
            search = req.args.get('search')
            logger.debug(search)
            option = req.args.get('option')
            order = 'desc'
            count = req.args.get('count')
            if count is None or count == '':
                count = 100
            query = ModelWavveProgram.make_query(search, option, order)
            query = query.limit(count)
            lists = query.all()
            return lists
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #22
0
파일: model.py 프로젝트: k123s456h/wavve
class ModelWavveProgram(db.Model):
    __tablename__ = 'plugin_%s_auto_program' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)

    created_time = db.Column(db.DateTime)
    completed_time = db.Column(db.DateTime)

    episode_code = db.Column(db.String)
    program_id = db.Column(db.String)
    quality = db.Column(db.String)
    program_title = db.Column(db.String)
    episode_number = db.Column(db.String)
    thumbnail = db.Column(db.String)
    programimage = db.Column(db.String)

    completed = db.Column(db.Boolean)

    def __init__(self, data):
        self.episode_code = data['episode_code']
        self.quality = data['quality']
        self.completed = False
        # self.program_title  = data.json_data['programtitle']
        # self.episode_number = data.json_data['episodenumber']
        # self.thumbnail      = data.json_data['image']
        # self.programimage   = data.json_data['programimage']

    def __repr__(self):
        #return "<Episode(id:%s, episode_code:%s, quality:%s)>" % (self.id, self.episode_code, self.quality)
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime(
            '%m-%d %H:%M:%S') if self.created_time is not None else ''
        ret['completed_time'] = self.completed_time.strftime(
            '%m-%d %H:%M:%S') if self.completed_time is not None else ''
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def set(key, value):
        try:
            item = db.session.query(ModelWavveProgram).filter_by(
                key=key).with_for_update().first()
            if item is not None:
                item.value = value.strip()
                db.session.commit()
            else:
                db.session.add(ModelWavveProgram(key, value.strip()))
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def get(episode_code, quality):
        try:
            return db.session.query(ModelWavveProgram).filter_by(
                episode_code=episode_code, quality=quality).first()
        except Exception as e:
            logger.error('Exception:%s %s', e, episode_code)
            logger.error(traceback.format_exc())

    @staticmethod
    def get_failed():
        try:
            return db.session.query(ModelWavveProgram).filter_by(
                completed=False).all()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def is_duplicate(episode_code, quality):
        try:
            if ModelWavveProgram.get(episode_code, quality) is not None:
                return True
            return False
        except Exception as e:
            logger.error('Exception:%s %s', e, episode_code)
            logger.error(traceback.format_exc())

    ### only works with completed items.
    @staticmethod
    def delete(episode_code, quality):
        try:
            item = db.session.query(ModelWavveProgram).filter_by(
                episode_code=episode_code, quality=quality).first()
            if item is not None:
                db.session.delete(item)
                db.session.commit()
            else:
                return False
            return True
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return False
예제 #23
0
class ModelItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    av_type = db.Column(db.String)
    is_file = db.Column(db.Boolean)

    source_dir = db.Column(db.String)
    source_filename = db.Column(db.String)
    source_path = db.Column(db.String)
    move_type = db.Column(db.Integer)  # -1, 0:정상, 1:타입불일치, 2:중복삭제
    # uncensored  0:정상, 1:no_type 2:중복삭제 3:유저타겟

    target_dir = db.Column(db.String)
    target_filename = db.Column(db.String)
    target_path = db.Column(db.String)

    log = db.Column(db.String)

    def __init__(self, av_type, source_dir, source_filename):
        self.av_type = av_type
        self.created_time = datetime.now()
        self.is_file = True
        self.source_dir = source_dir
        self.source_filename = source_filename
        self.move_type = -1

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        return ret

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def web_list(req):
        try:
            ret = {}
            page = 1
            page_size = ModelSetting.get_int('web_page_size')
            job_id = ''
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            option = req.form['option']
            order = req.form['order'] if 'order' in req.form else 'desc'
            av_type = req.form['av_type']

            query = ModelItem.make_query(search=search,
                                         av_type=av_type,
                                         option=option,
                                         order=order)
            count = query.count()
            query = query.limit(page_size).offset((page - 1) * page_size)
            logger.debug('ModelItem count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def make_query(search='', av_type='all', option='all', order='desc'):
        query = db.session.query(ModelItem)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(
                            ModelItem.source_filename.like('%' + tt.strip() +
                                                           '%'))
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query = query.filter(
                            ModelItem.source_filename.like('%' + tt.strip() +
                                                           '%'))
            else:
                query = query.filter(
                    or_(ModelItem.source_filename.like('%' + search + '%'),
                        ModelItem.target_filename.like('%' + search + '%')))

        if av_type != 'all':
            query = query.filter(ModelItem.av_type == av_type)

        if option != 'all':
            query = query.filter(ModelItem.move_type == option)

        if order == 'desc':
            query = query.order_by(desc(ModelItem.id))
        else:
            query = query.order_by(ModelItem.id)

        return query
예제 #24
0
파일: model.py 프로젝트: zavarat/tving
class Episode(db.Model):
    __tablename__ = 'plugin_%s_auto_episode' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    episode_code = db.Column(db.String(20), unique=False, nullable=False)
    quality = db.Column(db.String(10), unique=False, nullable=False)
    program_name = db.Column(db.String(30))
    program_code = db.Column(db.String(20))
    frequency = db.Column(db.Integer)
    broadcast_date = db.Column(db.String(6))
    channel_id = db.Column(db.String(10))
    channel_name = db.Column(db.String(20))
    duration = db.Column(db.Integer)
    json = db.Column(db.JSON)

    filename = db.Column(db.String(), unique=False, nullable=False)
    start_time = db.Column(db.DateTime)
    end_time = db.Column(db.DateTime)
    download_time = db.Column(db.Integer)
    completed = db.Column(db.Boolean)
    user_abort = db.Column(db.Boolean)
    pf_abort = db.Column(db.Boolean)
    etc_abort = db.Column(db.Integer)  #ffmpeg 원인 1, 채널, 프로그램
    ffmpeg_status = db.Column(db.Integer)
    temp_path = db.Column(db.String())
    save_path = db.Column(db.String())
    pf = db.Column(db.Integer)
    retry = db.Column(db.Integer)

    filesize = db.Column(db.Integer)
    filesize_str = db.Column(db.String(10))
    download_speed = db.Column(db.String(10))
    call = db.Column(db.String(10))
    vod_url = db.Column(db.String)

    def __init__(self, call):
        self.completed = False
        self.user_abort = False
        self.pf_abort = False
        self.etc_abort = 0
        self.ffmpeg_status = -1
        self.pf = 0
        self.retry = 0
        self.call = call

    def __repr__(self):
        #return "<Episode(id:%s, episode_code:%s, quality:%s)>" % (self.id, self.episode_code, self.quality)
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['start_time'] = self.start_time.strftime(
            '%m-%d %H:%M:%S') if self.start_time is not None else ''
        ret['end_time'] = self.end_time.strftime(
            '%m-%d %H:%M:%S') if self.end_time is not None else ''
        return ret
예제 #25
0
class ModelRss(db.Model):
    __tablename__ = '%s_rss' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)

    update_time = db.Column(db.DateTime)
    name = db.Column(db.String)
    rss_url = db.Column(db.String)
    download_program = db.Column(db.String)
    download_path = db.Column(db.String)

    download_mode = db.Column(db.String)  #0:필터 사용 1:모두 받기. 2: RSS만 저장,

    include_keyword = db.Column(db.String)
    exclude_keyword = db.Column(db.String)

    use_filename_filter = db.Column(db.Boolean)
    filename_include_keyword = db.Column(db.String)  #정규식 혹은 키워드  || 폴더
    filename_exclude_keyword = db.Column(db.String)

    feed_list = db.relationship('ModelFeed', backref='rss', lazy=True)

    def __init__(self):
        self.created_time = datetime.datetime.now()
        self.use_filename_filter = False

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['update_time'] = '' if self.update_time is None else self.created_time.strftime(
            '%m-%d %H:%M:%S')
        ret['name'] = self.name if self.name is not None else self.id

        return ret

    @staticmethod
    def save(req):
        try:
            rss_id = req.form['rss_id']
            if rss_id == '-1':
                entity = ModelRss()
            else:
                entity = db.session.query(ModelRss).filter_by(
                    id=rss_id).first()
            entity.name = req.form['name']
            entity.rss_url = req.form['rss_url']
            entity.download_program = req.form['download_program']
            entity.download_path = req.form['download_path']
            entity.download_mode = req.form['download_mode']
            entity.include_keyword = req.form['include_keyword']
            entity.exclude_keyword = req.form['exclude_keyword']
            """
            entity.use_filename_filter = (req.form['use_filename_filter'] == 'True')
            entity.filename_include_keyword = req.form['filename_include_keyword']
            entity.filename_exclude_keyword = req.form['filename_exclude_keyword']
            """
            db.session.add(entity)
            db.session.commit()
            return 'success'
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return 'fail'

    @staticmethod
    def get_list(by_dict=False):
        try:
            tmp = db.session.query(ModelRss).all()
            if by_dict:
                tmp = [x.as_dict() for x in tmp]
            return tmp
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def remove(rss_id):
        try:
            logger.debug('remove_rss id:%s', rss_id)
            entity = db.session.query(ModelRss).filter_by(id=rss_id).first()
            db.session.delete(entity)
            db.session.commit()
            return 'success'
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            return 'fail'

    """
예제 #26
0
class ModelSampleItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)
    # 사용할 필드 정의
    sample_string = db.Column(db.String)
    sample_integer = db.Column(db.Integer)
    sample_boolean = db.Column(db.Boolean)
    sample_imgurl = db.Column(db.String)

    def __init__(self, string, integer, boolean, imgurl):
        self.created_time = datetime.now()
        self.sample_string = py_unicode(string)
        self.sample_integer = integer
        self.sample_boolean = boolean
        self.sample_imgurl = imgurl

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def delete(cls, id):
        db.session.query(cls).filter_by(id=id).delete()
        db.session.commit()

    @classmethod
    def get_by_id(cls, id):
        return db.session.query(cls).filter_by(id=id).first()

    @classmethod
    def get_by_string(cls, string):
        return db.session.query(cls).filter_by(
            sample_string=sample_string).first()

    @classmethod
    def get_by_integer(cls, integer):
        return db.session.query(cls).filter_by(
            sample_integer=sample_integer).first()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def web_list(cls, req):
        try:
            ret = {}
            page = 1
            page_size = 30
            job_id = ''
            search = ''
            category = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            if 'order' in req.form:
                order = req.form['order']

            query = cls.make_query(search=search, order=order)
            count = query.count()
            query = query.limit(page_size).offset((page - 1) * page_size)
            logger.debug('cls count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @classmethod
    def make_query(cls, search='', order='desc'):
        query = db.session.query(cls)
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(
                            cls.sample_string.like('%' + tt.strip() + '%'))
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query = query.filter(
                            cls.sample_string.like('%' + tt.strip() + '%'))
            else:
                query = query.filter(cls.sample_string.like('%' + search +
                                                            '%'))

        if order == 'desc': query = query.order_by(desc(cls.id))
        else: query = query.order_by(cls.id)
        return query
예제 #27
0
class ModelBotDownloaderKtvVodItem(db.Model):
    __tablename__ = '%s_%s_item' % (P.package_name, sub_name)
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = P.package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    share_request_time = db.Column(db.DateTime)
    share_completed_time = db.Column(db.DateTime)
    data = db.Column(db.JSON)
    fileid = db.Column(db.String)
    filename = db.Column(db.String)
    size = db.Column(db.Integer)
    filename_name = db.Column(db.String)
    filename_number = db.Column(db.Integer)
    filename_release = db.Column(db.String)
    filename_filename_rule = db.Column(db.String)
    filename_date = db.Column(db.String)
    filename_quality = db.Column(db.String)
    daum_genre = db.Column(db.String)
    daum_id = db.Column(db.String)
    daum_title = db.Column(db.String)
    daum_poster_url = db.Column(db.String)
    log = db.Column(db.String)

    def __init__(self):
        self.created_time = datetime.datetime.now()
        self.log = ''

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['share_request_time'] = self.share_request_time.strftime(
            '%m-%d %H:%M:%S') if self.share_request_time is not None else None
        ret['share_completed_time'] = self.share_completed_time.strftime(
            '%m-%d %H:%M:%S'
        ) if self.share_completed_time is not None else None
        return ret

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception as e:
            P.logger.error('Exception:%s', e)
            P.logger.error(traceback.format_exc())

    @classmethod
    def process_telegram_data(cls, data):
        try:
            entity = db.session.query(cls).filter_by(
                filename=data['f']).first()
            if entity is not None:
                return
            entity = ModelBotDownloaderKtvVodItem()
            entity.data = data
            entity.fileid = data['id']
            entity.filename = data['f']
            entity.size = data['s']

            entity.filename_name = data['ktv']['name']
            entity.filename_number = data['ktv']['number']
            entity.filename_release = data['ktv']['release']
            entity.filename_rule = data['ktv']['filename_rule']
            entity.filename_date = data['ktv']['date']
            entity.filename_quality = data['ktv']['quality']

            if data['daum'] is not None:
                entity.daum_genre = data['daum']['genre']
                entity.daum_id = data['daum']['daum_id']
                entity.daum_title = data['daum']['title']
                entity.daum_poster_url = data['daum']['poster_url']
            else:
                entity.daum_genre = u'미분류'
            #entity.save()
            return entity
        except Exception as e:
            P.logger.error('Exception:%s', e)
            P.logger.error(traceback.format_exc())

    @classmethod
    def web_list(cls, req):
        try:
            ret = {}
            page = 1
            page_size = 30
            search = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            option = req.form['option'] if 'option' in req.form else None
            order = req.form['order'] if 'order' in req.form else 'desc'
            query = cls.make_query(search, option, order)
            count = query.count()
            query = query.limit(page_size).offset((page - 1) * page_size)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception, e:
            P.logger.error('Exception:%s', e)
            P.logger.error(traceback.format_exc())
예제 #28
0
class ModelItem(db.Model):
    __tablename__ = '%s_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    json = db.Column(db.JSON)
    created_time = db.Column(db.DateTime)

    inqueue_time = db.Column(db.DateTime)
    #start_time = db.Column(db.DateTime)
    #end_time = db.Column(db.DateTime)
    status = db.Column(db.Integer)  # 0:생성, 1:요청, 2:실패 11완료 12:파일잇음
    str_status = db.Column(db.String)

    title_id = db.Column(db.String)
    episode_id = db.Column(db.String)
    title = db.Column(db.String)
    episode_title = db.Column(db.String)
    download_count = db.Column(db.Integer)
    filename = db.Column(db.String)

    def __init__(self, title_id, episode_id):
        self.created_time = datetime.now()
        self.download_count = 0
        self.title_id = title_id
        self.episode_id = episode_id
        self.status = 0
        self.str_status = u'대기'

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S')
        ret['inqueue_time'] = self.inqueue_time.strftime(
            '%m-%d %H:%M:%S') if self.inqueue_time is not None else None
        #ret['start_time'] = self.start_time.strftime('%m-%d %H:%M:%S') if self.start_time is not None else None
        #ret['end_time'] = self.end_time.strftime('%m-%d %H:%M:%S') if self.end_time is not None else None

        return ret

    @staticmethod
    def init(title_id, episode_id):
        try:
            entity = db.session.query(ModelItem).filter_by(
                title_id=title_id).filter_by(episode_id=episode_id).first()
            if entity is None:
                entity = ModelItem(title_id, episode_id)
                entity.inqueue_time = datetime.now()
                db.session.add(entity)
                db.session.commit()
            #else:
            #    if entity.status > 10:
            #        return None
            return entity.as_dict()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def save_as_dict(d):
        try:
            logger.debug(d)
            entity = db.session.query(ModelItem).filter_by(
                id=d['id']).with_for_update().first()
            if entity is not None:
                entity.status = d['status']
                entity.str_status = unicode(d['str_status'])
                entity.title_id = d['title_id']
                entity.episode_id = d['episode_id']
                entity.title = unicode(d['title'])
                entity.episode_title = unicode(d['episode_title'])
                entity.download_count = d['download_count']
                entity.filename = d['filename']

                db.session.commit()
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @staticmethod
    def get_list(by_dict=False):
        try:
            tmp = db.session.query(ModelItem).all()
            if by_dict:
                tmp = [x.as_dict() for x in tmp]
            return tmp
        except Exception, e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
예제 #29
0
class ModelSetting(db.Model):
    __tablename__ = '%s_setting' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), unique=True, nullable=False)
    value = db.Column(db.String, nullable=False)

    def __init__(self, key, value):
        self.key = key
        self.value = value

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        return {x.name: getattr(self, x.name) for x in self.__table__.columns}

    @staticmethod
    def get(key):
        try:
            return db.session.query(ModelSetting).filter_by(key=key).first().value.strip()
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())


    @staticmethod
    def get_int(key):
        try:
            return int(ModelSetting.get(key))
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def get_bool(key):
        try:
            return (ModelSetting.get(key) == 'True')
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def set(key, value):
        try:
            item = db.session.query(ModelSetting).filter_by(key=key).with_for_update().first()
            if item is not None:
                item.value = value.strip()
                db.session.commit()
            else:
                db.session.add(ModelSetting(key, value.strip()))
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def to_dict():
        try:
            from framework.util import Util
            return Util.db_list_to_dict(db.session.query(ModelSetting).all())
        except Exception as e:
            logger.error('Exception:%s %s', e, key)
            logger.error(traceback.format_exc())

    @staticmethod
    def setting_save(req):
        try:
            for key, value in req.form.items():
                if key in ['scheduler', 'is_running']:
                    continue
                if key.startswith('tmp_'):
                    continue
                logger.debug('Key:%s Value:%s', key, value)
                entity = db.session.query(ModelSetting).filter_by(key=key).with_for_update().first()
                entity.value = value
            db.session.commit()
            return True                  
        except Exception as e: 
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            logger.debug('Error Key:%s Value:%s', key, value)
            return False

    @staticmethod
    def get_list(key):
        try:
            value = ModelSetting.get(key)
            values = [x.strip().strip() for x in value.replace('\n', '|').split('|')]
            values = Util.get_list_except_empty(values)
            return values
        except Exception as e: 
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())
            logger.error('Error Key:%s Value:%s', key, value)
예제 #30
0
class ModelRuleItem(db.Model):
    __tablename__ = '%s_rule_item' % package_name
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    __bind_key__ = package_name

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime)
    reserved = db.Column(db.JSON)
    name = db.Column(db.String)
    agent_type = db.Column(db.String)
    root_folder_id = db.Column(db.String)
    root_full_path = db.Column(db.String)
    max_depth = db.Column(db.Integer)
    target_folder_id = db.Column(db.String)
    target_full_path = db.Column(db.String)
    item_count = db.Column(db.Integer)
    shortcut_count = db.Column(db.Integer)
    use_subfolder = db.Column(db.Boolean)
    subfolder_rule = db.Column(db.String)
    last_searched_time = db.Column(db.DateTime)
    use_schedule = db.Column(db.Boolean)
    use_plex = db.Column(db.Boolean)
    use_auto_create_shortcut = db.Column(db.Boolean)

    def __init__(self, info):
        self.created_time = datetime.now()
        self.name = py_unicode(info['name'])
        self.agent_type = py_unicode(info['agent_type'])
        self.root_folder_id = py_unicode(info['root_folder_id'])
        self.root_full_path = py_unicode(info['root_full_path'])
        self.max_depth = info['max_depth']
        self.target_folder_id = py_unicode(info['target_folder_id'])
        self.target_full_path = py_unicode(info['target_full_path'])
        self.item_count = 0
        self.shortcut_count = 0
        self.use_sub_folder = info['use_subfolder']
        self.subfolder_rule = py_unicode(info['subfolder_rule'])
        self.use_auto_create_shortcut = info['use_auto_create_shortcut']
        self.use_schedule = info['use_schedule']
        self.use_plex = info['use_plex']
        self.last_search_time = None

    def __repr__(self):
        return repr(self.as_dict())

    def as_dict(self):
        ret = {x.name: getattr(self, x.name) for x in self.__table__.columns}
        ret['created_time'] = self.created_time.strftime('%m-%d %H:%M:%S') 
        if self.last_searched_time == None:
            ret['last_searched_time'] = u'-'
        else:
            ret['last_searched_time'] = self.last_searched_time.strftime('%m-%d %H:%M:%S') 
        return ret

    def save(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def delete(cls, id):
        db.session.query(cls).filter_by(id=id).delete()
        db.session.commit()

    @classmethod
    def get_by_id(cls, id):
        return db.session.query(cls).filter_by(id=id).first()
    
    @classmethod
    def get_by_name(cls, name):
        return db.session.query(cls).filter_by(name=name).first()

    @classmethod
    def get_scheduled_entities(cls):
        return db.session.query(cls).filter_by(use_schedule=True).all()
    
    @classmethod
    def get_by_root_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(root_folder_id=folder_id).first()

    @classmethod
    def get_by_target_folder_id(cls, folder_id):
        return db.session.query(cls).filter_by(target_folder_id=folder_id).first()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def get_all_entities(cls):
        return db.session.query(cls).all()

    @classmethod
    def get_all_entities_except_av(cls):
        query = db.session.query(cls)
        return query.filter(not_(cls.agent_type.like('av%'))).all()

    @classmethod
    def get_all_tv_entities(cls):
        query = db.session.query(cls)
        return query.filter(cls.agent_type.like('%tv')).all()

    @classmethod
    def get_all_mv_entities(cls):
        return db.session.query(cls).filter_by(agent_type='movie').all()

    @classmethod
    def get_all_av_entities(cls):
        query = db.session.query(cls)
        return query.filter(cls.agent_type.like('av%')).all()

    @classmethod
    def get_all_entities_by_agent_type(cls, agent_type):
        query = db.session.query(cls)
        return query.filter(cls.agent_type == agent_type).all()

    @classmethod
    def get_all_agent_types(cls):
        use_av = ModelSetting.get_bool('use_av')
        query = db.session.query(cls)
        if use_av == False:
            query = query.filter(not_(cls.agent_type.like('av%')))
        return query.group_by(cls.agent_type).order_by(func.count(cls.id).desc()).all()

    @classmethod
    def web_list(cls, req):
        try:
            ret = {}
            page = 1
            page_size = ModelSetting.get_int('item_per_page')
            job_id = ''
            search = ''
            category = ''
            if 'page' in req.form:
                page = int(req.form['page'])
            if 'search_word' in req.form:
                search = req.form['search_word']
            agent_type = req.form['agent_type'] if 'agent_type' in req.form else 'all'

            query = cls.make_query(search=search, agent_type=agent_type)
            count = query.count()
            query = query.limit(page_size).offset((page-1)*page_size)
            logger.debug('cls count:%s', count)
            lists = query.all()
            ret['list'] = [item.as_dict() for item in lists]
            ret['paging'] = Util.get_paging_info(count, page, page_size)
            return ret
        except Exception as e:
            logger.error('Exception:%s', e)
            logger.error(traceback.format_exc())

    @classmethod
    def make_query(cls, search='', agent_type='all', order='desc'):
        use_av = ModelSetting.get_bool('use_av')
        query = db.session.query(cls)
        if use_av == False: query = query.filter(not_(cls.agent_type.like('av%')))
        if search is not None and search != '':
            if search.find('|') != -1:
                tmp = search.split('|')
                conditions = []
                for tt in tmp:
                    if tt != '':
                        conditions.append(cls.title.like('%'+tt.strip()+'%') )
                query = query.filter(or_(*conditions))
            elif search.find(',') != -1:
                tmp = search.split(',')
                for tt in tmp:
                    if tt != '':
                        query = query.filter(cls.name.like('%'+tt.strip()+'%'))
            else:
                query = query.filter(or_(cls.name.like('%'+search+'%'), cls.name.like('%'+search+'%')))

        if agent_type != 'all':
            query = query.filter(cls.agent_type == agent_type)
        if order == 'desc':
            query = query.order_by(desc(cls.id))
        else:
            query = query.order_by(cls.id)
        return query