示例#1
0
    def get_filter(self):
        search_filter = Filter()

        # type filter
        def format_type(img_type):
            return ('itp:lineart' if img_type == 'linedrawing' else 'itp:' +
                    img_type)

        type_choices = ['photo', 'face', 'clipart', 'linedrawing', 'animated']
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        def format_color(color):
            if color in ['color', 'blackandwhite', 'transparent']:
                code = {
                    'color': 'color',
                    'blackandwhite': 'gray',
                    'transparent': 'trans'
                }
                return 'ic:' + code[color]
            else:
                return 'ic:specific,isc:{}'.format(color)

        color_choices = [
            'color', 'blackandwhite', 'transparent', 'red', 'orange', 'yellow',
            'green', 'teal', 'blue', 'purple', 'pink', 'white', 'gray',
            'black', 'brown'
        ]
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['large', 'medium', 'icon']:
                size_code = {'large': 'l', 'medium': 'm', 'icon': 'i'}
                return 'isz:' + size_code[size]
            elif size.startswith('>'):
                size_code = {
                    '400x300': 'qsvga',
                    '640x480': 'vga',
                    '800x600': 'svga',
                    '1024x768': 'xga',
                    '2mp': '2mp',
                    '4mp': '4mp',
                    '6mp': '6mp',
                    '8mp': '8mp',
                    '10mp': '10mp',
                    '12mp': '12mp',
                    '15mp': '15mp',
                    '20mp': '20mp',
                    '40mp': '40mp',
                    '70mp': '70mp',
                }
                return 'isz:lt,islt:' + size_code[size[1:]]
            elif size.startswith('='):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return 'isz:ex,iszw:{},iszh:{}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'large, medium, icon, >[]x[], =[]x[] ([] is an integer)')

        search_filter.add_rule('size', format_size)

        # licence filter
        license_code = {
            'noncommercial': 'f',
            'commercial': 'fc',
            'noncommercial,modify': 'fm',
            'commercial,modify': 'fmc'
        }

        def format_license(license):
            return 'sur:' + license_code[license]

        license_choices = list(license_code.keys())
        search_filter.add_rule('license', format_license, license_choices)

        # date filter
        def format_date(date):
            if date == 'pastday':
                return 'qdr:d'
            elif date == 'pastweek':
                return 'qdr:w'
            elif isinstance(date, tuple):
                assert len(date) == 2
                date_range = []
                for date_ in date:
                    if date_ is None:
                        date_str = ''
                    elif isinstance(date_, (tuple, datetime.date)):
                        date_ = datetime.date(
                            *date_) if isinstance(date_, tuple) else date_
                        date_str = date_.strftime('%m/%d/%Y')
                    else:
                        raise TypeError(
                            'date must be a tuple or datetime.date object')
                    date_range.append(date_str)
                return 'cdr:1,cd_min:{},cd_max:{}'.format(*date_range)
            else:
                raise TypeError(
                    'filter option "date" must be "pastday", "pastweek" or '
                    'a tuple of dates')

        search_filter.add_rule('date', format_date)

        return search_filter
示例#2
0
文件: bing.py 项目: hellock/icrawler
    def get_filter(self):
        search_filter = Filter()

        # type filter
        def format_type(img_type):
            prefix = '+filterui:photo-'
            return (prefix + 'animatedgif'
                    if img_type == 'animated' else prefix + img_type)

        type_choices = [
            'photo', 'clipart', 'linedrawing', 'transparent', 'animated'
        ]
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        def format_color(color):
            prefix = '+filterui:color2-'
            if color == 'color':
                return prefix + 'color'
            elif color == 'blackandwhite':
                return prefix + 'bw'
            else:
                return prefix + 'FGcls_' + color.upper()

        color_choices = [
            'color', 'blackandwhite', 'red', 'orange', 'yellow', 'green',
            'teal', 'blue', 'purple', 'pink', 'white', 'gray', 'black', 'brown'
        ]
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['large', 'medium', 'small']:
                return '+filterui:imagesize-' + size
            elif size == 'extralarge':
                return '+filterui:imagesize-wallpaper'
            elif size.startswith('>'):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return '+filterui:imagesize-custom_{}_{}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'extralarge, large, medium, small, >[]x[] '
                    '([] is an integer)')

        search_filter.add_rule('size', format_size)

        # licence filter
        license_code = {
            'creativecommons': 'licenseType-Any',
            'publicdomain': 'license-L1',
            'noncommercial': 'license-L2_L3_L4_L5_L6_L7',
            'commercial': 'license-L2_L3_L4',
            'noncommercial,modify': 'license-L2_L3_L5_L6',
            'commercial,modify': 'license-L2_L3'
        }

        def format_license(license):
            return '+filterui:' + license_code[license]

        license_choices = list(license_code.keys())
        search_filter.add_rule('license', format_license, license_choices)

        # layout filter
        layout_choices = ['square', 'wide', 'tall']
        search_filter.add_rule('layout', lambda x: '+filterui:aspect-' + x,
                               layout_choices)

        # people filter
        people_choices = ['face', 'portrait']
        search_filter.add_rule('people', lambda x: '+filterui:face-' + x,
                               people_choices)

        # date filter
        date_minutes = {
            'pastday': 1440,
            'pastweek': 10080,
            'pastmonth': 43200,
            'pastyear': 525600
        }

        def format_date(date):
            return '+filterui:age-lt' + str(date_minutes[date])

        date_choices = list(date_minutes.keys())
        search_filter.add_rule('date', format_date, date_choices)

        return search_filter
