Example #1
0
class HaikuFindingThread(threading.Thread):
  def __init__(self, results_queue):
    self.results_queue = results_queue
    self.terminate_request_flag = False
    self.haiku_finder = HaikuFinder()
    self.untex_thread_class = UntexWrapper()
    self.last_parsed_id = get_last_parsed_id()
    threading.Thread.__init__(self)
    
  def terminate_request(self):
    self.terminate_request_flag = True
    
  def terminate(self):
    if not no_dictionary_update:
      logging.info("Saving dictionary")
      self.haiku_finder.save_dict()
    logging.info("Saving last_parsed_id")
    set_last_parsed_id(self.last_parsed_id)

  def run(self):
    for (article_id, raw_tex) in rssparse(arxiv_feed_url, self.last_parsed_id):
      if self.terminate_request_flag:
        critical("Worker thread caught terminate_request_flag, terminating")
        self.terminate()
        return
      
      self.last_parsed_id = article_id

      logging.info("Attempting raw tex from article_id: "+ str(article_id))
      
      if not raw_tex:
        error("Got empty raw_tex from article_id: " + str(article_id))
        continue
      
      haiku_list = []
      try:
        raw_text = self.untex_thread_class.run_untex(raw_tex)
        haiku_list = self.haiku_finder.find_haiku_in_text(raw_text)
      except RuntimeError as e:
        print_with_warning(str(e))
      if len(haiku_list)==0:
        logging.info("Found no Haiku in article_id: " + str(article_id))
      else:
        for haiku in haiku_list:
          self.results_queue.put(haiku + " (" + str(article_id) + ") #arXivHaiku")
          logging.info("Found haiku in article_id :" + str(article_id) + " : " + haiku)
    print_with_ok("Got to the end of the list :(")
    logging.info("Worker thread finished")
    self.terminate()
Example #2
0
 def __init__(self, results_queue):
   self.results_queue = results_queue
   self.terminate_request_flag = False
   self.haiku_finder = HaikuFinder()
   self.untex_thread_class = UntexWrapper()
   self.last_parsed_id = get_last_parsed_id()
   threading.Thread.__init__(self)