def get(self): page = self.get_argument("page", 1) per_page = self.get_argument("per_page", 10) per_page = int(per_page) page = int(page) entity_type = self.get_argument("entity_type", 'share') user_id = self.current_user["user_id"] assert entity_type in 'share comment viewpoint'.split() cond = { 'user_id': user_id, 'entity_type': entity_type, 'likenum': 1, } # number = Like.find(cond, {'_id': 0}).count() collects = Like.find(cond, { '_id': 0 }).sort('_id', -1).limit(per_page).skip((page - 1) * per_page) res = [] for collect in collects: # 'status': {'$gte': 1}, share = Share.find_one({'id': collect.id}, {'_id': 0}) share = fix_share(share) res.append(share) self.res = {'articles': res} # number=number return self.write_json()
def get(self): user_id = self.current_user["user_id"] if user_id not in admin_ids: self.write_json() return share_num = Share.find().count() share_state1_num = Share.find({'status': {'$gte': 1}}).count() share_with_tag_num = share_num - Share.find({'tags': []}).count() share_state1_with_tag_num = share_state1_num - \ Share.find({'status': {'$gte': 1}, 'tags': []}).count() user_num = User.find().count() user_rss_num = User.find({'user_rss': {'$ne': ''}}).count() like_num = Like.find().count() # todo hit_num = Hit.find().count() # todo hit_num_in_5_day = Hit.find({'hittime': {'$gt': time.time()-86400*5}}).count() # 日活 self.res = { 'share_num': share_num, 'share_state1_num': share_state1_num, 'share_with_tag_num': share_with_tag_num, 'share_state1_with_tag_num': share_state1_with_tag_num, 'user_num': user_num, 'user_rss_num': user_rss_num, 'like_num': like_num, 'hit_num': hit_num, 'hit_num_in_5_day': hit_num_in_5_day, } self.write_json()
def get(self, slug): share = get_share_by_slug(slug) if not share: return self.write_error(404) # 小程序客户端 # 时间格式转换 share.published = int(share.published * 1000) share.updated = int(share.updated * 1000) # 暂时不显示作者 user_id = self.current_user["user_id"] if self.current_user else None # if user_id: # like = Like.find_one( # {'share_id': share.id, 'user_id': user_id}) # collect = Collect.find_one( # {'share_id': share.id, 'user_id': user_id}) like = Like.find_one({ 'entity_id': share.id, 'entity_type': 'share', 'user_id': user_id }) collect = Collect.find_one({ 'entity_id': share.id, 'entity_type': 'share', 'user_id': user_id }) d_share = dict(share) d_share.pop('content') d_share['is_liking'] = bool(like.likenum) if like else False d_share['is_disliking'] = bool(like.dislikenum) if like else False d_share['is_collecting'] = bool(like.collectnum) if collect else False # 对于链接分享类,增加原文预览 if d_share.get('link'): # Webcache should add index doc = Webcache.find_one({'url': d_share['link']}, {'_id': 0}) if doc and doc['markdown'] and '禁止转载' not in doc['markdown']: doc['markdown'] = doc['markdown'].replace('本文授权转自', '') # d_share['markdown'] += '\n\n--预览--\n\n' + doc['markdown'] d_share['markdown'] += '\n\n--预览(快照)(以后会默认折叠)--\n\n' + doc[ 'markdown'] d_share['markdown'] += '\n\n[阅读原文]({})'.format(doc['url']) # fix md parse d_share['markdown'] = d_share['markdown'].replace('>\n\n', '') d_share['markdown'] = d_share['markdown'].replace('\n]', ']') d_share['markdown'] = d_share['markdown'].replace( '.jpg#)', '.jpg)') # 添加原文链接 d_share['url'] = '预览: <a href="{}">{}</a>'.format( share.link, share.title) viewpoints = Viewpoint.find({'share_id': share.id}, {'_id': 0}) d_share['viewpoints'] = list(viewpoints) d_share['title'] = d_share['title'].split('_')[0] self.res = d_share add_hit_stat(user_id, share) self.write_json()
def post(self, action): entity_id = int(self.get_argument("entity_id", 0)) entity_type = self.get_argument("entity_type", None) user_id = self.current_user["user_id"] assert action in 'addlike dellike adddislike deldislike'.split() assert entity_type in 'share comment viewpoint tag'.split() _action = action[3:] + 'num' doc = { 'user_id': user_id, 'entity_id': entity_id, 'entity_type': entity_type, } is_changed = Like.change_like(doc, _action, action[:3]) # 冗余储存 没有做成事件绑定,需要定期校验修复 if entity_type == 'share': entity = Share.by_sid(entity_id) # 如果是管理员,需要将status + 1 # 64=kp 65=kp email # 63=lb # 60=xie if is_changed and user_id in admin_ids: if action == 'addlike': if entity['status'] == 0: entity['suggested'] = time.time() entity['status'] += 1 elif action == 'adddislike': entity['status'] -= 1 elif action == 'deldislike': entity['status'] += 1 else: entity['status'] -= 1 elif entity_type == 'comment': entity = Comment.by_sid(entity_id) elif entity_type == 'viewpoint': entity = Viewpoint.by_sid(entity_id) elif entity_type == 'tag': entity = Tag.by_sid(entity_id) user = User.by_sid(user_id) if action == 'addlike' and entity.name not in user.user_tags: user.user_tags.append(entity.name) elif action == 'dellike' and entity.name in user.user_tags: user.user_tags.pop(entity.name) user.save() if action[:3] == 'add': entity[_action] += 1 else: entity[_action] -= 1 entity.save() if entity.dislikenum < 0: entity.dislikenum = 0 if entity.likenum < 0: entity.likenum = 0 self.res = { 'likenum': entity.likenum, 'dislikenum': entity.dislikenum, } self.write_json()
def get(self, name): user = User.find_one({"user_domain": name}) user.user_jointime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(user.user_jointime)) like_res = Like.find({"user_id": user.id}) likenum = like_res.count() likes = [] for like in like_res: share = Share.by_sid(like.share_id) like.title = share.title like.id = share.id like.type = share.sharetype likes.append(like) user.gravatar = get_avatar(user.user_email, 100) self.render("userlike.html", user=user, likenum=likenum, likes=likes)
def get(self): page = self.get_argument("page", 1) per_page = self.get_argument("per_page", 10) per_page = int(per_page) page = int(page) entity_type = self.get_argument("entity_type", 'share') user_id = self.current_user["user_id"] assert entity_type in 'share comment viewpoint'.split() cond = { 'user_id': user_id, 'entity_type': entity_type, 'likenum': 1, } number = Like.find(cond, {'_id': 0}).count() collects = Like.find(cond, {'_id': 0}).sort( '_id', -1).limit(per_page).skip((page - 1) * per_page) res = [] for collect in collects: # 'status': {'$gte': 1}, share = Share.find_one({'id': collect.id}, {'_id': 0}) share = fix_share(share) res.append(share) self.res = res return self.write_json(number=number)
def get(self, name): user = User.find_one({'user_domain': name}) user.user_jointime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(user.user_jointime)) like_res = Like.find({'user_id': user.id}) likenum = like_res.count() likes = [] for like in like_res: share = Share.by_sid(like.share_id) like.title = share.title like.id = share.id like.type = share.sharetype likes.append(like) user.gravatar = get_avatar(user.user_email, 100) self.render('userlike.html', user=user, likenum=likenum, likes=likes)
def post(self, action): share_id = int(self.get_argument("share_id", None)) user_id = self.current_user["user_id"] doc = { 'user_id': user_id, 'share_id': share_id } newlikes = None if action == 'addlike': Like.change_like(doc, 'likenum') share = Share.by_sid(share_id) share.likenum += 1 share.save() user = User.by_sid(share.user_id) user.user_leaf += 4 user.save() user = User.by_sid(user_id) user.user_leaf += 2 user.save() newlikes = str(share.likenum) elif action == 'dellike': Like.change_like(doc, 'likenum') share = Share.by_sid(share_id) share.likenum -= 1 share.save() user = User.by_sid(share.user_id) user.user_leaf -= 4 user.save() user = User.by_sid(user_id) user.user_leaf -= 2 user.save() newlikes = str(share.likenum) elif action == 'adddislike': Like.change_like(doc, 'dislikenum') share = Share.by_sid(share_id) share.dislikenum += 1 share.save() newlikes = str(share.dislikenum) elif action == 'deldislike': Like.change_like(doc, 'dislikenum') share = Share.by_sid(share_id) share.dislikenum -= 1 share.save() newlikes = str(share.dislikenum) self.write(newlikes)
def post(self, action): entity_id = int(self.get_argument("entity_id", 0)) entity_type = self.get_argument("entity_type", None) user_id = self.current_user["user_id"] assert action in 'addlike dellike adddislike deldislike'.split() assert entity_type in 'share comment viewpoint'.split() _action = action[3:] + 'num' doc = { 'user_id': user_id, 'entity_id': entity_id, 'entity_type': entity_type, } is_changed = Like.change_like(doc, _action, action[:3]) # 冗余储存 没有做成事件绑定,需要定期校验修复 if entity_type == 'share': entity = Share.by_sid(entity_id) # 如果是管理员,需要将status + 1 # 64=kp 65=kp email # 63=lb # 60=xie if is_changed and user_id in admin_ids: if action == 'addlike': entity['status'] += 1 elif action == 'adddislike': entity['status'] -= 1 elif action == 'deldislike': entity['status'] += 1 else: entity['status'] -= 1 elif entity_type == 'comment': entity = Comment.by_sid(entity_id) elif entity_type == 'viewpoint': entity = Viewpoint.by_sid(entity_id) if action[:3] == 'add': entity[_action] += 1 else: entity[_action] -= 1 entity.save() self.res = { 'likenum': entity.likenum, 'dislikenum': entity.dislikenum, } self.write_json()
def get(self, slug): share = get_share_by_slug(slug) if not share: return self.write_error(404) # 小程序客户端 # 时间格式转换 share.published = int(share.published * 1000) share.updated = int(share.updated * 1000) # 暂时不显示作者 user_id = self.current_user["user_id"] if self.current_user else None # if user_id: like = Like.find_one( {'share_id': share.id, 'user_id': user_id}) collect = Collect.find_one( {'share_id': share.id, 'user_id': user_id}) d_share = dict(share) d_share['is_liking'] = bool(like.likenum) if like else False d_share['is_disliking'] = bool(like.dislikenum) if like else False d_share['is_collecting'] = bool(like.collectnum) if collect else False # 对于链接分享类,增加原文预览 if d_share.get('link'): # Webcache should add index doc = Webcache.find_one({'url': d_share['link']}, {'_id': 0}) if doc and doc['markdown'] and '禁止转载' not in doc['markdown']: doc['markdown'] = doc['markdown'].replace('本文授权转自', '') d_share['markdown'] += '\n\n--预览--\n\n' + doc['markdown'] d_share['markdown'] += '\n\n[阅读原文]({})'.format(doc['url']) # 添加原文链接 d_share['url'] = '预览: <a href="{}">{}</a>'.format( share.link, share.title) viewpoints = Viewpoint.find({'share_id': share.id}, {'_id': 0}) d_share['viewpoints'] = list(viewpoints) d_share['title'] = d_share['title'].split('_')[0] self.res = d_share self.write_json() add_hit_stat(user_id, share)
def get(self, slug): share = None if slug.isdigit(): share = Share.by_sid(slug) else: share = Share.by_slug(slug) if share: share.hitnum += 1 share.save() share.markdown = markdown2.markdown(share.markdown) user = User.by_sid(share.user_id) share.user_name = user.user_name share.user_domain = user.user_domain tags = '' if share.tags: tags += 'tags:' for i in share.tags.split(' '): tags += '<a href="/tag/%s">%s</a> ' % (i, i) share.tags = tags user_id = int( self.current_user["user_id"]) if self.current_user else None like = Like.find_one( {'share_id': share.id, 'user_id': user_id}) share.is_liking = bool(like.likenum % 2) if like else None share.is_disliking = bool(like.dislikenum % 2) if like else None comments = [] comment_res = Comment.find({'share_id': share.id}) for comment in comment_res: user = User.by_sid(comment.user_id) comment.name = user.user_name comment.domain = user.user_domain comment.gravatar = get_avatar(user.user_email, 50) comments.append(comment) if user_id: hit = Hit.find( {'share_id': share.id}, {'user_id': int(self.current_user["user_id"])}, ) if hit.count() == 0: hit = Hit hit['share_id'] = share.id hit['user_id'] = int(self.current_user["user_id"]) hit.save() else: if not self.get_cookie(share.id): self.set_cookie(str(share.id), "1") posts = Share.find() suggest = [] for post in posts: post.score = 100 + post.id - post.user_id + post.commentnum * 3 post.score += post.likenum * 4 + post.hitnum * 0.01 post.score += randint(1, 999) * 0.001 common_tags = [i for i in post.tags.split( ' ') if i in share.tags.split(' ')] # list(set(b1) & set(b2)) post.score += len(common_tags) if post.sharetype == share.sharetype: post.score += 5 if self.current_user: is_hitted = Hit.find( {'share_id': share._id}, {'user_id': int(self.current_user["user_id"])}, ).count() > 0 else: is_hitted = self.get_cookie(share.id) if is_hitted: post.score -= 50 suggest.append(post) suggest.sort(key=lambda obj: obj.get('score')) suggest = suggest[:5] self.render( "sharee.html", share=share, comments=comments, suggest=suggest) else: old = 'http://blog.anwensf.com/' for i in options.old_links: if slug in i: self.redirect('%s%s' % (old, i), permanent=True) break return self.redirect("/404")
def get(self, slug): share = get_share_by_slug(slug) if not share: return self.write_error(404) # for web user = User.by_sid(share.user_id) share.author_name = user.user_name share.author_domain = user.user_domain share.tags = format_tags(share) share.title = share.title.split('_')[0] if share.markdown: md = share.markdown md = md.replace('>\n', '> ') share.content = markdown2.markdown(md) # 对于链接分享类,增加原文预览 if share.sharetype == 'rss': assert share.link if share.link: if share.sharetype == 'rss': pass else: # Webcache should add index doc = Webcache.find_one({'url': share.link}, {'_id': 0}) # 此文章须经作者同意 转载 禁止转载 # 禁止任何形式的转载 # ('禁止' not in doc['markdown'] and '转载' not in doc['markdown']): if doc and doc['markdown'] and ('禁止转载' not in doc['markdown'] or '禁止任何形式的转载' not in doc['markdown']): doc['markdown'] = doc['markdown'].replace('本文授权转自', '') md = share['markdown'] md += '\n\n--预览(快照)(以后会默认折叠)--\n\n' + doc['markdown'] md += '\n\n[阅读原文]({})'.format(doc['url']) parsed_uri = urlparse(share.link) domain = '{uri.scheme}://{uri.netloc}/'.format( uri=parsed_uri) md = md.replace('![image](/', '![image]({}/'.format(domain)) md = md.replace('\n* \n', '\n\n') md = md.replace('\n*\n', '\n\n') md = md.replace('>\n', '> ') # md = md.replace('>\n\n', '') # ??? while '\n\n\n' in md: md = md.replace('\n\n\n', '\n\n') share.content = markdown2.markdown(md) user_id = self.current_user["user_id"] if self.current_user else None # user_id like = Like.find_one({ 'entity_id': share.id, 'user_id': user_id, 'entity_type': 'share' }) collect = Collect.find_one({ 'entity_id': share.id, 'user_id': user_id, 'entity_type': 'share' }) share.is_liking = bool(like.likenum) if like else False share.is_disliking = bool(like.dislikenum) if like else False share.is_collecting = bool(collect.collectnum) if collect else False # logger.info('user_id: {}'.format(user_id)) # logger.info('share.is_liking: {}'.format(share.is_liking)) suggest = [] # is_liking = db.getCollection('Like_Col').find({'entity_id':1,'entity_type':'share','user_id':1}) if user_id: likes = Like.find({'user_id': user_id, 'entity_type': 'comment'}) likes = list(likes) like_commentids = [ alike.entity_id for alike in likes if alike.likenum > 0 ] dislike_commentids = [ alike.entity_id for alike in likes if alike.dislikenum > 0 ] else: like_commentids = [] dislike_commentids = [] comments = get_comments(share, like_commentids, dislike_commentids) share.viewpoints = Viewpoint.find({'share_id': share.id}) # 未登录用户记录访问cookie if not user_id and not self.get_cookie(share.id): self.set_cookie(str(share.id), "1") self.render("share_view.html", share=share, comments=comments, suggest=suggest) add_hit_stat(user_id, share) return # 文章推荐 suggest = [] posts = Share.find() for post in posts: post.score = 100 + post.id - post.user_id # post.score += post.likenum * 4 + post.hitnum * 0.01 + post.commentnum * 3 post.score += randint(1, 999) * 0.001 common_tags = [ i for i in post.tags.split(' ') if i in share.tags.split(' ') ] # list(set(b1) & set(b2)) post.score += len(common_tags) if post.sharetype == share.sharetype: post.score += 1 # todo if self.current_user: is_hitted = Hit.find( { 'share_id': share.id }, { 'user_id': int(self.current_user["user_id"]) }, ).count() > 0 else: is_hitted = self.get_cookie(share.id) if is_hitted: post.score -= 50 suggest.append(post) suggest.sort(key=lambda obj: obj.get('score')) suggest = suggest[:5]
def get(self): page = self.get_argument("page", 1) per_page = self.get_argument("per_page", 10) per_page = int(per_page) page = int(page) entity_type = self.get_argument("entity_type", 'share') user_id = self.current_user["user_id"] assert entity_type in 'share comment viewpoint'.split() # token = self.request.headers.get('Authorization', '') # user = self.get_user_dict(token) meta_info = self.get_argument("meta_info", 1) cond = { 'user_id': user_id, 'entity_type': entity_type, 'likenum': 1, } likes = Like.find(cond, { '_id': 0 }).sort('_id', -1).limit(per_page).skip((page - 1) * per_page) new_shares = [] filter_d = {} filter_d['_id'] = 0 # 白名单里的属性才展示 filter_d['id'] = 1 filter_d['images'] = 1 filter_d['title'] = 1 filter_d['user_id'] = 1 filter_d['tags'] = 1 filter_d['published'] = 1 filter_d['post_img'] = 1 for like in likes: # 'status': {'$gte': 1}, {'_id': 0} share = Share.find_one({'id': like.id}, filter_d) user = User.by_sid(share.user_id) share['author'] = user.user_name share['type'] = 1 if share.get('post_img'): share['type'] = 2 share['images'] = [ IMG_BASE + share['post_img'].replace('_1200.jpg', '_260.jpg') ] share.pop('post_img') else: share['images'] = [] share['published'] = int(share['published'] * 1000) new_shares.append(share) if meta_info: meta = {} number = Like.find(cond, {'_id': 0}).count() meta['number'] = number self.res = {'articles': new_shares} self.meta = meta return self.write_json()
def get(self, slug): share = None if slug.isdigit(): share = Share.by_sid(slug) else: share = Share.by_slug(slug) if share: share.hitnum += 1 share.save() if share.markdown: share.content = markdown2.markdown(share.markdown) user = User.by_sid(share.user_id) share.user_name = user.user_name share.user_domain = user.user_domain tags = '' if share.tags: tags += 'tags:' for i in share.tags.split(' '): tags += '<a href="/tag/%s">%s</a> ' % (i, i) share.tags = tags user_id = int( self.current_user["user_id"]) if self.current_user else None like = Like.find_one( {'share_id': share.id, 'user_id': user_id}) share.is_liking = bool(like.likenum % 2) if like else None share.is_disliking = bool(like.dislikenum % 2) if like else None comments = [] comment_res = Comment.find({'share_id': share.id}) for comment in comment_res: user = User.by_sid(comment.user_id) comment.name = user.user_name comment.domain = user.user_domain comment.gravatar = get_avatar(user.user_email, 50) comments.append(comment) if user_id: hit = Hit.find( {'share_id': share.id}, {'user_id': int(self.current_user["user_id"])}, ) if hit.count() == 0: hit = Hit hit['share_id'] = share.id hit['user_id'] = int(self.current_user["user_id"]) hit.save() else: if not self.get_cookie(share.id): self.set_cookie(str(share.id), "1") posts = Share.find() suggest = [] for post in posts: post.score = 100 + post.id - post.user_id + post.commentnum * 3 post.score += post.likenum * 4 + post.hitnum * 0.01 post.score += randint(1, 999) * 0.001 common_tags = [i for i in post.tags.split( ' ') if i in share.tags.split(' ')] # list(set(b1) & set(b2)) post.score += len(common_tags) if post.sharetype == share.sharetype: post.score += 5 if self.current_user: is_hitted = Hit.find( {'share_id': share._id}, {'user_id': int(self.current_user["user_id"])}, ).count() > 0 else: is_hitted = self.get_cookie(share.id) if is_hitted: post.score -= 50 suggest.append(post) suggest.sort(key=lambda obj: obj.get('score')) suggest = suggest[:5] self.render( "sharee.html", share=share, comments=comments, suggest=suggest) else: old = 'http://blog.anwensf.com/' for i in options.old_links: if slug in i: self.redirect('%s%s' % (old, i), permanent=True) break return self.redirect("/404")
def get(self, name): user = User.find_one({'user_domain': name}) user.user_say = markdown2.markdown(user.user_say) user.user_jointime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(user.user_jointime)) likenum = User.find({'user_id': user._id}).count() user.gravatar = get_avatar(user.user_email, 100) shares = Share.find({ 'user_id': user.id }, { 'markdown': 0, 'summary': 0 }).sort('_id', -1).limit(100) likes = set() dislikes = set() collects = set() if self.current_user: user_id = self.current_user["user_id"] _likes = Like.find({ 'entity_type': 'share', 'user_id': user_id }, { '_id': 0, 'entity_id': 1, 'likenum': 1, 'dislikenum': 1 }) _likes = list(_likes) print(_likes[0]) likes = set(i.entity_id for i in _likes if i.likenum > 0) dislikes = set(i.entity_id for i in _likes if i.dislikenum > 0) collects = Collect.find( { 'entity_type': 'share', 'user_id': user_id }, { '_id': 0, 'entity_id': 1, 'collectnum': 1 }) collects = set(i.entity_id for i in collects if i.collectnum > 0) l_share = [] print(shares[0]) for share in shares: # d_share = dict(share) # d_share = share # if self.current_user: # user_id = self.current_user["user_id"] # like = Like.find_one( # {'entity_id': share.id, 'entity_type': 'share', 'user_id': user_id}) # collect = Collect.find_one( # {'entity_id': share.id, 'entity_type': 'share', 'user_id': user_id}) # d_share.is_liking = bool(like.likenum) if like else False # d_share.is_disliking = bool(like.dislikenum) if like else False # d_share.is_collecting = bool(collect.collectnum) if collect else False # print(d_share.id, len(likes)) share.is_liking = True if likes and share.id in likes else False share.is_disliking = True if dislikes and share.id in dislikes else False share.is_collecting = True if collects and share.id in collects else False l_share.append(share) self.render('userhome.html', user=user, shares=l_share, is_login=bool(self.current_user), likenum=likenum)