Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
Archivo: share.py Proyecto: 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)
Ejemplo 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)
Ejemplo n.º 6
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))
Ejemplo n.º 7
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)
Ejemplo n.º 8
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()
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
Archivo: user.py Proyecto: 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)
Ejemplo n.º 11
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))
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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))
Ejemplo n.º 16
0
Archivo: index.py Proyecto: 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)
Ejemplo n.º 17
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)
Ejemplo 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'.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()
Ejemplo n.º 19
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)
Ejemplo n.º 20
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)
Ejemplo 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
Ejemplo n.º 22
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
Ejemplo n.º 23
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()
Ejemplo n.º 24
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)
Ejemplo n.º 25
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))
Ejemplo n.º 26
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)
Ejemplo n.º 27
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()
Ejemplo n.º 28
0
Archivo: share.py Proyecto: 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)
Ejemplo n.º 29
0
Archivo: share.py Proyecto: 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")
Ejemplo n.º 30
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")