def download_wallpaper(run_config): records = list() 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 base_url = 'http://www.bing.com' if run_config.server == 'global' else \ 'http://s.cn.bing.net' if run_config.server == 'china' else \ run_config.customserver try: s = bingwallpaper.BingWallpaperPage( idx, base=base_url, country_code=country_code, market_code=market_code, high_resolution=bingwallpaper.HighResolutionSetting.getByName( run_config.size_mode), resolution=run_config.image_size, collect=set(run_config.collect)) _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.error('can not load url %s. aborting...', s.url) raise CannotLoadImagePage(s) for wplinks, metadata in s.image_links(): _logger.debug('%s photo list: %s', metadata, wplinks) mainlink = wplinks[0] copyright = metadata['copyright'] outfile = get_output_filename(run_config, mainlink) rec = record.default_manager.get_by_url(mainlink) _logger.debug('related download records: %s', rec) if 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"', copyright) raw = save_a_picture(mainlink, copyright, outfile) if not raw: continue r = record.DownloadRecord( mainlink, outfile, copyright, raw=None if run_config.database_no_image else raw, market=metadata['market']) records.append(r) collect_assets(wplinks[1:], metadata, run_config.output_folder, records) return records _logger.info('bad luck, no wallpaper today:(') return None
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.error('can not load url %s. aborting...', s.url) raise CannotLoadImagePage(s) for wplinks, info in s.image_links(): _logger.info('%s photo list: %s', info, wplinks) mainlink = wplinks[0] outfile = get_output_filename(run_config, mainlink) rec = record.default_manager.get_by_url(mainlink) _logger.debug('%s', rec) if 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) if not save_a_picture(mainlink, info, outfile): continue collect_accompanying_pictures(wplinks[1:], info, run_config.output_folder) r = record.DownloadRecord(mainlink, outfile) return r _logger.info('bad luck, no wallpaper today:(') return None
def collect_accompanying_pictures(wplinks, metadata, output_folder, records): copyright = metadata['copyright'] market = metadata['market'] for link in wplinks: filename = pathjoin(output_folder, basename(link)) _logger.info('download accompanying photo of "%s" from %s to %s', copyright, link, output_folder) raw = save_a_picture(link, copyright, filename) if raw: r = record.DownloadRecord( link, filename, copyright, raw=None if run_config.database_no_image else raw, is_accompany=True, market=market) records.append(r)
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
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
def collect_assets(wplinks, metadata, output_folder, records): copyright = metadata['copyright'] market = metadata['market'] for link in wplinks: _logger.debug('downloading assets of "%s" from %s to %s', copyright, link, output_folder) filename = pathjoin(output_folder, basename(link)) raw = save_a_picture(link, copyright, filename, optional=True) if not raw: continue elif filename.endswith('jpg'): r = record.DownloadRecord( link, filename, copyright, raw=None if run_config.database_no_image else raw, is_accompany=True, market=market) records.append(r) _logger.info('assets "%s" of "%s" has been downloaded to %s', link, copyright, output_folder)