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()
def process_incoming_haiku(haiku): while True: print_with_ok("Found something!") x = raw_input(haiku + "\nIs this a haiku (y/n)?").strip().lower() if x == "y": post_haiku_to_the_world (get_text_with_vi(haiku, "\n Edit Haiku to post to twitter.")) return elif x == "n": return else: print "Didn't get Y or N"
def post_haiku_to_the_world(haiku): if (post_to_twitter): (success, error) = twitter.post_status_to_twitter(haiku) if(success): print_with_ok ("Tweet Successful!") else: print_with_fail("Tweet Failed: " + str(error)) if (post_to_facebook): (success, error) = facebook.post_to_facebook(haiku) if(success): print_with_ok ("Post to facebook Successful!") else: print_with_fail("Post to facebook Failed: " + str(error))
def main(): results_queue = Queue.Queue() haiku_finding_thread = HaikuFindingThread(results_queue=results_queue) haiku_finding_thread.start() print_with_ok("Started haiku finding thread...") try: while(haiku_finding_thread.is_alive()): try: haiku = results_queue.get(block=True,timeout=1) #Need such a timeout so KeyboardInterrupt works! except Queue.Empty as e: continue process_incoming_haiku(haiku) except KeyboardInterrupt as e: print "Caught KeyboardInterrupt, Attempting to terminate worker thread" logging.critical("Caught KeyboardInterrupt, Attempting to terminate worker thread") haiku_finding_thread.terminate_request()
) return (resp,content) #Post status to twitter, returns tuple of (success, error_message) def post_status_to_twitter(status): logging.info("Attempting to tweet: "+status) content = oauth2_request( 'https://api.twitter.com/1.1/statuses/update.json', app_token, app_token_secret, http_method="POST", post_body="status="+htmlescape(status), ) if content[0]['status'] != '200': logging.warning("Tweet unsuccessful. Error message: " + str(content)) return (False, content) logging.info("Tweet successful.") return (True, None) if __name__ == "__main__": logging.basicConfig(filename="../logs/twitter.log", level=logging.DEBUG) print "Testing by posting twice to twitter, the first should succeed and the second fail for posting same status twice.\n" (success1, error1) = post_status_to_twitter("Blah Blah Haiku") (success2, error2) = post_status_to_twitter("Blah Blah Haiku") if success1 and not success2: print_with_ok("Test Successful\n") else: print_with_fail("Test Unsuccessful\n") print_with_warning("You need to delete the last post off twitter now...\n")
'access_token': access_token}))) if "id" in json.loads(response): return (True, None) else: return (False, str(json.loads(response))) def post_to_facebook(message): try: page_access_token = get_page_access_token(long_term_token) except AuthenticationException as e: return (False, e.msg) return post(page_access_token, arXivHaiku_page_id, message) if __name__ == "__main__": print """If auth fails because the long term token is out of date go to https://developers.facebook.com/tools/explorer/ to get a new short term token (select the app arXivHaiku then choose "get access token". Then replace short_term_token and run exchange_token to get a new 60 day token.""" try: page_access_token = get_page_access_token(long_term_token) except AuthenticationException as e: terminalcolours.print_with_fail(e.msg) exit(2) success, error = post(page_access_token, arXivHaiku_page_id, "We say that C1, C4 form a K-cluster, or K-quadruple, (1505.03121) #arXivHaiku") if success: terminalcolours.print_with_ok("Posted to facebook") else: terminalcolours.print_with_fail(error) exit(2)