Exemplo n.º 1
0
    def _post_comment(self, index_url, comment_str, author_ip, user):
        title = urlparse(index_url).netloc

        # print comment_str, user
#        logger.info(u'评论:' + comment_str
#                     + u'IP:' + author_ip
#                     + u' 用户:' + str(user)
#                     + u' TITLE:' + title
#                     + u' INDEX:' + index_url)

        comment_board, created = CommentBoard.objects.get_or_create(
                                    url=index_url,
                                    title=title)
        comment_board.save() if created else None
        if user.is_authenticated():
            author = get_author(user)
            comment = Comment(
                    time_added=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    time_modified=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    comment_str=comment_str,
                    comment_board=comment_board,
                    author_ip=author_ip,
                    title=title,
                    author=author) # 以后换成author,现在先用user
        else:
            '''
            Annoymous User access the site.
            '''
            comment = Comment(
                    time_added=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    time_modified=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    comment_str=comment_str,
                    comment_board=comment_board,
                    title=title,
                    author_ip=author_ip)

        # Generate top 5 tags for comment.
        comment.tags = jieba.analyse.extract_tags(comment_str, topK=3)
        logger.info('tags: ' + repr(comment.tags).decode("unicode-escape"))
        comment.save()
Exemplo n.º 2
0
    def _post_comment(url_b64, comment_str, author_ip, user):

        index_url = base64.b64decode(url_b64.encode('ascii', 'ignore'), '+-')
        try:
            title = urlparse(index_url).netloc
        except Exception as e:
            print index_url, e
            return None

        comment = Comment(
                time_added=datetime.datetime.utcnow().replace(
                                tzinfo=utc),
                time_modified=datetime.datetime.utcnow().replace(
                                tzinfo=utc),
                comment_str=comment_str,
                url_b64 = url_b64,
                author_ip = author_ip,
                title = title,
                )


        comment.save()#need to save here to create the ID

        # Generate top 5 tags for comment.
        stags= jieba.analyse.extract_tags(comment_str, topK=3)
        for stag in stags:
            tags = Tag.objects.filter(name=stag)
            if len(tags) > 0:
                comment.tags.append(tags[0])
                tags[0].comments.append(comment.id)
                tags[0].save()
            else:
                tag = Tag.objects.create(
                        name=stag,
                        time_added=datetime.datetime.utcnow().replace(
                                        tzinfo=utc),
                        )
                tag.comments.append(comment.id)
                tag.save()
                comment.tags.append(tag)
        #logger.info('tags: ' + repr(comment.tags).decode("unicode-escape"))
        comment.save()
Exemplo n.º 3
0
    def _post_comment(url_b64, comment_str, author_ip, user):

        index_url = base64.b64decode(url_b64.encode('ascii', 'ignore'), '+-')
        try:
            title = urlparse(index_url).netloc
        except Exception as e:
            print index_url, e
            return None

        comment = Comment(
            time_added=datetime.datetime.utcnow().replace(tzinfo=utc),
            time_modified=datetime.datetime.utcnow().replace(tzinfo=utc),
            comment_str=comment_str,
            url_b64=url_b64,
            author_ip=author_ip,
            title=title,
        )

        comment.save()  #need to save here to create the ID

        # Generate top 5 tags for comment.
        stags = jieba.analyse.extract_tags(comment_str, topK=3)
        for stag in stags:
            tags = Tag.objects.filter(name=stag)
            if len(tags) > 0:
                comment.tags.append(tags[0])
                tags[0].comments.append(comment.id)
                tags[0].save()
            else:
                tag = Tag.objects.create(
                    name=stag,
                    time_added=datetime.datetime.utcnow().replace(tzinfo=utc),
                )
                tag.comments.append(comment.id)
                tag.save()
                comment.tags.append(tag)
        #logger.info('tags: ' + repr(comment.tags).decode("unicode-escape"))
        comment.save()