示例#3
0
    def get_filter(self):
        search_filter = Filter()

        # type filter
        type_code = {
            'portrait': 's=3&lm=0&st=-1&face=0',
            'face': 's=0&lm=0&st=-1&face=1',
            'clipart': 's=0&lm=0&st=1&face=0',
            'linedrawing': 's=0&lm=0&st=2&face=0',
            'animated': 's=0&lm=6&st=-1&face=0',
            'static': 's=0&lm=7&st=-1&face=0'
        }

        def format_type(img_type):
            return type_code[img_type]

        type_choices = list(type_code.keys())
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        color_code = {
            'red': 1,
            'orange': 256,
            'yellow': 2,
            'green': 4,
            'purple': 32,
            'pink': 64,
            'teal': 8,
            'blue': 16,
            'brown': 12,
            'white': 1024,
            'black': 512,
            'blackandwhite': 2048
        }

        def format_color(color):
            return 'ic={}'.format(color_code[color])

        color_choices = list(color_code.keys())
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['extralarge', 'large', 'medium', 'small']:
                size_code = {
                    'extralarge': 9,
                    'large': 3,
                    'medium': 2,
                    'small': 1
                }
                return 'z={}'.format(size_code[size])
            elif size.startswith('='):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return 'width={}&height={}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'extralarge, large, medium, small, >[]x[] '
                    '([] is an integer)')

        search_filter.add_rule('size', format_size)

        return search_filter
示例#4
0
    def get_filter(self):
        search_filter = Filter()

        # type filter
        def format_type(img_type):
            prefix = '+filterui:photo-'
            return (prefix + 'animatedgif'
                    if img_type == 'animated' else prefix + img_type)

        type_choices = [
            'photo', 'clipart', 'linedrawing', 'transparent', 'animated'
        ]
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        def format_color(color):
            prefix = '+filterui:color2-'
            if color == 'color':
                return prefix + 'color'
            elif color == 'blackandwhite':
                return prefix + 'bw'
            else:
                return prefix + 'FGcls_' + color.upper()

        color_choices = [
            'color', 'blackandwhite', 'red', 'orange', 'yellow', 'green',
            'teal', 'blue', 'purple', 'pink', 'white', 'gray', 'black', 'brown'
        ]
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['large', 'medium', 'small']:
                return '+filterui:imagesize-' + size
            elif size == 'extralarge':
                return '+filterui:imagesize-wallpaper'
            elif size.startswith('>'):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return '+filterui:imagesize-custom_{}_{}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'extralarge, large, medium, small, >[]x[] '
                    '([] is an integer)')

        search_filter.add_rule('size', format_size)

        # licence filter
        license_code = {
            'creativecommons': 'licenseType-Any',
            'publicdomain': 'license-L1',
            'noncommercial': 'license-L2_L3_L4_L5_L6_L7',
            'commercial': 'license-L2_L3_L4',
            'noncommercial,modify': 'license-L2_L3_L5_L6',
            'commercial,modify': 'license-L2_L3'
        }

        def format_license(license):
            return '+filterui:' + license_code[license]

        license_choices = list(license_code.keys())
        search_filter.add_rule('license', format_license, license_choices)

        # layout filter
        layout_choices = ['square', 'wide', 'tall']
        search_filter.add_rule('layout', lambda x: '+filterui:aspect-' + x,
                               layout_choices)

        # people filter
        people_choices = ['face', 'portrait']
        search_filter.add_rule('people', lambda x: '+filterui:face-' + x,
                               people_choices)

        # date filter
        date_minutes = {
            'pastday': 1440,
            'pastweek': 10080,
            'pastmonth': 43200,
            'pastyear': 525600
        }

        def format_date(date):
            return '+filterui:age-lt' + str(date_minutes[date])

        date_choices = list(date_minutes.keys())
        search_filter.add_rule('date', format_date, date_choices)

        return search_filter
