Exemplo n.º 1
0
def save_a_picture(pic_url, info, outfile):
    picture_content = webutil.loadurl(pic_url)
    if picture_content:
        with open(outfile, 'wb') as of:
            of.write(picture_content)
            _logger.info('file saved %s', outfile)
    return picture_content
Exemplo n.º 2
0
def save_a_picture(pic_url, info, outfile, optional=False):
    picture_content = webutil.loadurl(pic_url, optional=optional)
    if picture_content:
        with open(outfile, 'wb') as of:
            of.write(picture_content)
            _logger.info('file saved %s', outfile)
    return picture_content
Exemplo n.º 3
0
def download_wallpaper(run_config):
    idx = run_config.offset
    country_code = None if run_config.country == 'auto' else run_config.country
    market_code = None if not run_config.market else run_config.market
    try:
        s = bingwallpaper.BingWallpaperPage(idx, 
                country_code = country_code,
                market_code = market_code,
                high_resolution = bingwallpaper.HighResolutionSetting.getByName(
                    run_config.size_mode
                    ),
                resolution = run_config.image_size
                )
        _logger.debug(repr(s))
        s.load()
        _logger.log(log.PAGEDUMP, str(s))
    except Exception:
        _logger.fatal('error happened during loading from bing.com.', exc_info=1)
        return None

    if not s.loaded():
        _logger.fatal('can not load url %s. aborting...', s.url)
        return None
    for wplink, info in s.image_links():
        if wplink:
            outfile = get_output_filename(run_config, wplink)
            rec = record.default_manager.get(wplink, None)

            if rec and outfile == rec['local_file']:
                if not run_config.redownload:
                    _logger.info('file has been downloaded before, exit')
                    return None
                else:
                    _logger.info('file has been downloaded before, redownload it')

            _logger.info('download photo of "%s"', info)
            picture_content = webutil.loadurl(wplink)
            if picture_content:
                with open(outfile, 'wb') as of:
                    of.write(picture_content)
                    _logger.info('file saved %s', outfile)
                r = record.DownloadRecord(wplink, outfile)
                return r
        _logger.debug('no wallpaper, try next')
        
    if s.filtered > 0:
        _logger.info('%d picture(s) filtered, try again with -f option if you want them',
                     s.filtered)
    _logger.info('bad luck, no wallpaper today:(')
    return None
Exemplo n.º 4
0
def download_wallpaper(config):
    p = config.persistence
    s = ngphoto.NgPhotoPage(config.URL[0])
    while True:
        _logger.debug(repr(s))
        s.load()
        _logger.debug(str(s))
        if not s.loaded():
            _logger.fatal('can not load url %s. aborting...', s.url)
            sysexit(1)
        if not s.isphoto():
            _logger.fatal('%s is not a valid NG photo page.', s.url)
            sysexit(1)
        wplink = s.wallpaper_link()
        if not wplink and config.force:
            _logger.info('in force mode')
            wplink = s.img_link()

        if wplink:
            outfile = get_output_filename(config, wplink)
            rec = record.default_manager.get(wplink, None)

            if rec and outfile == rec['local_file']:
                if not config.redownload:
                    _logger.info('file has been downloaded before, exit')
                    sysexit(0)
                    return None
                else:
                    _logger.info('file has been downloaded before, redownload it')

            with open(outfile, 'wb') as of:
                _logger.info('download photo of "%s"', s.title())
                of.write(webutil.loadurl(wplink))
            _logger.info('file saved %s', outfile)
            r = record.DownloadRecord(wplink, outfile)
            return r
        p -= 1
        if p <= 0:
            _logger.info('bad luck, no wallpaper today:(')
            break
        _logger.debug('no wallpaper, try previous')
        s.update(s.prev_link())
    return None
Exemplo n.º 5
0
def download_wallpaper(config):
    idx = config.offset
    s = bingwallpaper.BingWallpaperPage(
        idx,
        country_code=config.country,
        high_resolution=bingwallpaper.HighResolutionSetting.getByName(
            config.size_mode))

    _logger.debug(repr(s))
    s.load()
    _logger.log(log.PAGEDUMP, str(s))
    if not s.loaded():
        _logger.fatal('can not load url %s. aborting...', s.url)
        return None
    for wplink, info in s.image_links():
        if wplink:
            outfile = get_output_filename(config, wplink)
            rec = record.default_manager.get(wplink, None)

            if rec and outfile == rec['local_file']:
                if not config.redownload:
                    _logger.info('file has been downloaded before, exit')
                    return None
                else:
                    _logger.info(
                        'file has been downloaded before, redownload it')

            _logger.info('download photo of "%s"', info)
            picture_content = webutil.loadurl(wplink)
            if picture_content:
                with open(outfile, 'wb') as of:
                    of.write(picture_content)
                    _logger.info('file saved %s', outfile)
                r = record.DownloadRecord(wplink, outfile)
                return r
        _logger.debug('no wallpaper, try next')

    if s.filtered > 0:
        _logger.info(
            '%d picture(s) filtered, try again with -f option if you want them',
            s.filtered)
    _logger.info('bad luck, no wallpaper today:(')
    return None
