Esempio n. 1
0
 def get(self):
     action = self.request.path[7:]
     share_res = Share.find()
     if 'add' in action:
         do, share_id, suggestscore = action.split('!')
         share = Share.by_sid(share_id)
         share.suggestscore = float(suggestscore)
         share.save()
     for share in share_res:
         share.score = 0.001 * share.hitnum + share.likenum - \
             share.dislikenum + 0.5 * share.commentnum - \
             share.status + share.suggestscore + 0.5 * share.id
         share.save()
     # self.write_json({'objs': list(Share.find().sort('score',
     # DESCENDING))})
     share_res = Share.find().sort('score', DESCENDING)
     display = ''
     display += '<p>score sugg hit like dis comment status title id</p>'
     for share in share_res:
         display += '<p>%s  %s  %s  %s  %s  %s  %s  %s  %s</p>' % (
             share.score,
             share.suggestscore,
             share.hitnum,
             share.likenum,
             share.dislikenum,
             share.commentnum,
             share.status,
             share.title,
             share.id)
     self.write(display)
Esempio n. 2
0
 def get(self, action):
     action = self.request.path[7:]
     share_res = Share.find()
     if 'add' in action:
         do, share_id, suggestscore = action.split('!')
         share = Share.by_sid(share_id)
         share.suggestscore = float(suggestscore)
         share.save()
     for share in share_res:
         share.score = 0.001 * share.hitnum + share.likenum - \
             share.dislikenum + 0.5 * share.commentnum - \
             share.status + share.suggestscore + 0.5 * share.id
         share.save()
     # self.write_json({'objs': list(Share.find().sort('score',
     # DESCENDING))})
     share_res = Share.find().sort('score', DESCENDING)
     display = ''
     display += '<p>score sugg hit like dis comment status title id</p>'
     for share in share_res:
         display += '<p>%s  %s  %s  %s  %s  %s  %s  %s  %s</p>' % (
             share.score,
             share.suggestscore,
             share.hitnum,
             share.likenum,
             share.dislikenum,
             share.commentnum,
             share.status,
             share.title,
             share.id)
     self.write(display)
Esempio n. 3
0
    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()
Esempio n. 4
0
File: share.py Progetto: bowu8/anwen
 def post(self):
     commentbody = self.get_argument("commentbody", None)
     share_id = self.get_argument("share_id", None)
     html = markdown2.markdown(commentbody)
     comment = Comment
     doc = {}
     doc['user_id'] = self.current_user["user_id"]
     doc['share_id'] = int(share_id)
     doc['commentbody'] = commentbody
     comment.new(doc)
     share = Share.by_sid(share_id)
     share.commentnum += 1
     share.save()
     name = tornado.escape.xhtml_escape(self.current_user["user_name"])
     gravatar = get_avatar(self.current_user["user_email"], 50)
     newcomment = ''.join([
         '<div class="comment">',
         '<div class="avatar">',
         '<img src="', gravatar,
         '</div>',
         '<div class="name">', name,
         '</div>',
         '<div class="date" title="at"></div>', html,
         '</div>',
     ])
     self.write(newcomment)
Esempio n. 5
0
    def post(self):
        share_id = self.get_argument("share_id", None)
        tags = self.get_argument("tags", '')
        # user_id = self.current_user["user_id"]
        tags = tags.strip()
        if share_id:
            share = Share.by_sid(share_id)
            if share and tags not in share.tags:
                tags = share.tags + ' ' + tags
                res = {
                    'tags': tags,
                    'updated': time.time(),
                }

                share.update(res)
                share.save()

                tags = tags.split(' ')
                tags = list(set(tags))
                for i in tags:
                    doc = {
                        'name': i,
                        'share_ids': share.id
                    }
                    Tag.new(doc)
Esempio n. 6
0
def todo_get_suggest(share, current_user):
    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 += 1  # todo
        if current_user:
            is_hitted = Hit.find(
                {
                    'share_id': share.id
                },
                {
                    'user_id': int(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]
Esempio n. 7
0
    def post(self):
        id = self.get_argument("id", None)
        tags = self.get_argument("tags", '')
        user_id = self.current_user["user_id"]
        res = {
            'title': self.get_argument("title"),
            'markdown': self.get_argument("markdown"),
            'sharetype': self.get_argument("type"),
            'slug': self.get_argument("slug", ''),
            'tags': tags,
            'updated': time.time(),
        }

        if id:
            share = Share.by_sid(id)
            if not share:
                self.redirect("/404")
            share.update(res)
            share.save()
        else:
            share = Share
            res['user_id'] = user_id
            share = share.new(res)
            user = User.by_sid(user_id)
            user.user_leaf += 10
            user.save()
        for i in tags.split(' '):
            Tag.new(i, share.id)
        self.redirect("/share/" + str(share.id))
Esempio n. 8
0
    def get(self, name=None):
        if not name:
            tags = Tag.find()
            self.render("tag.html", tags=tags)
        else:
            tag = Tag.find_one({'name': name})
            shares = []
            share_ids = tag.share_ids.split(' ')
            share_ids = list(set(share_ids))
            cond = {}
            cond['_id'] = 0
            cond['id'] = 1
            cond['user_id'] = 1
            cond['published'] = 1
            cond['sharetype'] = 1
            cond['title'] = 1
            cond['commentnum'] = 1

            for share_id in share_ids:
                # share = Share.by_sid(share_id)
                share = Share.find_one({'id': int(share_id)}, cond)
                print(share)
                # <!-- <p class="info">{{ escape(share.markdown) }} ...</p> -->
                user = User.by_sid(share.user_id)
                share.user_name = user.user_name
                share.user_domain = user.user_domain
                share.published = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
                # share.markdown = filter_tags(
                #     markdown2.markdown(share.markdown))[:100]
                shares.append(share)
            self.render("tage.html", tag=tag, name=name, shares=shares)
Esempio n. 9
0
 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()
Esempio n. 10
0
 def post(self):
     commentbody = self.get_argument("commentbody", None)
     share_id = self.get_argument("share_id", None)
     html = markdown2.markdown(commentbody)
     comment = Comment
     doc = {}
     doc['user_id'] = self.current_user["user_id"]
     doc['share_id'] = int(share_id)
     doc['commentbody'] = commentbody
     comment.new(doc)
     share = Share.by_sid(share_id)
     share.commentnum += 1
     share.save()
     name = tornado.escape.xhtml_escape(self.current_user["user_name"])
     gravatar = get_avatar(self.current_user["user_email"], 50)
     newcomment = ''.join([
         '<div class="comment">',
         '<div class="avatar">',
         '<img src="',
         gravatar,
         '</div>',
         '<div class="name">',
         name,
         '</div>',
         '<div class="date" title="at"></div>',
         html,
         '</div>',
     ])
     self.write(newcomment)
Esempio n. 11
0
 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,
         'collectnum': 1,
     }
     number = Collect.find(cond, {'_id': 0}).count()
     collects = Collect.find(cond, {'_id': 0}).sort(
         '_id', -1).limit(per_page).skip((page - 1) * per_page)
     res = []
     print(collects.count())
     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)
