def tweet_with_id(id): tweet = Tweet.for_id(id) if request.method == "GET": return jsonify(tweet=tweet.json_object()) elif request.method == "DELETE": response = Tweet.remove_with_id(id) if not response: return database_error_response() tweet_dict = response.json_object() return jsonify(tweet=tweet_dict)
def move(): from_index = request.body.get("from", "") to_index = request.body.get("to", "") if not from_index: from_index = (Tweet.count() - 1) if not to_index: to_index = 0 from_index = int(from_index) to_index = int(to_index) response = Tweet.move(from_index, to_index) if not response: return database_error_response() tweet_dict = response.json_object() return jsonify(tweet=tweet_dict)
def all_tweets(): if request.method == "GET": return jsonify(tweets=Tweet.all_dicts()) elif request.method == "POST": tweet = request.body.get("tweet", "") index = request.body.get("index", "") if not tweet: return Response("Give me content, ya dingus.", status=412) if not index: response = Tweet.add(h.unescape(tweet)) else: if not index.isdigit(): return Response("Index must be a number.", status=412) response = Tweet.insert_at_index(tweet, int(index)) if not response: return database_error_response() tweet_dict = response.json_object() return jsonify(tweet=tweet_dict)
def tweet_count(): return jsonify(count=Tweet.count())
def next_tweet(): tweet = Tweet.top() if not tweet: return Response("No tweets.", 200) return jsonify(tweet=tweet.json_object())
def tweet(twit): tweet_to_tweet = Tweet.top() if not tweet_to_tweet: return return (twit.statuses.update(status=tweet_to_tweet.content), tweet_to_tweet)
credentials = oauth_credentials() consumer_key = credentials["consumer_key"] consumer_secret = credentials["consumer_secret"] access_key = credentials["access_key"] access_secret = credentials["access_secret"] auth = twitter.OAuth(access_key, access_secret, consumer_key, consumer_secret) return twitter.Twitter(auth=auth) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-t", "--test", help="Don't tweet and print to standard out.", action="store_true") args = parser.parse_args() if Tweet.count() == 0: print("No tweets found.") exit(0) if args.test: print(Tweet.top().content) exit(0) twitter_client = authenticate() response, tweeted = tweet(twitter_client) if response: Tweet.pop() print("Tweeted: \"", tweeted.content, "\"", sep="") else: print("Could not send tweet.")