def do_main(modname, prio, iterable): """ Run the main function. """ # Setup logfile logfile = "%s/%s.log" % (get_logdir(), modname) logbook.FileHandler(logfile).push_application() log = logbook.Logger(modname) with log.catch_exceptions(): # Get the external state directory extdir = get_i3status_rundir() # Write out my own pid pidfile = "%s/%d%s.pid" % (extdir, prio, modname) with open(pidfile, "w") as fobj: fobj.write(str(os.getpid())) register_exit_signals() # File to write block info blockfile = "%s/%d%s.block" % (extdir, prio, modname) while True: for blocks in iterable: with aw.open(blockfile, "w") as fobj: json.dump(blocks, fobj) wake_i3status()
def set_background(screen, mode, cfg, statefile): """ Try to set the desktop background. """ # Location of the state file log.info("Getting image list ...") state_update(SYMB_GET_IMG_LIST, statefile) urls = [] for sub in cfg.wallpaper_subreddits: us = get_subreddit_images(sub, cfg.user_agent) urls.extend(us) urls = set(urls) # Load seen urls file log.info("Checking which urls have already been seen ...") if isfile(cfg.seenurls_fname): with open(cfg.seenurls_fname) as fobj: seen_urls = set(json.load(fobj)) else: seen_urls = set() # Create unseen urls list unseen_urls = urls - seen_urls if not unseen_urls: unseen_urls = urls unseen_urls = list(unseen_urls) random.shuffle(unseen_urls) for url in unseen_urls: seen_urls.add(url) # Create a unique filename for the url uhash = hashlib.sha1(url.encode("utf-8")).hexdigest() fname = "%s.%s" % (uhash, _getext(url)) fname = join(cfg.save_dir, fname) # Download the image state_update(SYMB_DOWNLOAD_IMG, statefile) if not download_image(url, fname): continue if not _is_image_file(fname): continue # Set background set_nitrogen_bg(cfg.nitrogen_conf_fname, screen, fname, mode) break # Dump updated seen urls log.info("Writing down list of checked urls ...") with aw.open(cfg.seenurls_fname, "w") as fobj: json.dump(sorted(seen_urls), fobj)
def state_update(text, fname): """ Write text to file atomically. Text may contain unicode. """ block = [{ "full_text": "{}: {}".format(SYMB_REDDIT, text), "color": COLORS.green }] with aw.open(fname, "w") as fobj: json.dump(block, fobj) wake_i3status()