Esempio n. 12
0
    def get(self, node):
        page = self.get_argument("page", 1)
        share_res = Share.find({'sharetype': node}).sort(
            '_id', DESCENDING).limit(10).skip((int(page) - 1) * 10)
        pagesum = (share_res.count() + 9) / 10

        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = time.strftime(
                '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
            share.domain = user.user_domain
            share.markdown = filter_tags(
                markdown2.markdown(share.markdown))[:100]
            share.gravatar = get_avatar(user.user_email, 16)
            shares.append(share)
        members = User.find().sort('_id', DESCENDING).limit(20)
        members_dict = []
        for member in members:
            member.gravatar = get_avatar(member.user_email, 25)
            members_dict.append(member)
        node_about = options.node_about[node]
        self.render(
            "node.html", shares=shares, members=members_dict,
            pagesum=pagesum, page=page, node=node, node_about=node_about)
Esempio n. 13
0
File: index.py Progetto: anwen/anwen
 def get(self, node='home'):
     page = self.get_argument("page", 1)
     # share_res = Share.find({'status': 0}).sort(
     #     'score', DESCENDING).limit(11).skip((int(page) - 1) * 11)
     share_res = Share.find({'status': {'$gte': 1}}).sort(
         '_id', DESCENDING).limit(11).skip((int(page) - 1) * 11)
     pagesum = (share_res.count() + 10) / 11
     shares = []
     for share in share_res:
         if share.id in (48, 47):
             continue
         user = User.by_sid(share.user_id)
         share.name = user.user_name
         share.published = time.strftime(
             '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
         share.domain = user.user_domain
         share.markdown = cutter(
             markdown2.markdown(share.markdown))
         share.title = share.title.split('_')[0]
         shares.append(share)
     self.render(
         "node.html",
         shares=shares,
         pagesum=pagesum, page=page, node=node,
     )
Esempio n. 14
0
def todo_get_suggest(share, current_user):
    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 += 1  # todo
        if current_user:
            is_hitted = Hit.find(
                {'share_id': share.id},
                {'user_id': int(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]
Esempio n. 15
0
 def get(self):
     user_id = self.current_user['user_id']
     share_id = self.get_argument("delete", None)
     s = self.get_argument("s", None)
     if admin.is_admin(user_id):
         if share_id and not s:
             admin.delete_share(share_id)
         if share_id and s and admin.is_superadmin(user_id):
             admin.delete_share_by_s(share_id)
         if s:
             shares = Share.find({'status': {'$ne': 1}})
             self.render('admin/super_share.html', shares=shares)
         else:
             shares = Share.find()
             self.render('admin/share.html', shares=shares)
     else:
         self.render('admin/join_admin.html')
Esempio n. 16
0
 def get(self):
     user_id = self.current_user['user_id']
     share_id = self.get_argument("delete", None)
     s = self.get_argument("s", None)
     if admin.is_admin(user_id):
         if share_id and not s:
             admin.delete_share(share_id)
         if share_id and s and admin.is_superadmin(user_id):
             admin.delete_share_by_s(share_id)
         if s:
             shares = Share.find({'status': {'$ne': 1}})
             self.render('admin/super_share.html', shares=shares)
         else:
             shares = Share.find()
             self.render('admin/share.html', shares=shares)
     else:
         self.render('admin/join_admin.html')
Esempio n. 17
0
 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)
Esempio n. 18
0
    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()
Esempio n. 19
0
 def get(self):
     share_id = self.get_argument("id", None)
     share = None
     if share_id:
         share = Share.by_sid(share_id)
     editor = self.get_argument("editor", options.default_editor)
     if editor:
         self.render("share_wysiwyg.html", share=share)
     else:
         self.render("share.html", share=share)
Esempio n. 20
0
def get_share_by_slug(slug):
    # 特殊id ramdom
    if slug == 'random':
        cond = {}
        cond['status'] = {'$gte': 1}
        # shares = Share.find(cond, {'_id': 0})
        shares = Share.find(cond)
        share = random.choice(list(shares))
    elif slug.isdigit():
        share = Share.by_sid(slug)
    else:
        share = Share.by_slug(slug)
    if share:
        share.hitnum += 1
        if isinstance(share.tags, str):
            share.tags = share.tags.split()
        share.save()
        share.pop('_id')
    return share
Esempio n. 21
0
def get_share_by_slug(slug):
    # 特殊id ramdom
    if slug == 'random':
        cond = {}
        cond['status'] = {'$gte': 1}
        # shares = Share.find(cond, {'_id': 0})
        shares = Share.find(cond)
        share = random.choice(list(shares))
    elif slug.isdigit():
        share = Share.by_sid(slug)
    else:
        share = Share.by_slug(slug)
    if share:
        share.hitnum += 1
        if isinstance(share.tags, str):
            share.tags = share.tags.split()
        share.save()
        share.pop('_id')
    return share
Esempio n. 22
0
 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))
     likes = User.find({'user_id': user._id})
     likenum = likes.count()
     for like in likes:
         share = Share.by_id(like.share_id)
         like.title = share.title
         like.id = share.id
         like.type = share.sharetype
     user.gravatar = get_avatar(user.user_email, 100)
     self.render("userlike.html", user=user, likenum=likenum, likes=likes)
Esempio n. 23
0
File: user.py Progetto: anwen/anwen
 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)
Esempio n. 24
0
    def get(self):
        share_res = Share.find()
        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = datetime.datetime.fromtimestamp(share.published)
            share.updated = datetime.datetime.fromtimestamp(share.updated)
            share.domain = user.user_domain
            share.content = markdown2.markdown(share.markdown)
            shares.append(share)

        self.set_header("Content-Type", "application/atom+xml")
        self.render("feed.xml", shares=shares)
Esempio n. 25
0
    def get(self):
        share_res = Share.find()
        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = datetime.datetime.fromtimestamp(share.published)
            share.updated = datetime.datetime.fromtimestamp(share.updated)
            share.domain = user.user_domain
            share.content = markdown2.markdown(share.markdown)
            shares.append(share)

        self.set_header("Content-Type", "application/atom+xml")
        self.render("feed.xml", shares=shares)
