def consumer_(): """Consumer thread, tweets the book updates.""" con_schedule = Scheduler(config.TIMETABLE_TWI) if config.BOT_TYPE == 'DesktopBot': bot = DesktopBot() bot.sign_in(auth.user, auth.password) else: bot = TwitterAPIBot() bot.sign_in() while True: if con_schedule.is_time(): book = new_books.get() total_len = len(book[0]) + len(book[1]) + 1 if total_len > 140: book = (book[0][:-(total_len - 140)], book[1]) bot.tweet(book[0] + " " + book[1]) try: print " [ consumer@%s ] Tweet: %s" % (strftime("%H:%M:%S, %d/%m/%y"), (book[0] + " " + book[1])) except: print " [ consumer@%s ] Tweet." % strftime("%H:%M:%S, %d/%m/%y") with to_mark_lock: to_mark.append(book[1]) to_mark_lock.notify() sleep(config.TW_FREQ) else: sleep(60)
def producer_(): """Producer thread, checks the book pages.""" pro_schedule = Scheduler(config.TIMETABLE_SCRA) crawlers = [] for subcrawler in BookCrawler.__subclasses__(): crawlers.append(BookCrawler.factory(subcrawler.__name__)) while True: if pro_schedule.is_time(): for crawler in crawlers: books = crawler.get_books() for book in books.keys(): if is_new_book(book): # url of web page, book name, book url insert_book(crawler.get_url(), books[book], book) try: print " [ producer@%s ] New book: %s" % (strftime("%H:%M:%S, %d/%m/%y"), books[book] + \ " - " + \ crawler.get_url() + \ book) except: print " [ producer@%s ] New book." % strftime("%H:%M:%S, %d/%m/%y") new_books.put((books[book], crawler.get_url() + book)) with to_mark_lock: while not to_mark: to_mark_lock.wait() mark = to_mark.pop(0) mark_tweeted(mark) sleep(1) # Wait sleep(config.S_FREQ) else: sleep(60)