第1关:创建用户与动态
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import time
import redis
conn = redis.Redis()
# 创建新用户
def create_user(login_name, real_name):
# 请在下面完成要求的功能
#********* Begin *********#
login_name = login_name.lower()
if conn.hget("users", login_name):
return None
uid = conn.incr("user:id")
pipe = conn.pipeline(True)
pipe.hset("users", login_name, uid)
pipe.hmset("user:%i"%(uid), {
'login_name': login_name,
'id': uid,
'real_name': real_name,
'followers': 0,
'following': 0,
'posts': 0,
'last_signup': time.time(),
})
pipe.execute()
return uid
#********* End *********#
# 为用户创建新动态
def create_post(uid, content):
# 请在下面完成要求的功能
#********* Begin *********#
pipe = conn.pipeline(True)
pipe.hget("user:%i"%(uid), 'login_name')
pipe.incr("post:id")
login_name, pid = pipe.execute()
if not login_name:
return None
pipe.hmset("post:%i"%(pid), {
'id': pid,
'uid': uid,
'content': content,
'posted': time.time(),
'user_name': login_name,
})
pipe.hincrby("user:%i"%(uid), 'posts')
pipe.execute()
return pid
#********* End *********#
第2关:处理用户关系
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import redis
conn = redis.Redis()
# 文本序列化
def tokenize(content):
# 请在下面完成要求的功能
#********* Begin *********#
words = set()
for word in re.findall("[a-z]{2,}", content.lower()):
if len(word) >= 2:
words.add(word)
return words
#********* End *********#
# 创建文本的反向索引
def index_document(content):
# 请在下面完成要求的功能
#********* Begin *********#
content_id = conn.incr("content:id")
conn.hset("contents", content_id, content)
words = tokenize(content)
pipeline = conn.pipeline(True)
for word in words:
pipeline.sadd('keyword:' + word, content_id)
pipeline.execute()
#********* End *********#
第3关:状态与信息流
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import time
import redis
conn = redis.Redis()
# 获得主页时间线
def get_home_timeline(uid, page=1, count=30):
# 请在下面完成要求的功能
#********* Begin *********#
post_ids = conn.zrevrange("home:%s"%(uid), 0, -1)
pipe = conn.pipeline(True)
for pid in post_ids:
pipe.hgetall("post:%s"%(pid))
return pipe.execute()
#********* End *********#
# 发布动态并将动态推送给粉丝
def post(uid, content):
# 请在下面完成要求的功能
#********* Begin *********#
pid = create_post(uid, content)
if not pid:
return None
posted = conn.hget("post:%s"%(pid), "posted")
conn.zadd("profile:%s"%(uid), pid, float(posted))
followers = conn.zrange("followers:%s"%(uid), 0, -1)
pipe = conn.pipeline(False)
for follower in followers:
pipe.zadd("home:%s"%(follower), pid, float(posted))
pipe.execute()
return pid
#********* End *********#
# 关注用户
def follow(uid, other_uid):
fkey1 = "following:%s"%(uid)
fkey2 = "followers:%s"%(other_uid)
if conn.zscore(fkey1, other_uid):
return None
now = time.time()
pipe = conn.pipeline(True)
pipe.zadd(fkey1, other_uid, now)
pipe.zadd(fkey2, uid, now)
following, followers = pipe.execute()
posts = conn.zrevrange("profile:%s"%(other_uid), 0, 100, withscores=True)
if posts:
pipe.zadd("home:%s"%(uid), **dict(posts))
pipe.hincrby("user:%s"%(uid), 'following', int(following))
pipe.hincrby("user:%s"%(other_uid), 'followers', int(followers))
pipe.execute()
return True
# 取消关注
def unfollow(uid, other_uid):
fkey1 = "following:%s"%(uid)
fkey2 = "followers:%s"%(other_uid)
if not conn.zscore(fkey1, other_uid):
return None
pipe = conn.pipeline(True)
pipe.zrem(fkey1, other_uid)
pipe.zrem(fkey2, uid)
following, followers = pipe.execute()
posts = conn.zrevrange("profile:%s"%(other_uid), 0, -1)
if posts:
pipe.zrem("home:%s"%(uid), *posts)
pipe.hincrby("user:%s"%(uid), 'following', -int(following))
pipe.hincrby("user:%s"%(other_uid), 'followers', -int(followers))
pipe.execute()
return True
# 创建新用户
def create_user(login_name, real_name):
login_name = login_name.lower()
if conn.hget("users", login_name):
return None
uid = conn.incr("user:id")
pipe = conn.pipeline(True)
pipe.hset("users", login_name, uid)
pipe.hmset("user:%i"%(uid), {
'login_name': login_name,
'id': uid,
'real_name': real_name,
'followers': 0,
'following': 0,
'posts': 0,
'last_signup': time.time(),
})
pipe.execute()
return uid
# 为用户创建新动态
def create_post(uid, content):
pipe = conn.pipeline(True)
pipe.hget("user:%i"%(uid), 'login_name')
pipe.incr("post:id")
login_name, pid = pipe.execute()
if not login_name:
return None
pipe.hmset("post:%i"%(pid), {
'id': pid,
'uid': uid,
'content': content,
'posted': time.time(),
'user_name': login_name,
})
pipe.hincrby("user:%i"%(uid), 'posts')
pipe.execute()
return pid