Esempio n. 26
0
    def post(self):
        # print self.request.arguments
        share_id = self.get_argument("id", None)
        title = self.get_argument("title", '')
        markdown = self.get_argument("markdown", '')
        content = self.get_argument("content", '')
        sharetype = self.get_argument("type", '')
        slug = self.get_argument("slug", '')
        status = 1 if self.get_argument("dosubmit", None) == u'保存草稿' else 0
        tags = self.get_argument("tags", '')
        upload_img = self.get_argument("uploadImg", '')
        post_img = self.get_argument("post_Img", '')
        post_img = '' if post_img == 'None' else post_img
        user_id = self.current_user["user_id"]
        res = {
            'title': title,
            'markdown': markdown,
            'content': content,
            'sharetype': sharetype,
            'slug': slug,
            'tags': tags,
            'status': status,
            'upload_img': upload_img,
            'post_img': post_img,
            'updated': time.time(),
        }

        if share_id:
            share = Share.by_sid(share_id)
            if not share:
                self.redirect("/404")
            share.update(res)
            share.save()
        else:
            share = Share
            res['user_id'] = user_id
            share = share.new(res)
            user = User.by_sid(user_id)
            user.user_leaf += 10
            user.save()
        for i in tags.split(' '):
            doc = {
                'name': i,
                'share_ids': share.id
            }
            Tag.new(doc)
        if status == 1:
            self.redirect("/share/?id=" + str(share.id))
        self.redirect("/share/" + str(share.id))
Esempio n. 27
0
 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)
Esempio n. 28
0
 def get(self):
     share_id = self.get_argument("id", None)
     sharetype = self.get_argument("sharetype", None)
     editor = self.get_argument("editor", options.default_editor)
     share = None
     if share_id:
         share = Share.by_sid(share_id)
         sharetype = share.sharetype if share else None
     print(sharetype)
     if sharetype == 'goodlink':
         self.render("share_link.html", share=share)
     if editor:
         self.render("share_wysiwyg.html", share=share)
     else:
         self.render("share.html", share=share)
Esempio n. 29
0
def add_share(user_from, user_to, user_checkpoint):
    """
    Adds a share record to the database (if it does not exists)
    """
    share = get_share_w_attr(user_from, user_to, user_checkpoint)
    
    if share is None:
    
        from db import db, Share
        now = datetime.datetime.now()
        
        share = Share()
        share.user_from_id = user_from.id
        share.user_to_id = user_to.id
        share.user_checkpoint_id = user_checkpoint.id
        share.timestamp = now
        
        db.session.add(share)
        db.session.commit()
    
    #add notification
    add_notification(NOTIFICATION_TYPE, user_from, user_to, share.id, user_checkpoint.id)
    
    return share
Esempio n. 30
0
    def post(self):
        # print self.request.arguments
        share_id = self.get_argument("id", None)
        title = self.get_argument("title", '')
        markdown = self.get_argument("markdown", '')
        content = self.get_argument("content", '')
        sharetype = self.get_argument("sharetype", '')
        slug = self.get_argument("slug", '')
        tags = self.get_argument("tags", '')
        upload_img = self.get_argument("uploadImg", '')
        post_img = self.get_argument("post_Img", '')
        link = self.get_argument("link", '')
        user_id = self.current_user["user_id"]
        res = {
            'title': title,
            'markdown': markdown,
            'content': content,
            'sharetype': sharetype,
            'slug': slug,
            'tags': tags,
            'upload_img': upload_img,
            'post_img': post_img,
            'link': link,
            'updated': time.time(),
        }

        if share_id:
            share = Share.by_sid(share_id)
            if not share:
                self.redirect("/404")
            share.update(res)
            share.save()
        else:
            share = Share
            res['user_id'] = user_id
            share = share.new(res)
            user = User.by_sid(user_id)
            user.user_leaf += 10
            user.save()
        for i in tags.split(' '):
            doc = {
                'name': i,
                'share_ids': share.id
            }
            Tag.new(doc)
        self.redirect("/share/" + str(share.id))
Esempio n. 31
0
File: index.py Progetto: bowu8/anwen
    def get(self, node):
        page = self.get_argument("page", 1)
        share_res = Share.find({"sharetype": node}).sort("_id", DESCENDING).limit(11).skip((int(page) - 1) * 11)
        pagesum = (share_res.count() + 10) / 11

        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(share.published))
            share.domain = user.user_domain
            share.markdown = filter_tags(markdown2.markdown(share.markdown))[:400]
            share.gravatar = get_avatar(user.user_email, 16)
            shares.append(share)

        node_about = options.node_about[node]
        self.render("node.html", shares=shares, pagesum=pagesum, page=page, node=node, node_about=node_about)
Esempio n. 32
0
 def get(self):
     share_id = self.get_argument("id", None)
     sharetype = self.get_argument("sharetype", None)
     editor = self.get_argument("editor", options.default_editor)
     share = None
     if share_id:
         share = Share.by_sid(share_id)
         if 'vote_open' not in share:
             share.vote_open = 0
             share.vote_title = ''
         sharetype = share.sharetype if share else None
     if sharetype == 'goodlink':
         self.render("share_link.html", share=share)
     elif editor:
         self.render("share_wysiwyg.html", share=share)
     else:
         self.render("share.html", share=share)
