def run(self): while True: scheduled_posts = ScheduledPost.read_config(self.sub) while True: try: self.o.refresh() sleep_time = float("inf") nextPost = None for p in scheduled_posts: if p.get_time_until_next_post() < sleep_time: sleep_time = p.get_time_until_next_post() nextPost = p print("sleep submission for", sleep_time, "s") if sleep_time == float("inf"): self.reschedule.wait() elif not self.reschedule.wait(sleep_time): Poster.lock.acquire() titleFormatted = nextPost.title titleFormatted = DATE_RE.sub(repl_date, titleFormatted) textFormatted = nextPost.text textFormatted = DATE_RE.sub(repl_date, textFormatted) submission = self.sub.submit(titleFormatted, textFormatted) submission.set_flair(flair_text=nextPost.flair_text, flair_css_class=nextPost.flair_css) if nextPost.distinguish: submission.distinguish() if nextPost.sticky: submission.sticky() if nextPost.contest_mode: submission.set_contest_mode(nextPost.contest_mode) print("submitted") Poster.lock.release() if self.reschedule.is_set(): self.reschedule.clear() break # Allows the bot to exit on ^C, all other exceptions are ignored except KeyboardInterrupt: print("We're almost done") break except Exception as e: print("Exception", e) import traceback traceback.print_exc() if self.weredone.is_set(): break
def run(self): while True: Poster.lock.acquire() scheduled_posts = [] for sub in self.subs: scheduled_posts.extend(ScheduledPost.read_config(sub)) Poster.lock.release() while True: try: Poster.lock.acquire() self.o.refresh() Poster.lock.release() sleep_time = float("inf") nextPost = None for p in scheduled_posts: if p.get_time_until_next_post() < sleep_time: sleep_time = p.get_time_until_next_post() nextPost = p nextPostNumber = nextPost.get_next_post_number() assert nextPostNumber >= 0 or sleep_time == float("inf") log.info("sleep submission for %s s(%s)", sleep_time, time.strftime("%d.%m.%Y %H:%M", time.localtime(nextPost.get_next_post_time()))) if sleep_time == float("inf"): self.reschedule.wait() elif not self.reschedule.wait(sleep_time): Poster.lock.acquire() titles = nextPost.title.split("\n") titleFormatted = titles[nextPostNumber % len(titles)] titleFormatted = DATE_RE.sub(repl_date, titleFormatted) textFormatted = nextPost.text if textFormatted is not None: textFormatted = DATE_RE.sub(repl_date, textFormatted) if nextPost.link is not None: links = nextPost.link.split("\n") link = links[nextPostNumber % len(links)] submission = nextPost.sub.submit(titleFormatted, url=link, resubmit=True) if textFormatted is not None and len(textFormatted) > 0: submission.add_comment(textFormatted) else: if textFormatted is None: textFormatted = "..." submission = nextPost.sub.submit(titleFormatted, textFormatted) submission.set_flair(flair_text=nextPost.flair_text, flair_css_class=nextPost.flair_css) if nextPost.distinguish: submission.distinguish() if nextPost.sticky: submission.sticky() if nextPost.contest_mode: submission.set_contest_mode(nextPost.contest_mode) log.info("submitted") Poster.lock.release() if self.reschedule.is_set(): self.reschedule.clear() break # Allows the bot to exit on ^C, all other exceptions are ignored except KeyboardInterrupt: log.info("We're almost done") break except Exception as e: log.error("Exception %s", e, exc_info=True) import traceback traceback.print_exc() Poster.lock.acquire(False) Poster.lock.release() if self.weredone.is_set(): break