Exemplo n.º 6
0
def download_wallpaper(config):
    idx = config.offset
    p = config.persistence
    s = bingwallpaper.BingWallpaperPage(idx, p, filter_wp = not config.force, country_code = config.country)

    _logger.debug(repr(s))
    s.load()
    _logger.debug(str(s))
    if not s.loaded():
        _logger.fatal('can not load url %s. aborting...', s.url)
        return None
    for i in s.images():
        wplink = i['url']

        if wplink:
            outfile = get_output_filename(config, wplink)
            rec = record.default_manager.get(wplink, None)

            if rec and outfile == rec['local_file']:
                if not config.redownload:
                    _logger.info('file has been downloaded before, exit')
                    return None
                else:
                    _logger.info('file has been downloaded before, redownload it')

            with open(outfile, 'wb') as of:
                _logger.info('download photo of "%s"', i['copyright'])
                of.write(webutil.loadurl(wplink))
            _logger.info('file saved %s', outfile)
            r = record.DownloadRecord(wplink, outfile)
            return r
        _logger.debug('no wallpaper, try next')
        
    if s.filtered > 0:
        _logger.info('%d picture(s) filtered, try again with -f option if you want them',
                     s.filtered)
    _logger.info('bad luck, no wallpaper today:(')
    return None
Exemplo n.º 7
0
            raise Exception('use property "{}" before loading'.format(propname))

    def __str__(self):
        s_basic = '<url="{}", loaded={}'.format(self.url, self.isloaded())
        if not self.isloaded():
            return s_basic + '>'
        s_all = s_basic + ', images="{}">'.format(self.get_images())
        return s_all

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, repr(self.url))
    
    @staticmethod
    def validate_market(market_code):
        #
        if not re.match(r'\w\w-\w\w', market_code):
            raise ValueError('%s is not a valid market code.'%(market_code,))
        return True

if __name__ == '__main__':
    log.setDebugLevel(log.DEBUG)
    s = BingWallpaperPage()
    _logger.debug(repr(s))
    _logger.debug(str(s))
    s.load()
    _logger.debug(str(s))
    for i in s.get_images():
        l = i['url']
        with open(i['urlbase'].rpartition('/')[2]+'.jpg', 'wb') as of:
            of.write(webutil.loadurl(l))
Exemplo n.º 8
0
    
    @_property_need_loading
    def image_links(self):
        return self.wplinks

    def _assert_load(self, propname):
        if not self.loaded():
            raise Exception('use property "{}" before loading'.format(propname))

    def __str__(self):
        s_basic = '<url="{}", loaded={}'.format(self.url, self.loaded())
        if not self.loaded():
            return s_basic + '>'
        s_all   = s_basic + ', images="{}">'.format(self.images())
        return s_all

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, repr(self.url))

if __name__ == '__main__':
    log.setDebugLevel(log.DEBUG)
    s = BingWallpaperPage(0, 4)
    _logger.debug(repr(s))
    _logger.debug(str(s))
    s.load()
    _logger.debug(str(s))
    for i in s.images():
        l = i['url']
        with open(i['urlbase'].rpartition('/')[2]+'.jpg', 'wb') as of:
            of.write(webutil.loadurl(l))
Exemplo n.º 9
0
            return s_loaded + '>'
        s_all = s_loaded + ', img_link="{}", wallpaper_link="{}", prev_link="{}", title="{}">'.format(
            self.img_link(), self.wallpaper_link(), self.prev_link(),
            self.title())
        return s_all

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, repr(self.url))


from sys import argv
if __name__ == '__main__':
    p = 5
    _logger.setLevel = log.DEBUG
    s = NgPhotoPage(argv[1])
    while True:
        _logger.debug(repr(s))
        _logger.debug(str(s))
        s.load()
        _logger.debug(str(s))
        if s.wallpaper_link():
            with open('w.jpg', 'wb') as of:
                of.write(webutil.loadurl(s.wallpaper_link()))
            break
        p -= 1
        if p <= 0:
            _logger('bad luck, no wallpaper this week:(')
            break
        _logger.debug('no wallpaper, try previous')
        s.update(s.prev_link())
Exemplo n.º 10
0
        s_loaded = s_basic + ', isphoto={}'.format(self.isphoto())
        if not self.isphoto():
            return s_loaded + '>'
        s_all   = s_loaded + ', img_link="{}", wallpaper_link="{}", prev_link="{}", title="{}">'.format(
                self.img_link(), self.wallpaper_link(), self.prev_link(), self.title())
        return s_all

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__, repr(self.url))

from sys import argv
if __name__ == '__main__':
    p = 5
    _logger.setLevel = log.DEBUG
    s = NgPhotoPage(argv[1])
    while True:
        _logger.debug(repr(s))
        _logger.debug(str(s))
        s.load()
        _logger.debug(str(s))
        if s.wallpaper_link():
            with open('w.jpg', 'wb') as of:
                of.write(webutil.loadurl(s.wallpaper_link()))
            break
        p -= 1
        if p <= 0:
            _logger('bad luck, no wallpaper this week:(')
            break
        _logger.debug('no wallpaper, try previous')
        s.update(s.prev_link())