Esempio n. 33
0
 def get(self, name=None):
     if not name:
         tags = Tag.find()
         self.render("tag.html", tags=tags)
     else:
         tag = Tag.find_one({'name': name})
         shares = []
         for i in tag.share_ids.split(' '):
             share = Share.by_sid(i)
             user = User.by_sid(share.user_id)
             share.name = user.user_name
             share.published = time.strftime(
                 '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
             share.domain = user.user_domain
             share.markdown = filter_tags(markdown2.markdown(
                 share.markdown))[:100]
             shares.append(share)
         self.render("tage.html", name=name, shares=shares)
Esempio n. 34
0
File: index.py Progetto: anwen/anwen
 def get(self, name=None):
     if not name:
         tags = Tag.find()
         self.render("tag.html", tags=tags)
     else:
         tag = Tag.find_one({'name': name})
         shares = []
         for i in tag.share_ids.split(' '):
             share = Share.by_sid(i)
             user = User.by_sid(share.user_id)
             share.name = user.user_name
             share.published = time.strftime(
                 '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
             share.domain = user.user_domain
             share.markdown = filter_tags(
                 markdown2.markdown(share.markdown))[:100]
             shares.append(share)
         self.render("tage.html", name=name, shares=shares)
Esempio n. 35
0
    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()
Esempio n. 36
0
 def post(self):
     share_id = self.get_argument("share_id", None)
     tags = self.get_argument("tags", '')
     tags = tags.strip()
     if share_id:
         share = Share.by_sid(share_id)
         if share and tags not in share.tags:
             tags = share.tags + ' ' + tags
             res = {
                 'tags': tags,
                 'updated': time.time(),
             }
             share.update(res)
             share.save()
             tags = tags.split(' ')
             tags = list(set(tags))
             for i in tags:
                 doc = {'name': i, 'share_ids': share.id}
                 Tag.new(doc)
Esempio n. 37
0
 def post(self):
     share_id = self.get_argument("share_id", None)
     likenum = self.get_argument("likenum", 0)
     like = Like
     like['user_id'] = self.current_user["user_id"]
     like['share_id'] = int(share_id)
     like.save()
     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(self.current_user["user_id"])
     user.user_leaf += 2
     user.save()
     likenum = int(likenum) + 1
     newlikes = ':) ' + str(likenum)
     self.write(newlikes)
Esempio n. 38
0
File: admin.py Progetto: anwen/anwen
 def get(self):
     user_id = self.current_user['user_id']
     delete_share_id = self.get_argument("delete", None)
     status = self.get_argument("status", 0)
     status = int(status)
     s = self.get_argument("s", None)  # super
     if admin.is_admin(user_id):
         if delete_share_id and not s:
             admin.delete_share(delete_share_id)
         if delete_share_id and s and admin.is_superadmin(user_id):
             admin.delete_share_by_s(delete_share_id)
         # shares = Share.find({'status': {'$ne': 1}}).sort('_id', -1)
         if status == 100:
             status = {'$gt': 3}
         shares = Share.find({'status': status}).sort('_id', -1)
         if s:
             self.render('admin/super_share.html', shares=shares)
         else:
             self.render('admin/share.html', shares=shares)
     else:
         self.render('admin/join_admin.html')
Esempio n. 39
0
File: admin.py Progetto: anwen/anwen
 def get(self):
     user_id = self.current_user['user_id']
     delete_share_id = self.get_argument("delete", None)
     status = self.get_argument("status", 0)
     status = int(status)
     s = self.get_argument("s", None)  # super
     if admin.is_admin(user_id):
         if delete_share_id and not s:
             admin.delete_share(delete_share_id)
         if delete_share_id and s and admin.is_superadmin(user_id):
             admin.delete_share_by_s(delete_share_id)
         # shares = Share.find({'status': {'$ne': 1}}).sort('_id', -1)
         if status == 100:
             status = {'$gt': 3}
         shares = Share.find({'status': status}).sort('_id', -1)
         if s:
             self.render('admin/super_share.html', shares=shares)
         else:
             self.render('admin/share.html', shares=shares)
     else:
         self.render('admin/join_admin.html')
Esempio n. 40
0
    def get(self, node):
        page = self.get_argument("page", 1)
        share_res = Share.find({'sharetype': node}).sort(
            '_id', DESCENDING).limit(11).skip((int(page) - 1) * 11)
        pagesum = (share_res.count() + 10) / 11

        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = time.strftime(
                '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
            share.domain = user.user_domain
            share.markdown = cutter(
                markdown2.markdown(share.markdown))
            shares.append(share)

        node_info = options.node_about[node]
        self.render(
            "node.html", shares=shares,
            pagesum=pagesum, page=page, node=node, node_info=node_info)
Esempio n. 41
0
 def post(self):
     commentbody = self.get_argument("commentbody", None)
     share_id = self.get_argument("share_id", None)
     comment = Comment
     doc = {}
     doc['user_id'] = self.current_user["user_id"]
     user = User.by_sid(self.current_user["user_id"])
     # user_name in current_user is not the newest
     doc['user_name'] = user["user_name"]
     doc['share_id'] = int(share_id)
     doc['commentbody'] = commentbody
     comment.new(doc)
     share = Share.by_sid(share_id)
     share.commentnum += 1
     share.save()
     # name = tornado.escape.xhtml_escape(self.current_user["user_name"])
     # gravatar = get_avatar(self.current_user["user_email"], 50)
     self.res = {
         'success': True,
     }
     self.write_json()
Esempio n. 42
0
def check():
    # share_num = Share.find().count()
    # share_with_tag_num = share_num - Share.find({'tags': []}).count()

    for i in adb.Share_Col.find().sort('_id', 1):
        if i['status'] < 1:
            continue
        # if i['tags'] == []:
        if i['tags']:
            continue
        # print(i['id'], i['title'])
        print(i['user_id'])

        # adb.Share_Col.update().sort('_id', 1):
        tags = get_tags(i)
        adb.Share_Col.update({'_id': i['_id']}, {'$set': {'tags': tags}})
        for tag in tags:
            doc = {'name': tag, 'share_ids': i['id']}
            Tag.new(doc)

    share_without_tag_num = Share.find({'tags': []}).count()
    print(share_without_tag_num)
Esempio n. 43
0
    def get(self, node='home'):
        page = self.get_argument("page", 1)
        share_res = Share.find({'status': 0}).sort(
            'score', DESCENDING).limit(11).skip((int(page) - 1) * 11)

        pagesum = (share_res.count() + 10) / 11
        shares = []
        for share in share_res:
            user = User.by_sid(share.user_id)
            share.name = user.user_name
            share.published = time.strftime(
                '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
            share.domain = user.user_domain
            share.markdown = filter_tags(
                markdown2.markdown(share.markdown))[:400]
            share.gravatar = get_avatar(user.user_email, 16)
            shares.append(share)

        self.render(
            "node.html", shares=shares,
            pagesum=pagesum, page=page, node=node,
        )
Esempio n. 44
0
 def get(self, node='home'):
     page = self.get_argument("page", 1)
     share_res = Share.find({
         'status': 0
     }).sort('score', DESCENDING).limit(11).skip((int(page) - 1) * 11)
     pagesum = (share_res.count() + 10) / 11
     shares = []
     for share in share_res:
         user = User.by_sid(share.user_id)
         share.name = user.user_name
         share.published = time.strftime('%Y-%m-%d %H:%M:%S',
                                         time.localtime(share.published))
         share.domain = user.user_domain
         share.markdown = cutter(markdown2.markdown(share.markdown))
         shares.append(share)
     self.render(
         "node.html",
         shares=shares,
         pagesum=pagesum,
         page=page,
         node=node,
     )
Esempio n. 45
0
 def post(self):
     commentbody = self.get_argument("commentbody", None)
     share_id = self.get_argument("share_id", None)
     html = markdown2.markdown(commentbody)
     comment = Comment
     comment['user_id'] = self.current_user["user_id"]
     comment['share_id'] = int(share_id)
     comment['commentbody'] = commentbody
     comment.save()
     share = Share.by_sid(share_id)
     share.commentnum += 1
     share.save()
     name = tornado.escape.xhtml_escape(self.current_user["user_name"])
     gravatar = get_avatar(self.current_user["user_email"], 50)
     newcomment = ''
     newcomment += ' <div class="comment">'
     newcomment += '<div class="avatar">'
     newcomment += '<img src="' + gravatar + '" />'
     newcomment += '</div>'
     newcomment += '<div class="name">' + name + '</div>'
     newcomment += '<div class="date" title="at"></div>'
     newcomment += html
     newcomment += '</div>'
     self.write(newcomment)
Esempio n. 46
0
    def get(self):
        token = self.request.headers.get('Authorization', '')
        page = self.get_argument("page", 1)
        per_page = self.get_argument("per_page", 10)
        tag = self.get_argument('tag', '')
        filter_type = self.get_argument("filter_type", '')  # my_tags my_likes
        last_suggested = self.get_argument("last_suggested", 0)
        read_status = self.get_argument('read_status', 1)
        meta_info = self.get_argument("meta_info", 1)

        read_status = int(read_status)
        per_page = int(per_page)
        page = int(page)
        if not last_suggested:
            last_suggested = 0
        last_suggested = float(last_suggested) / 1000 + 1

        user = self.get_user_dict(token)

        cond = {}
        tags = None
        if user and filter_type == 'my_tags':
            d_user = User.by_sid(user['user_id'])
            if d_user:
                tags = d_user['user_tags']
        # 按照tag来过滤
        if tags:
            cond['tags'] = {"$in": tags}
        elif tag:
            cond['tags'] = tag

        # 不同的用户显示不同级别的推荐
        # if user and user['user_id'] in wx_admin_ids:
        if user and user['user_id'] == 1:
            cond['status'] = {'$gte': 1}
        else:
            cond['status'] = {'$gte': 1}

        # 已读列表 20ms
        l_hitted_share_id = []
        if user and read_status:
            hits = Hit.find({'user_id': user['user_id']}, {
                '_id': 0,
                'share_id': 1
            })
            l_hitted_share_id = [i['share_id'] for i in hits]

        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
        shares = Share.find(cond, filter_d).sort('suggested',
                                                 -1).limit(per_page).skip(
                                                     (page - 1) * per_page)
        # 过滤
        new_shares = []
        for share in shares:
            user = User.by_sid(share.user_id)
            # share = dict(share)
            share['type'] = 1
            # if share.post_img:
            # if hasattr(share, 'post_img'):
            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['author'] = user.user_name
            share['published'] = int(share['published'] *
                                     1000)  # share.published
            if read_status:
                share['read'] = bool(share['id'] in l_hitted_share_id)

            if 0:  # 不展示作者头像
                if user.user_email.endswith('@wechat'):
                    share['user_img'] = options.site_url + \
                        get_avatar_by_wechat(user._id)
                if user.user_email.endswith('@anwensf.com'):
                    share['user_img'] = options.site_url + \
                        get_avatar_by_feed(user.id)
                else:
                    share['user_img'] = options.site_url + \
                        get_avatar(user.user_email, 100)
            new_shares.append(share)

        if meta_info:
            meta = {}
            if last_suggested:
                cond_update = copy.deepcopy(cond)
                cond_update['suggested'] = {'$gt': last_suggested}
                number_of_update = Share.find(cond_update, {
                    '_id': 0,
                    'id': 1
                }).count()
                meta['number_of_update'] = number_of_update
            if tag:  # 子标签的文章数量
                d_tags = get_tags()
                d_tags_parents = get_tags_parents()  # get_tags_parent
                if tag in d_tags:
                    sub_tags = []
                    for name in d_tags[tag]:
                        info = {}
                        info['name'] = name
                        # num = Share.find({'tags': name}, {'_id': 0}).count()
                        # num_recent = Share.find(
                        #     {'tags': name, 'published': {'$gt': time.time() - 86400 * 30}}, {'_id': 0}).count()
                        # info['num'] = num
                        # info['num_recent'] = num_recent
                        sub_tags.append(info)
                    meta['sub_tags'] = sub_tags
                meta['parent_tags'] = []
                if tag in d_tags_parents:
                    # meta['parent_tags'].append(d_tags_parent[tag])
                    meta['parent_tags'] = d_tags_parents[tag]  # hypernym
            number = Share.find(cond, {'_id': 0}).count()  # 'id': 1
            meta['number'] = number
            # if filter_type == 'my_tags':
            #     meta['tags'] = tags

        self.res = {'articles': new_shares}
        self.meta = meta
        return self.write_json()
Esempio n. 47
0
    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]
Esempio n. 48
0
    def post(self):
        # TODO
        # print(self.request.arguments)
        share_id = self.get_argument("id", None)
        title = self.get_argument("title", '')
        markdown = self.get_argument("markdown", '')
        content = self.get_argument("content", '')
        sharetype = self.get_argument("sharetype", '')
        slug = self.get_argument("slug", '')
        tags = self.get_argument("tags", '')
        # upload_img = self.get_argument("uploadImg", '')
        post_img = self.get_argument("post_Img", '')
        link = self.get_argument("link", '')
        user_id = self.current_user["user_id"]
        vote_open = self.get_argument("vote_open", '')
        vote_title = self.get_argument("vote_title", '')
        img_url = self.get_argument("img_url", '')

        tags = tags.split()

        if link:
            url = link
            doc = Webcache.find_one({'url': url}, {'_id': 0})
            if doc:
                logger.info('already downloaded')
                doc_title = doc.title
                # markdown = doc.markdown
            else:
                sessions = requests.session()
                sessions.headers[
                    'User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'
                try:
                    # response = sessions.get(url)
                    response = sessions.get(url, timeout=4)
                # TODO: try to use a proxy
                except (requests.ConnectionError, requests.Timeout) as e:
                    print(e)
                    self.write("GFW...")
                    return
                # except requests.exceptions.HTTPError as e:
                #     if e.response.status_code == 400:
                #         error = e.response.json()
                #         code = error['code']
                #         message = error['message']

                except Exception as e:
                    logger.info('e: {}'.format(e))
                    # self.redirect("/")
                    self.write("GFW")
                    return
                # response.encoding = 'utf-8'  # TODO
                response.encoding = get_charset(response)
                logger.info('response.encoding {}'.format(response.encoding))
                doc = Document(response.text)
                doc_title = doc.title()
                summary = doc.summary()
                _markdown = html2text.html2text(summary)
                _markdown = _markdown.replace('-\n', '-').strip()
                res_webcache = {}
                res_webcache['url'] = url
                res_webcache['title'] = doc_title
                res_webcache['markdown'] = _markdown
                if _markdown:
                    webcache = Webcache
                    webcache.new(res_webcache)

        if vote_open.isdigit():
            vote_open = int(vote_open)
        else:
            vote_open = 0
        if not title:
            title = doc_title

        # 处理封面链接

        if img_url and not post_img:
            ext = img_url.split('?')[0].split('.')[-1]
            ext = '.' + ext.lower()
            print(ext)
            assert ext in ['.jpg', '.jpeg', '.gif', '.png', '.bmp']
            img_dir = 'static/upload/img'
            now = datetime.datetime.now()
            t = now.strftime('%Y%m%d_%H%M%S_%f')
            img_name = '%s%s' % (t, ext)
            img_path = '%s/%s' % (img_dir, img_name)
            print(img_path)
            r = requests.get(img_url, verify=False,
                             stream=True)  # stream=True)
            chunk_size = 100
            with open(img_path, 'wb') as image:
                for chunk in r.iter_content(chunk_size):
                    image.write(chunk)

            im = Image.open(img_path)
            width, height = im.size
            if width / height > 5 or height / width > 5:
                os.remove(img_path)  # 判断比例 删除图片
                print('请不要上传长宽比例过大的图片')
            else:
                # 创建1200x550 750x230 365x230缩略图
                make_post_thumb(img_path,
                                sizes=[(1200, 550), (750, 230), (365, 230),
                                       (260, 160)])
                print('done')
                post_img = img_path.split('/')[-1]
                post_img = post_img.split('.')[0] + '_1200.jpg'

        res = {
            'title': title,
            'markdown': markdown,
            'content': content,
            'sharetype': sharetype,
            'slug': slug,
            'tags': tags,
            'post_img': post_img,
            'link': link,
            'vote_open': vote_open,
            'vote_title': vote_title,
            'updated': time.time(),
        }
        # if not markdown:
        #     self.redirect("/")
        #     return
        if share_id:
            share = Share.by_sid(share_id)
            if not share:
                self.redirect("/404")
            share.update(res)
            share.save()
        else:
            share = Share
            res['user_id'] = user_id
            share = share.new(res)
            user = User.by_sid(user_id)
            user.user_leaf += 10
            user.save()
        for i in tags:
            doc = {'name': i, 'share_ids': share.id}
            Tag.new(doc)
        self.redirect("/share/" + str(share.id))
Esempio n. 49
0
    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")
Esempio n. 50
0
    def get(self, node='home'):
        page = self.get_argument("page", 1)
        per_page = self.get_argument("per_page", 11)
        status = self.get_argument("status", 'gte_1')

        per_page = int(per_page)
        # 控制显示级别
        assert '_' in status
        st_type, st_num = status.split('_')
        status = {'${}'.format(st_type): int(st_num)}  # {'$gte': 1}
        # 当node不是home时,不控制显示级别
        conds = {'status': status}
        if node not in 'home'.split():
            conds['sharetype'] = node
            # if node not in 'rss'.split():
            conds['status'] = {'$gte': 0}

        # sort type
        # 'score', DESCENDING
        share_res = Share.find(conds).sort('_id',
                                           DESCENDING).limit(per_page).skip(
                                               (int(page) - 1) * per_page)
        pagesum = int((share_res.count() + per_page - 1) / per_page)

        shares = []
        if per_page >= 20:  # 另外一种显示UI
            for share in share_res:
                user = User.by_sid(share.user_id)
                share.name = user.user_name
                share.published = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
                share.domain = user.user_domain
                md = share.markdown
                md = md.replace('>\n', '> ')
                share.markdown = cutter(markdown2.markdown(md))
                share.title = share.title.split('_')[0]
                shares.append(share)
                del user
            tpl_name = 'node_alot'
        else:
            for share in share_res:
                # if share.id in (48, 47):  # 临时屏蔽
                #     continue
                user = User.by_sid(share.user_id)  # 获取用户信息,需要多次查表!!!
                share.name = user.user_name
                share.domain = user.user_domain
                share.published = time.strftime(
                    '%Y-%m-%d %H:%M:%S', time.localtime(share.published))
                md = share.markdown
                md = md.replace('>\n', '> ')
                share.markdown = cutter(markdown2.markdown(md))
                share.title = share.title.split('_')[0]
                shares.append(share)
                del user
                del share
                del md
            tpl_name = 'node'

        self.render(
            "{}.html".format(tpl_name),
            shares=shares,
            pagesum=pagesum,
            page=page,
            per_page=per_page,
            node=node,
        )
        del shares, share_res
        del tpl_name, pagesum, page, per_page, node, status
        return
Esempio n. 51
0
File: user.py Progetto: anwen/anwen
    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)
Esempio n. 52
0
    def get(self):
        page = self.get_argument("page", 1)
        per_page = self.get_argument("per_page", 10)
        filter_type = self.get_argument("filter_type", '')  # my_tags
        tag = self.get_argument('tag', '')
        meta_info = self.get_argument("meta_info", 1)
        last_suggested = self.get_argument("last_suggested", 0)
        read_status = self.get_argument('read_status', 1)
        token = self.request.headers.get('Authorization', '')
        # has_vote = self.get_argument("has_vote", None)
        # vote_open = self.get_argument("vote_open", None)

        read_status = int(read_status)
        per_page = int(per_page)
        page = int(page)
        last_suggested = float(last_suggested) / 1000 + 1
        user = self.get_user_dict(token)

        cond = {}
        # 按照tags来过滤
        tags = None
        if user and filter_type == 'my_tags':
            d_user = User.by_sid(user['user_id'])
            if d_user:
                tags = d_user['user_tags']
        # 按照tag来过滤
        if tags:
            cond['tags'] = {"$in": tags}
        elif tag:
            cond['tags'] = tag

        # 不同的用户显示不同级别的推荐
        if user and user['user_id'] in wx_admin_ids:
            cond['status'] = {'$gte': 1}
        else:
            cond['status'] = {'$gte': 1}

        l_hitted_share_id = []
        if user and read_status:
            hits = Hit.find({'user_id': user['user_id']})
            l_hitted_share_id = [i['share_id'] for i in hits]

        # if vote_open:
        #     if not vote_open.isdigit():
        #         return self.write_error(422)
        #     cond['vote_open'] = int(vote_open)
        # if has_vote:
        #     cond['vote_title'] = {'$ne': ''}

        number = Share.find(cond, {'_id': 0}).count()
        # sort: _id
        if last_suggested:
            cond_update = copy.deepcopy(cond)
            cond_update['suggested'] = {'$gt': last_suggested}
            number_of_update = Share.find(cond_update, {'_id': 0}).sort(
                'suggested', -1).count()
            logger.info('number_of_update 1: {}'.format(number_of_update))

        num_shares = Share.find(cond, {'_id': 0, 'id': 1}).count()

        shares = Share.find(cond, {'_id': 0}).sort(
            'suggested', -1).limit(per_page).skip((page - 1) * per_page)
        # shares = [fix_share(share) for share in shares]
        new_shares = []
        for share in shares:
            share = fix_share(share)
            user = User.by_sid(share.user_id)
            share = dict(share)
            share['user_name'] = user.user_name
            share['markdown'] = ''
            if read_status:
                share['read'] = bool(share['id'] in l_hitted_share_id)

            soup = BeautifulSoup(share['content'], "lxml")
            # kill all script and style elements
            for script in soup(["script", "style"]):
                script.extract()    # rip it out

            # get text
            text = soup.get_text()

            # break into lines and remove leading and trailing space on each
            lines = (line.strip() for line in text.splitlines())
            # break multi-headlines into a line each
            chunks = (phrase.strip()
                      for line in lines for phrase in line.split("  "))
            # drop blank lines
            text = '\n'.join(chunk for chunk in chunks if chunk)
            # print(text)
            share['summary'] = text[:150]
            share['content'] = ''

            if user.user_email.endswith('@wechat'):
                share['user_img'] = options.site_url + \
                    get_avatar_by_wechat(user._id)
            if user.user_email.endswith('@anwensf.com'):
                share['user_img'] = options.site_url + \
                    get_avatar_by_feed(user.id)
            else:
                share['user_img'] = options.site_url + \
                    get_avatar(user.user_email, 100)
            new_shares.append(share)

        # if tag:
        #     shares = [share for share in shares if tag in share['tags']]
        meta = {}
        meta['page'] = page
        meta['articleNumber'] = num_shares
        if meta_info and last_suggested:
            meta['number_of_update'] = number_of_update
        if meta_info and tag:
            d_tags = get_tags()
            # d_tags_parent = get_tags_parent()
            d_tags_parents = get_tags_parents()
            if tag in d_tags:
                sub_tags = []
                for name in d_tags[tag]:
                    num = Share.find({'tags': name}, {'_id': 0}).count()
                    num_recent = Share.find(
                        {'tags': name, 'published': {'$gt': time.time() - 86400 * 30}}, {'_id': 0}).count()
                    info = {}
                    info['name'] = name
                    info['num'] = num
                    info['num_recent'] = num_recent
                    sub_tags.append(info)
                meta['sub_tags'] = sub_tags
            meta['parent_tags'] = []
            if tag in d_tags_parents:
                # hypernym
                # meta['parent_tags'].append(d_tags_parent[tag])
                meta['parent_tags'] = d_tags_parents[tag]

        logger.info('last_suggested time: {}'.format(last_suggested))

        if new_shares:
            logger.info('new_shares[0] time: {}'.format(new_shares[0]['title']))
            logger.info('new_shares[0] published time: {}'.format(
                new_shares[0]['published']))
            logger.info('new_shares[0] suggested time: {}'.format(
                new_shares[0]['suggested']))

        self.res = {'articles': list(new_shares)}
        self.meta = meta
        # number=len(self.res)
        # number=number
        return self.write_json()
Esempio n. 53
0
    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()
Esempio n. 54
0
File: share.py Progetto: bowu8/anwen
 def get(self):
     shares = Share.find()
     self.set_header("Content-Type", "application/atom+xml")
     self.render("feed.xml", shares=shares)
Esempio n. 55
0
File: share.py Progetto: bowu8/anwen
 def get(self):
     share_id = self.get_argument("id", None)
     share = None
     if share_id:
         share = Share.by_sid(share_id)
     self.render("share.html", share=share)
Esempio n. 56
0
def fix():
    # for i in adb.Hit_Col.find().sort('_id', 1):
    for i in open(f1):
        i = i.strip()
        idx, title, ajson = i.split('\t')
        slug = title
        markdown = json.loads(ajson)['md']

        # markdown = markdown.replace(' BULLET::::', '\n')
        markdown = markdown.replace('BULLET::::', '†\n')
        markdown = markdown.replace('\n†\n', '\n')
        markdown = markdown.replace('†\n', '\n')
        markdown = markdown.replace('1.\n', '1. ')
        markdown = markdown.replace('2.\n', '2. ')
        markdown = markdown.replace('3.\n', '3. ')
        markdown = markdown.replace('4.\n', '4. ')
        markdown = markdown.replace('5.\n', '5. ')
        markdown = markdown.replace('6.\n', '6. ')
        markdown = markdown.replace('7.\n', '7. ')
        markdown = markdown.replace('8.\n', '8. ')
        markdown = markdown.replace('9.\n', '9. ')
        markdown = markdown.replace('0.\n', '0. ')

        markdown = markdown.replace('(\n)', '')
        markdown = markdown.replace('(,', '(')
        markdown = markdown.replace('()', '')
        markdown = markdown.replace('(; ,)', '')

        markdown = markdown.replace('\n!\n', '\n\n')

        # markdown = markdown.replace('## 三顾茅庐', '### 三顾茅庐')

        if title == '伍迪·艾伦':
            print(markdown)

        new_md = []
        for j in markdown.split('\n'):
            # 特殊例子
            # if cc_s2t.convert('三顾茅庐') in j:
            if '## 三顧茅廬' in j:
                j = j.replace('## 三顧茅廬', '### 三顧茅廬')
                print(j)
            if cc_s2t.convert('## 军事发明') in j:
                # 军事发明
                j = j.replace('### 軍事發明', '## 軍事發明')
                print(j)
            j = j.replace('! colspan="2" colspan="2" 袁', '袁')

            if '!-' in j:
                j = j.split('!-')[0]
            if 'style=' in j:
                j = j.split('style=')[0]
            if '|valign' in j:
                j = j.split('|valign')[0]
            if 'align=' in j:
                j = j.split('align=')[0]
            if '! width' in j:
                j = j.split('! width')[0]
            if '! colspan="7"' in j:
                j = j.split('! colspan="7"')[0]
            if '! scope="col"' in j:
                j = j.split('! scope="col"')[0]

            j = j.strip()

            if j.startswith('BULLET::::'):
                j = j[10:]
            # print(repr(j), 1111, j.startswith('BULLET::::'))
            # assert i.endswith('.')
            # i = i[:-1]
            # i = i.replace('BULLET::::-', '<li>') + '</li>'

            new_md.append(j)
        new_md = '\n'.join(new_md)
        markdown = cc.convert(new_md)
        sharetype = 'goodlink'
        link = 'https://zh.wikipedia.org/wiki?curid={}'.format(idx)
        user_id = 1
        res = {
            'title': title,
            'markdown': markdown,
            'sharetype': sharetype,
            'slug': slug,
            'link': link,
            'updated': time.time(),
        }

        if share_id:
            share = Share.by_sid(share_id)
            # print(share['title'], title)
            if share['title'] != title:
                continue
            if not share:
                raise
            share.update(res)
            share.save()
        else:
            share = Share.by_title(title)
            if share:
                print('fix', title)
                share.update(res)
                share.save()
                continue

            share = Share
            res['user_id'] = user_id
            share = share.new(res)
            print(idx)

            user = User.by_sid(user_id)
            # print(user)
            user.user_leaf += 10
            user.save()
Esempio n. 57
0
File: share.py Progetto: bowu8/anwen
    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")
Esempio n. 58
0
def add_from_file(rss_url, rss_hostname, rss_name):
    # rss_file = 'content/gen/qdaily_2019-04-20 15:07:12.xml'
    n = Share.find().count()
    print(n)
    print(rss_name)
    feeds = feedparser.parse(rss_url)
    for post in feeds.entries[::-1]:
        # authors
        # itunes_episodetype full
        # itunes_episode
        # itunes_explicit
        # itunes_title
        # itunes_duration
        # published link subtitle id image title tags
        # links title_detail author_detail summary_detail guidislink published_parsed summary content author
        # subtitle_detail

        # title title_detail
        # published published_parsed
        # summary summary_detail
        # author
        # link links guidislink
        # authors

        # 'itunes_title', 'itunes_episode'
        # 'author_detail', 'id', 'itunes_duration'
        # <itunes:duration>6957</itunes:duration>

        # TODO
        # 修正内容 目前暂时不支持
        # <enclosure type="audio/mpeg" url="https://kernelpanic.fm/55/audio.mp3"/>
        # <media:content url="https://cdn.flipboard.com/telegraph.co.uk/1356d637c7438f6fcffda0d5de177b6058904de6/original.jpg" medium="image" type="image/jpeg" width="480" height="300" />
        # media_content

        # print(post.keys())
        if hasattr(post, 'summary'):
            summary = post.summary
            assert post.summary == post.description
        else:
            summary = ''
        # 部分rss没有content
        if hasattr(post, 'content'):
            content = post.content[0]['value']
        else:
            if hasattr(post, 'summary'):
                content = post.summary
            else:
                print('no content', rss_url, rss_hostname, rss_name)
                continue
        if content.startswith('<![CDATA[') and content.endswith(']]>'):
            # m = rgx.search(content)
            # content = m.group(1)
            content = content[9:-3]
        if summary.startswith('<![CDATA[') and summary.endswith(']]>'):
            summary = summary[9:-3]

        if hasattr(post, 'published'):
            if 'GMT' == post.published[-3:]:
                published = datetime.strptime(post.published,
                                              "%a, %d %b %Y %H:%M:%S GMT")
            elif ',' in post.published:
                if post.published.endswith('2019'):
                    pass
                    # May 19, 2019
                    published = datetime.strptime(post.published, "%b %d, %Y")
                else:
                    published = datetime.strptime(post.published,
                                                  "%a, %d %b %Y %H:%M:%S %z")
                # Thu, 18 Apr 2019 19:32:58 +0800
            elif '/' in post.published:
                published = datetime.strptime(post.published,
                                              "%Y/%m/%d %H:%M:%S %z")
            elif 'Z' == post.published[-1]:
                post.published = post.published.replace('.000Z', 'Z')
                published = datetime.strptime(post.published,
                                              "%Y-%m-%dT%H:%M:%SZ")

            # <pubDate>15 Jun 2019 06:30:00 EST</pubDate>
            elif 'EST' in post.published:
                post.published = post.published[:-4]
                published = datetime.strptime(post.published,
                                              "%d %b %Y %H:%M:%S")
            elif 'T' in post.published:
                # 2019-05-24T15:05:50-04:00
                post.published = post.published[:-6]
                # tz = post.published[-6:].replace(':', '')
                published = datetime.strptime(post.published,
                                              "%Y-%m-%dT%H:%M:%S")
                # published = published.replace(tzinfo=FixedOffset(tz))
            elif post.published.count(' ') == 1:
                published = datetime.strptime(post.published,
                                              "%Y-%m-%d %H:%M:%S")
            else:
                published = datetime.strptime(post.published,
                                              "%Y-%m-%d %H:%M:%S %z")
            published = published.timestamp()
        else:
            if random.random() > 0.9:
                print('no published time')
            published = time.time()

        title = post.title
        link = post.link
        author = ''
        if hasattr(post, 'source'):
            source_title = post.source.title
            # print(source_title)
            print(rss_name, source_title)
            if rss_name == '虎嗅':
                pass
                author = source_title
            else:
                assert rss_name in source_title
            # assert rss_name == source_title
        source = rss_name

        if hasattr(post, 'category_title'):
            category = post.category_title
            assert ' ' not in category
            assert ',' not in category
            tags = [category]
        elif hasattr(post, 'tags'):
            tags = post.tags
            # print(tags)
            # assert len(tags) == 1
            # tags = tags[0]['term']
            tags = ','.join([t['term'] for t in tags])
            category = ''
            if '-' in tags:
                print(tags)
            tags = tags.replace(' ', '-')
            tags = tags.split(',')
            for tag in tags:
                if ' ' in tag:
                    print(tag)
        else:
            # print('no category')
            category = ''
            tags = []
        sharetype = 'rss'
        try:
            markdown = html2text.html2text(content)
        except Exception as e:
            print('error in html-to-markdown: {}'.format(e))
            continue
        assert link
        res = {
            'title': title,
            'link': link,
            'source': source,
            'category': category,
            'content': content,
            'summary': summary,
            'sharetype': sharetype,
            'tags': tags,
            'markdown': markdown,
            'published': published,
            'updated': time.time(),
        }
        # print(post.keys())
        if hasattr(post, 'author'):
            # TODO
            print('author: ', post.author)
            res['author'] = post.author
        else:
            res['author'] = author

        # 去重方案
        # - 标题重复
        found = Share.find({'title': title})
        if found.count():
            if found.count() > 1:
                print('!! repeated article title: {}'.format(title))
            elif found.count() == 1:
                # continue
                share = Share.by_sid(found[0].id)
                if share and summary and not share.link and link:
                    print(res['link'])
                    print('title {} updated'.format(title))
                    share.update(res)
                    share.save()
        else:
            print('title {} adding'.format(title))
            email = '{}@anwensf.com'.format(rss_hostname)
            auser = User.by_email(email)
            assert auser
            share = Share
            user_id = auser.id
            res['user_id'] = user_id  # just use 1 as default
            # continue
            assert res['link']
            share = share.new(res)

            user = User.by_sid(user_id)
            user.user_leaf += 10
            user.save()
            for i in tags:
                doc = {'name': i, 'share_ids': share.id}
                Tag.new(doc)