def closet(uid): user_timeline_count = current_app.redis.zcard('user:%s:timeline'%uid) since_id = int(request.args.get('since_id', user_timeline_count)) page = int(request.args.get('page', 1)) rev_since_id = user_timeline_count - since_id + (page-1)*PAGE_SIZE recent_postids = current_app.redis.zrevrange('user:%s:timeline'%uid, rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 user_timeline = get_timeline(recent_postids) fields = ['id', 'username', 'photo'] user = current_app.redis.hmget('user:%d'%uid, fields) user = dict(zip(fields, user)) if current_app.redis.zscore('user:%s:followed'%g.user['id'], uid): user['followed'] = True user['listing_count'] = current_app.redis.zcard('user:%s:timeline'%uid) or 0 user['following_count'] = current_app.redis.zcard('user:%s:following'%uid) or 0 user['followed_count'] = current_app.redis.zcard('user:%s:followed'%uid) or 0 if request.is_xhr: return jsonify(user=user, timeline=user_timeline, since_id=since_id, page=page) return render_template('closet.html', user=user, timeline=user_timeline, since_id=since_id, page=page)
def feed(): user_feed_count = current_app.redis.zcard('user:%s:feed'%g.user['id']) since_id = int(request.args.get('since_id', user_feed_count)) page = int(request.args.get('page', 1)) rev_since_id = user_feed_count - since_id + (page-1)*PAGE_SIZE recent_postids = current_app.redis.zrevrange('user:%s:feed'%g.user['id'], rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 feed_timeline = get_timeline(recent_postids, g.user) if request.is_xhr: return jsonify(timeline=feed_timeline, since_id=since_id, page=page) return render_template('feed.html', timeline=feed_timeline, since_id=since_id, page=page)
def likes(): likes_count = current_app.redis.zcard('user:%s:like'%g.user['id']) since_id = int(request.args.get('since_id', likes_count)) page = int(request.args.get('page', 1)) rev_since_id = likes_count - since_id + (page-1)*PAGE_SIZE user_like_id_list = current_app.redis.zrevrange('user_likes:%s'%g.user['id'], rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 user_likes = get_timeline(user_like_id_list) if request.is_xhr: return jsonify(likes=user_likes, since_id=since_id, page=page) return render_template('likes.html', likes=user_likes, since_id=since_id, page=page)
def public(): global_timeline_count = current_app.redis.zcard('global_timeline') since_id = int(request.args.get('since_id', global_timeline_count)) page = int(request.args.get('page', 1)) rev_since_id = global_timeline_count - since_id + (page-1)*PAGE_SIZE recent_postids = current_app.redis.zrevrange('global_timeline', rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 public_timeline = get_timeline(recent_postids, g.user) if request.is_xhr: return jsonify(timeline=public_timeline, since_id=since_id, page=page) return render_template('public.html', timeline=public_timeline, since_id=since_id, page=page)
def item(pid): item_comment_count = current_app.redis.zcard('post:%s:comment'%pid) since_id = int(request.args.get('since_id', item_comment_count)) page = int(request.args.get('page', 1)) rev_since_id = item_comment_count - since_id + (page-1)*PAGE_SIZE request_item = current_app.redis.hgetall('post:%s'%pid) if not request_item: abort(404) request_item['photos'] = json.loads(request_item['photos']) fields = ['id', 'username', 'photo'] user = current_app.redis.hmget('user:%s'%request_item['uid'], fields) user = dict(zip(fields, user)) request_item['user'] = user if current_app.redis.zscore('user:%s:like'%g.user['id'], pid): request_item['liked'] = True post_commentid_list = current_app.redis.zrevrange('post:%s:comment'%pid, rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 comments = [] for commentid in post_commentid_list: comments.append(json.loads(current_app.redis.hget('comment', commentid))) if request.is_xhr: return jsonify(request_item=request_item, user=user, comments=comments, since_id=since_id, page=page) user_postid_list = current_app.redis.zrange('user:%s:timeline'%user['id'], 0, 7) user_timeline = get_timeline(user_postid_list) return render_template('item.html', request_item=request_item, user=user, timeline=user_timeline, comments=comments, since_id=since_id, page=page)
def explore_brand(filter): page = int(request.args.get('page', 1)) category = request.args.get('category', None) since_id = 0 postid_list = [] if category: brand_count = current_app.redis.zcard('brand:%s:category:%s'%(filter, category)) since_id = request.args.get('since_id', brand_count) rev_since_id = brand_count - since_id + (page-1)*PAGE_SIZE postid_list = current_app.redis.zrevrange('brand:%s:category:%s'%(filter, category), rev_since_id, rev_since_id+PAGE_SIZE) else: brand_count = current_app.redis.zcard('brand:%s'%filter) since_id = request.args.get('since_id', brand_count) rev_since_id = brand_count - since_id + (page-1)*PAGE_SIZE postid_list = current_app.redis.zrevrange('brand:%s'%filter, rev_since_id, rev_since_id+PAGE_SIZE) page = page + 1 filtered_timeline = get_timeline(postid_list, g.user) if request.is_xhr: return jsonify(timeline=filtered_timeline, since_id=since_id, page=page) return render_template('explore.html', timeline=filtered_timeline, since_id=since_id, page=page)
def index(): recent_postids = current_app.redis.zrevrange('global_timeline', 0, PAGE_SIZE) public_timeline = get_timeline(recent_postids, g.user) return render_template('index.html', timeline=public_timeline)