示例#5
0
    def get_filter(self):
        search_filter = Filter()

        # type filter
        def format_type(img_type):
            return ('itp:lineart'
                    if img_type == 'linedrawing' else 'itp:' + img_type)

        type_choices = ['photo', 'face', 'clipart', 'linedrawing', 'animated']
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        def format_color(color):
            if color in ['color', 'blackandwhite', 'transparent']:
                code = {
                    'color': 'color',
                    'blackandwhite': 'gray',
                    'transparent': 'trans'
                }
                return 'ic:' + code[color]
            else:
                return 'ic:specific,isc:{}'.format(color)

        color_choices = [
            'color', 'blackandwhite', 'transparent', 'red', 'orange', 'yellow',
            'green', 'teal', 'blue', 'purple', 'pink', 'white', 'gray',
            'black', 'brown'
        ]
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['large', 'medium', 'icon']:
                size_code = {'large': 'l', 'medium': 'm', 'icon': 'i'}
                return 'isz:' + size_code[size]
            elif size.startswith('>'):
                size_code = {
                    '400x300': 'qsvga',
                    '640x480': 'vga',
                    '800x600': 'svga',
                    '1024x768': 'xga',
                    '2mp': '2mp',
                    '4mp': '4mp',
                    '6mp': '6mp',
                    '8mp': '8mp',
                    '10mp': '10mp',
                    '12mp': '12mp',
                    '15mp': '15mp',
                    '20mp': '20mp',
                    '40mp': '40mp',
                    '70mp': '70mp',
                }
                return 'isz:lt,islt:' + size_code[size[1:]]
            elif size.startswith('='):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return 'isz:ex,iszw:{},iszh:{}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'large, medium, icon, >[]x[], =[]x[] ([] is an integer)')

        search_filter.add_rule('size', format_size)

        # licence filter
        license_code = {
            'noncommercial': 'f',
            'commercial': 'fc',
            'noncommercial,modify': 'fm',
            'commercial,modify': 'fmc'
        }

        def format_license(license):
            return 'sur:' + license_code[license]

        license_choices = list(license_code.keys())
        search_filter.add_rule('license', format_license, license_choices)

        # date filter
        def format_date(date):
            if date == 'pastday':
                return 'qdr:d'
            elif date == 'pastweek':
                return 'qdr:w'
            elif isinstance(date, tuple):
                assert len(date) == 2
                date_range = []
                for date_ in date:
                    if date_ is None:
                        date_str = ''
                    elif isinstance(date_, (tuple, datetime.date)):
                        date_ = datetime.date(*date_) if isinstance(
                            date_, tuple) else date_
                        date_str = date_.strftime('%m/%d/%Y')
                    else:
                        raise TypeError(
                            'date must be a tuple or datetime.date object')
                    date_range.append(date_str)
                return 'cdr:1,cd_min:{},cd_max:{}'.format(*date_range)
            else:
                raise TypeError(
                    'filter option "date" must be "pastday", "pastweek" or '
                    'a tuple of dates')

        search_filter.add_rule('date', format_date)

        return search_filter
示例#6
0
文件: baidu.py 项目: hellock/icrawler
    def get_filter(self):
        search_filter = Filter()

        # type filter
        type_code = {
            'portrait': 's=3&lm=0&st=-1&face=0',
            'face': 's=0&lm=0&st=-1&face=1',
            'clipart': 's=0&lm=0&st=1&face=0',
            'linedrawing': 's=0&lm=0&st=2&face=0',
            'animated': 's=0&lm=6&st=-1&face=0',
            'static': 's=0&lm=7&st=-1&face=0'
        }

        def format_type(img_type):
            return type_code[img_type]

        type_choices = list(type_code.keys())
        search_filter.add_rule('type', format_type, type_choices)

        # color filter
        color_code = {
            'red': 1,
            'orange': 256,
            'yellow': 2,
            'green': 4,
            'purple': 32,
            'pink': 64,
            'teal': 8,
            'blue': 16,
            'brown': 12,
            'white': 1024,
            'black': 512,
            'blackandwhite': 2048
        }

        def format_color(color):
            return 'ic={}'.format(color_code[color])

        color_choices = list(color_code.keys())
        search_filter.add_rule('color', format_color, color_choices)

        # size filter
        def format_size(size):
            if size in ['extralarge', 'large', 'medium', 'small']:
                size_code = {
                    'extralarge': 9,
                    'large': 3,
                    'medium': 2,
                    'small': 1
                }
                return 'z={}'.format(size_code[size])
            elif size.startswith('='):
                wh = size[1:].split('x')
                assert len(wh) == 2
                return 'width={}&height={}'.format(*wh)
            else:
                raise ValueError(
                    'filter option "size" must be one of the following: '
                    'extralarge, large, medium, small, >[]x[] '
                    '([] is an integer)')

        search_filter.add_rule('size', format_size)

        return search_filter