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 parse_entry(article_id): src_link = src_link_url_base + article_id logging.info("Downloading: "+ src_link) try: (filename, headers) = urllib.urlretrieve(src_link) except IOError as e: print_with_fail("Couldn't retrieve " + src_link + "\t" + str(e)) return None content_type, content_encoding = (headers.get("Content-Type"), headers.get("Content-Encoding")) if content_type == "application/x-eprint-tar": logging.info("Recieved: application/x-eprint-tar, unpacking..") return (article_id, extract_from_tar(filename)) elif content_type == "application/x-eprint" and content_encoding == "x-gzip": return (article_id, extract_from_eprint_with_gzip(filename)) elif content_type == "application/pdf": logging.info("Recieved Content Type: application/pdf, not currently supported") return None else: logging.warning("Recieved Content Type: " + str(content_type) + "\n With Encoding: " + str(content_encoding) + "\n I don't even know what this is\n\n") return None
) 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")
def critical(msg): logging.critical(msg) print_with_fail(msg)
'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)