Exemplo n.º 4
0
    def _post_comment(index_url, comment_str, author_ip, user):
        try:
            title = urlparse(index_url).netloc
        except Exception as e:
            print index_url, e
            return None

        # print comment_str, user
        logger.info(u'评论:' + comment_str
                     + u'IP:' + author_ip
                     + u' 用户:' + str(user)
                     + u' TITLE:' + title
                     + u' INDEX:' + index_url
                     + u' UTC:' + str(datetime.datetime.utcnow().replace(tzinfo=utc))
                     + u' Time:' + str(datetime.datetime.now()))

        comment_board, created = CommentBoard.objects.get_or_create(
                                    url=index_url,
                                    title=title)
        comment_board.save() if created else None
        if user and user.is_authenticated():
            author = get_author(user)
            comment = Comment(
                    time_added=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    time_modified=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    comment_str=comment_str,
                    comment_board=comment_board,
                    author_ip=author_ip,
                    title=title,
                    author=author)
            author.comments_sum += 1
            author.points += 5
            author.save()
        else:
            '''
            Annoymous User access the site.
            '''
            comment = Comment(
                    time_added=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    time_modified=datetime.datetime.utcnow().replace(
                                    tzinfo=utc),
                    comment_str=comment_str,
                    comment_board=comment_board,
                    title=title,
                    author_ip=author_ip)

        # get the page's screenshot and save url_b64 to comment
        url_b64 = base64.b64encode(index_url, '+-')
        shot_process = Process(target=save_pageshot, args=(index_url, url_b64))
        shot_process.start()
        comment.url_b64 = url_b64

        comment.save()#need to save here to create the ID

        # Generate top 5 tags for comment.
        stags= jieba.analyse.extract_tags(comment_str, topK=3)
        for stag in stags:
            tags = Tag.objects.filter(name=stag)
            if len(tags) > 0:
                comment.tags.append(tags[0])
                tags[0].comments.append(comment.id)
                tags[0].save()
            else:
                tag = Tag.objects.create(
                        name=stag,
                        time_added=datetime.datetime.utcnow().replace(
                                        tzinfo=utc),
                        )
                tag.comments.append(comment.id)
                tag.save()
                comment.tags.append(tag)
        #logger.info('tags: ' + repr(comment.tags).decode("unicode-escape"))
        comment.save()
Exemplo n.º 5
0
    def _post_comment(index_url, comment_str, author_ip, user):
        try:
            title = urlparse(index_url).netloc
        except Exception as e:
            print index_url, e
            return None

        # print comment_str, user
        logger.info(u'评论:' + comment_str + u'IP:' + author_ip + u' 用户:' +
                    str(user) + u' TITLE:' + title + u' INDEX:' + index_url +
                    u' UTC:' +
                    str(datetime.datetime.utcnow().replace(tzinfo=utc)) +
                    u' Time:' + str(datetime.datetime.now()))

        comment_board, created = CommentBoard.objects.get_or_create(
            url=index_url, title=title)
        comment_board.save() if created else None
        if user and user.is_authenticated():
            author = get_author(user)
            comment = Comment(
                time_added=datetime.datetime.utcnow().replace(tzinfo=utc),
                time_modified=datetime.datetime.utcnow().replace(tzinfo=utc),
                comment_str=comment_str,
                comment_board=comment_board,
                author_ip=author_ip,
                title=title,
                author=author)
            author.comments_sum += 1
            author.points += 5
            author.save()
        else:
            '''
            Annoymous User access the site.
            '''
            comment = Comment(
                time_added=datetime.datetime.utcnow().replace(tzinfo=utc),
                time_modified=datetime.datetime.utcnow().replace(tzinfo=utc),
                comment_str=comment_str,
                comment_board=comment_board,
                title=title,
                author_ip=author_ip)

        # get the page's screenshot and save url_b64 to comment
        url_b64 = base64.b64encode(index_url, '+-')
        shot_process = Process(target=save_pageshot, args=(index_url, url_b64))
        shot_process.start()
        comment.url_b64 = url_b64

        comment.save()  #need to save here to create the ID

        # Generate top 5 tags for comment.
        stags = jieba.analyse.extract_tags(comment_str, topK=3)
        for stag in stags:
            tags = Tag.objects.filter(name=stag)
            if len(tags) > 0:
                comment.tags.append(tags[0])
                tags[0].comments.append(comment.id)
                tags[0].save()
            else:
                tag = Tag.objects.create(
                    name=stag,
                    time_added=datetime.datetime.utcnow().replace(tzinfo=utc),
                )
                tag.comments.append(comment.id)
                tag.save()
                comment.tags.append(tag)
        #logger.info('tags: ' + repr(comment.tags).decode("unicode-escape"))
        comment.save()