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 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, 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")