def tweet_message(user, message): # save tweet tweet = {} tweet["tweet_id"] = new_id("tweets") tweet["user_id"] = user["user_id"] tweet["username"] = user["username"] tweet["datetime"] = Util.typemap(datetime.now()) tweet["message"] = message tweets.set(tweet["tweet_id"], dumps(tweet)) # save tweet index by creator/datetime --- to list the latest tweets tweeted by user index_tweets_creator_datetime.set( Util.composite(tweet["user_id"], tweet["datetime"]), tweet["tweet_id"]) # save tweet index by follower/datetime for each follower --- to list the latest tweets per stream follower_ids = get_follower_ids(tweet["user_id"]) for follower_id in follower_ids: index_tweets_follower_datetime.set( Util.composite(follower_id, tweet["datetime"]), tweet["tweet_id"]) # cache last 200 li = loads(cached_tweets.get(tweet["user_id"])) li.append(tweet["tweet_id"]) if len(li) > 200: li.pop() cached_tweets.set(tweet["user_id"], dumps(li)) # cache last 200 tweets for followers for follower_id in follower_ids: li = loads(cached_tweets.get(follower_id)) li.append(tweet["tweet_id"]) if len(li) > 200: li.pop() cached_tweets.set(follower_id, dumps(li))
def tweet_message(user, message): # save tweet tweet = {} tweet["tweet_id"] = new_id("tweets") tweet["user_id"] = user["user_id"] tweet["username"] = user["username"] tweet["datetime"] = Util.typemap(datetime.now()) tweet["message"] = message tweets.set(tweet["tweet_id"], dumps(tweet)) # save tweet index by creator/datetime --- to list the latest tweets tweeted by user index_tweets_creator_datetime.set(Util.composite(tweet["user_id"], tweet["datetime"]), tweet["tweet_id"]) # save tweet index by follower/datetime for each follower --- to list the latest tweets per stream follower_ids = get_follower_ids(tweet["user_id"]) for follower_id in follower_ids: index_tweets_follower_datetime.set(Util.composite(follower_id, tweet["datetime"]), tweet["tweet_id"]) # cache last 200 li = loads(cached_tweets.get(tweet["user_id"])) li.append(tweet["tweet_id"]) if len(li) > 200: li.pop() cached_tweets.set(tweet["user_id"], dumps(li)); # cache last 200 tweets for followers for follower_id in follower_ids: li = loads(cached_tweets.get(follower_id)) li.append(tweet["tweet_id"]) if len(li) > 200: li.pop() cached_tweets.set(follower_id, dumps(li))
print("%s's stream:" % user["username"]) tweet_ids = loads(cached_tweets.get(user["user_id"])) for tweet_id in tweet_ids: tweet = loads(tweets.get(tweet_id)) print("%s: %s" % (tweet["username"], tweet["message"])) print() truncate_all() mtrencseni = {} mtrencseni["user_id"] = new_id("users") mtrencseni["username"] = "******" mtrencseni["password"] = "******" mtrencseni["interests"] = "Foo and Bar!" mtrencseni["datetime"] = Util.typemap(datetime.now()) set_user(mtrencseni) agazso = {} agazso["user_id"] = new_id("users") agazso["username"] = "******" agazso["password"] = "******" agazso["interests"] = "Foo and Bar!" agazso["datetime"] = Util.typemap(datetime.now()) set_user(agazso) follow(mtrencseni, agazso) #follow(agazso, mtrencseni) tweet_message(mtrencseni, "Hello world!") tweet_message(mtrencseni, "Foo Bar")
def print_stream(user): print("%s's stream:" % user["username"]) tweet_ids = loads(cached_tweets.get(user["user_id"])) for tweet_id in tweet_ids: tweet = loads(tweets.get(tweet_id)) print("%s: %s" % (tweet["username"], tweet["message"])) print() truncate_all() mtrencseni = {} mtrencseni["user_id"] = new_id("users") mtrencseni["username"] = "******" mtrencseni["password"] = "******" mtrencseni["interests"] = "Foo and Bar!" mtrencseni["datetime"] = Util.typemap(datetime.now()) set_user(mtrencseni) agazso = {} agazso["user_id"] = new_id("users") agazso["username"] = "******" agazso["password"] = "******" agazso["interests"] = "Foo and Bar!" agazso["datetime"] = Util.typemap(datetime.now()) set_user(agazso) follow(mtrencseni, agazso) #follow(agazso, mtrencseni) tweet_message(mtrencseni, "Hello world!") tweet_message(mtrencseni, "Foo Bar")