Exemplo n.º 1
0
    def _get_comments(self, foreign_id=None):
        comments = []
        comments_counter = 0
        comments_page_counter = 0
        while True:
            comments_page_counter += 1
            response = requests.get(self._comments_url.format(foreign_id, comments_page_counter))
            if response.status_code == 404:
                return comments
            json_top_comments_list = json.loads(response.content)['data']
            for json_top_comment in json_top_comments_list:
                comments_counter += 1
                sub_comments_counter = 0
                top_comment = json_top_comment['Comment']
                top_comment_original_id = top_comment['id']

                top_text = top_comment['text']
                top_comment_id = str(comments_counter)
                parent_id = top_comment['parent_id']
                comments.append(Comment(top_comment_id, parent_id, top_text))
                if 'SubComment' not in json_top_comment:
                    continue
                else:
                    json_sub_comments = json_top_comment['SubComment']
                sub_comment_ids = []
                for json_sub_comment in json_sub_comments:
                    sub_comment_ids.append(json_sub_comment['id'])
                    sub_comments_counter += 1
                    sub_text = json_sub_comment['text']
                    sub_id = "%s-%d" % (top_comment_id, sub_comments_counter)
                    sub_parent_id = top_comment_id
                    comments.append(Comment(sub_id, sub_parent_id, sub_text))
                comments.extend(
                    self._get_sub_comments(foreign_id, top_comment_original_id, sub_comment_ids, top_comment_id))
Exemplo n.º 2
0
    def _get_facebook_comments(self, **kwargs):
        """
        Scrape all Facebook comments using Facebook comments URL.
        :param facebook_id: Facebook ID
        :param domain: domain
        :param url: url
        :return:
        """
        comments = []
        fb_comments_response = requests.get(
            self._comments_url.format(kwargs['facebook_id'], kwargs['domain'],
                                      kwargs['url'])).content.decode('utf-8')
        fb_comments_json = json.loads(
            re.search(r'handleServerJS\(({\"instances\".*)\);\}',
                      fb_comments_response).group(1))
        target_fb_id = fb_comments_json['require'][2][3][0]['props']['meta'][
            'targetFBID']

        fb_pager_url = "https://www.facebook.com/plugins/comments/async/{}/pager/time".format(
            target_fb_id)
        top_comments_response = requests.post(fb_pager_url,
                                              data={
                                                  '__a': 1,
                                                  'limit': 5000
                                              }).content.decode('utf-8')
        top_comments_json = json.loads(
            re.search('({.*})', top_comments_response).group())

        top_comments_list = top_comments_json['payload']['commentIDs']
        top_comments_counter = 0
        for top_comment_id in top_comments_list:
            top_comments_counter += 1
            comments.append(
                Comment(comment_id=str(top_comments_counter),
                        parent_comment_id=',',
                        text=top_comments_json['payload']['idMap']
                        [top_comment_id]['body']['text']))

            sub_comments_url = 'https://www.facebook.com/plugins/comments/async/comment/{}/pager'.format(
                top_comment_id)
            sub_comments_response = requests.post(sub_comments_url,
                                                  data={
                                                      '__a': '1'
                                                  }).content.decode('utf-8')
            sub_comments_json = json.loads(
                re.search('({.*})', sub_comments_response).group())

            sub_comments_counter = 0
            for sub_comment_id in sub_comments_json['payload']['commentIDs']:
                sub_comments_counter += 1
                sub_comment_text = sub_comments_json['payload']['idMap'][
                    sub_comment_id]['body']['text']
                comments.append(
                    Comment(comment_id="%d-%d" %
                            (top_comments_counter, sub_comments_counter),
                            parent_comment_id=str(top_comments_counter),
                            text=sub_comment_text))
        return comments
Exemplo n.º 3
0
def example_data():
    """Create some sample data for tests file to use."""

    # Create some fake users
    u1 = User(user_id=1,
              email="*****@*****.**",
              name="Jessi DiTocco",
              password="******")
    u2 = User(user_id=2,
              email="*****@*****.**",
              name="Liz Lee",
              password="******")

    # Create some fake events
    e1 = Event(event_id="43104373341",
               name="Aristophanes @ Cafe du Nord",
               start_time="Friday, February 16 at 9:30PM+",
               end_time="Saturday, February 17 at 2:00AM",
               address="2174 Market Street, San Francisco, CA 94114",
               latitude=37.7649659,
               longitude=-122.431,
               venue_name="Slate")
    e2 = Event(event_id="41465350981",
               name="Funk",
               start_time="Friday, February 15 at 9:30PM+",
               end_time="Saturday, February 18 at 2:00AM",
               address="4123 Market Street, San Francisco, CA 94114",
               latitude=39.7649659,
               longitude=-122.431,
               venue_name="The Jam")

    # Create some bookmark types
    bt1 = BookmarkType(bookmark_type_id=1, bookmark_type="going")
    bt2 = BookmarkType(bookmark_type_id=2, bookmark_type="interested")

    # Create some fake bookmarks
    b1 = Bookmark(bookmark_id=1,
                  user_id=1,
                  event_id="43104373341",
                  bookmark_type_id=2)
    b2 = Bookmark(bookmark_id=2,
                  user_id=2,
                  event_id="43104373341",
                  bookmark_type_id=1)

    # Create some fake Comments
    c1 = Comment(comment_id=1,
                 user_id=1,
                 event_id="43104373341",
                 comment="HI!")
    c2 = Comment(comment_id=2,
                 user_id=2,
                 event_id="43104373341",
                 comment="HI!")

    db.session.add_all([u1, u2, e1, e2, bt1, bt2, b1, b2, c1, c2])
    db.session.commit()
Exemplo n.º 4
0
def add_post_comment(post_id, form_data):
    """Add a comment to a post in the database."""

    time_stamp = datetime.now()
    comment_body = form_data.get("comment")

    user_type = get_user_type_from_session()

    if user_type == "patient":
        author_type = "pat"
        patient = get_current_patient()
        author_id = patient.patient_id

    else:
        author_type = "diet"
        dietitian = get_current_dietitian()
        author_id = dietitian.dietitian_id

    new_comment = Comment(post_id=post_id,
                          author_type=author_type,
                          author_id=author_id,
                          time_stamp=time_stamp,
                          comment_body=comment_body)

    db.session.add(new_comment)
    db.session.commit()

    return (new_comment)
Exemplo n.º 5
0
def home():

    if request.method == 'POST':
        comment = request.form['comment']
        user = User.query.get(current_user.get_id())
        lat = request.form['lat']
        lng = request.form['lng']
        hours = request.form['hours']

        pollingcenter = PollingCenter.query.filter_by(lat=lat, lng=lng).first()
        if not pollingcenter:
            pollingcenter = PollingCenter(lat=lat,
                                          lng=lng,
                                          hours_of_operation=hours)
            db.session.add(pollingcenter)
            db.session.commit()

        new_comment = Comment(comment=comment,
                              polling_id=pollingcenter.polling_id,
                              user_id=user.id)
        db.session.add(new_comment)
        db.session.commit()

        flash("Comment submitted")
    return render_template('home.html')
Exemplo n.º 6
0
def get_comment_list(id, start_page=1, max_page=50):
    """获取评论列表"""
    url = "https://m.weibo.cn/api/comments/show?id=" + str(id)
    page = start_page
    session = requests.session()
    comments_list = []
    while page <= max_page:
        req = prepare_requset(url=url + "&page=" + str(page))
        datas = get_page(req=req, session=session).json()
        if datas.get('ok') != 1:
            break
        else:
            for data in datas.get('data'):
                user = data.get('user')
                text = data.get('text')
                comment = Comment(id=data.get('id'),
                                  user_id=user.get('id'),
                                  user_name=user.get('screen_name'),
                                  source=data.get('source'),
                                  created_at=data.get('created_at'),
                                  text=re.sub(r'<.*?>', '|', text),
                                  reply=data.get('reply_id'))
                comments_list.append(comment)
            page += 1
    return comments_list
Exemplo n.º 7
0
def post_detail_comments_form(post_id):
    """Show details about a post and save comments."""

    # Get variables
    user_id = session.get("user_id")
    if not user_id:
        flash("Please log in to access posts.")
        return redirect("/login")

    cid = request.form["comment_id"]
    parent = request.form["parent"]
    date_created = request.form["date_created"]
    # TODO: check if date_modified should be in another function or if loop
    # date_modified = request.form["date_modified"]
    content = request.form["content"]
    upvotes = request.form["upvote_count"]

    # TODO: iterate through form variables to eliminate blanks
    # TODO: iterate through form variables to verify data formats

    # comment = Comment(comment_id=comment_id, user_id=user_id, post_id=post_id, parent=parent,
    #                   date_created=date_created, date_modified=date_modified, content=content,
    #                   upvotes=upvote_count)
    comment = Comment(user_id=user_id, post_id=post_id, cid=cid, parent=parent,
                      date_created=date_created, content=content, upvotes=upvotes)
    db.session.add(comment)
    db.session.commit()
    flash("Comment added.")

    # return redirect("/posts/detail/%s" % post.post_id)

    # return render_template("post_comments.html", user_post=user_post, post_comments=post_comments)
    return jsonify({'status': 'success'})
Exemplo n.º 8
0
def add_aritcle_comment(userid):
    user = User.objects(id=userid).first()
    body = request.json
    content = body.get('content')
    articleid = body.get('target')
    comment = Comment(
        content=content,
        user=user
    )
    article = Article.objects(id=articleid).first()
    article.comments.append(comment)
    article.save()

    return jsonify({
        "message": 'OK',
        "data": {
            "com_id": -1,
            "target": articleid,
            "art_id": articleid,
            "new_obj": {
                "com_id": -1,
                "aut_id": str(user.id),
                "pubdate": comment.created,
                "content": comment.content,
                "is_top": 0,
                "aut_name": user.name,
                "aut_photo": user.photo,
                "like_count": 0,
                "reply_count": 0
            }
        }
    })
Exemplo n.º 9
0
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          post=post,
                          author=current_user._get_current_object())
        db.session.add(comment)
        flash('Your comment has been published.')
        return redirect(url_for('.post', id=post.id, page=-1))
    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (post.comments.count() - 1) / \
               current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
    if page == 0:
        page = 1
    print(page)
    pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(
        page,
        per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
        error_out=False)
    comments = pagination.items
    return render_template('post.html',
                           posts=[post],
                           form=form,
                           comments=comments,
                           pagination=pagination)
Exemplo n.º 10
0
    def _get_sub_comments(self, foreign_id, parent_id, sub_comment_ids, parent_local_id):
        comments = []
        if len(sub_comment_ids) == 0:
            return comments
        page = 1
        while True:
            sub_comments_url = 'http://www.politika.rs/api/v1/getComments/{}?page={}&parent_id={}&ids={}'.format(
                foreign_id, page, parent_id, ",".join(sub_comment_ids))
            sub_comments_response = requests.get(sub_comments_url).content.decode('utf-8-sig')
            try:
                sub_comments_json = json.loads(sub_comments_response)
            except json.JSONDecodeError:
                break
            if 'data' not in sub_comments_response or len(sub_comments_json['data']) == 0:
                break
            sub_comments_counter = len(sub_comment_ids) + 1
            for sub_comment in sub_comments_json['data']:
                sub_text = sub_comment['Comment']['text']
                sub_id = "%s-%d" % (parent_local_id, sub_comments_counter)
                comments.append(Comment(sub_id, parent_local_id, sub_text))
                sub_comments_counter += 1

            page += 1

        return comments
Exemplo n.º 11
0
def test_comments():
    comment = request.form.get('comment')

    #Comment Location data
    start = session.get('start')
    end = session.get('end')
    article_id = session.get('article_id')

    user_id = session.get('user_id')

    phrase_key = (article_id, start, end)

    new_comment = Comment(phrase_id=phrase_key,
                          user_id=user_id,
                          article_id=article_id,
                          comment=comment)
    db.session.add(new_comment)
    db.session.commit()

    user = User.query.get(user_id)

    user_name = user.first_name

    image = user.image

    commentData = [{
        'userName': user_name,
        'userComment': comment,
        'userImage': image
    }]

    print "\n\n\n\n\n\n %s \n\n\n\n\n\n" % commentData
    return jsonify(commentData=commentData)
Exemplo n.º 12
0
    def extract_comments(self, sid, text):
        """
        Extracts comments from a XML string. The parsing is done
        using feedparser library.

            :param sid: the id of the new.
            :param text: the RSS xml.

    """
        parsed = feedparser.parse(text)
        try:
            published = parsed.feed.published
        except AttributeError:
            published = parsed.feed.updated

        comments = []
        for comment in parsed.entries:
            meneame_comment = Comment(sid)
            meneame_comment.order = comment['meneame_order']
            meneame_comment.karma = comment['meneame_karma']
            meneame_comment.user = comment['meneame_user']
            meneame_comment.votes = comment['meneame_votes']
            meneame_comment.id = comment['meneame_comment_id']
            try:
                meneame_comment.published = comment.published
            except AttributeError:
                meneame_comment.published = comment.updated
            meneame_comment.summary = comment.summary
            comments.append(meneame_comment)

        return comments, published
Exemplo n.º 13
0
def create_comment(user, news, comment_text):
    comment = Comment(commenter_user=user,
                      news=news,
                      comment_text=comment_text)
    db.session.add(comment)
    db.session.commit()
    return comment
Exemplo n.º 14
0
def post_comment():

    comment = request.form['comment']
    site_id = request.form['site_id']

    print comment

    new_comment = Comment(comment_string=comment,
                          user_id=session['user_id'],
                          site_id=site_id,
                          date=datetime.datetime.today().date())

    db.session.add(new_comment)
    db.session.commit()

    #get the user's profile photo and base64 encode it
    #send the comment and photo in a JSON
    profile_photo = base64.b64encode(new_comment.user.photo)

    success = {
        'comment': new_comment.comment_string,
        "commenter_fname": new_comment.user.fname,
        "commenter_lname": new_comment.user.lname,
        "profile_photo": profile_photo
    }

    return jsonify(success)
Exemplo n.º 15
0
def comment():
    """
        comment handler, add a new comment to DB
        give a new notification to post poster
        and send a validate email
    """
    zid = session['zid']
    user = User.findByKey(zid)
    if not user:
        return redirect(url_for('login'))
    pid = request.form.get('pid')
    poster_zid = request.form.get('poster_zid')
    m_user = User.findByKey(poster_zid)
    message = request.form.get('message')
    new_comment = Comment(pid=pid, zid=zid, message=message)
    new_comment.save()
    flash('Success: you have post a new comment.')
    notification = Notifications(from_zid=user.zid, to_zid=poster_zid, noti_type='reply', from_name=user.full_name, from_img=user.image, pid=pid)
    # notification.pid = pid
    notification.save()
    mail = MailUtils('*****@*****.**', 'MateLook Team', m_user.full_name)
    mail.notificaion()
    post = Post.findByKey(pid)
    poster = User.findByKey(post.zid)
    page = int(request.args.get('page', '1'))
    total = len(post.getComments())
    pag = Pagination(page, total)
    return redirect(url_for('postlook', pid=pid, page=page))
Exemplo n.º 16
0
 def post(self, post_id):
     blogpost = self.get_blogpost(post_id)
     user = self.get_user_object()
     if user:
         if blogpost:
             # To detect which form is sent, the like or the comment
             if "likeBtn" in self.request.POST:
                 if blogpost.author.key().id() == user.key().id():
                     error = "You cannot like your own post!"
                     comments = self.get_comments(blogpost)
                     self.render_content(blogpost=blogpost,
                                         user=user,
                                         comments=comments,
                                         error_like=error)
                 else:
                     like = LikePost(user=user, blogpost=blogpost)
                     like.put()
                     self.redirect('/%s' % post_id)
             else:
                 content = self.request.get("comment")
                 if content:
                     comment = Comment(user=user,
                                       blogpost=blogpost,
                                       content=content)
                     comment.put()
                     self.redirect('/%s' % post_id)
                 else:
                     error = "Please write something before posting!"
                     comments = self.get_comments(blogpost)
                     self.render_content(blogpost=blogpost,
                                         user=user,
                                         comments=comments,
                                         error_comment=error)
     else:
         self.redirect("/login")
Exemplo n.º 17
0
def add_comment(name, email, comment):
    """
	Add a comment to the database, given
	their name, email, and the comment.
	"""
    comment_object = Comment(name=name, email=email, comment=comment)
    session.add(comment_object)
    session.commit()
Exemplo n.º 18
0
    def comment_parse(comments_tag) -> Comment:
        com = Comment()

        com.name = comments_tag.select_one(".user .name").string
        com.score = len(comments_tag.select(".score-star li i.active"))
        com.context = comments_tag.select_one(".comment-content").string
        com.date = comments_tag.select_one(".time")["title"]
        return com
Exemplo n.º 19
0
def add_comment_to_db(user_id, event_id, comment):
    """Adds a comment to the database if the user is logged in."""

    # Add comment to the comments table
    comment = Comment(user_id=user_id, event_id=event_id, comment=comment)

    db.session.add(comment)
    db.session.commit()
Exemplo n.º 20
0
def create_comment(comment_text, star_rating):

    comment = Comment(comment_text=comment_text,star_rating=star_rating)
    
    db.session.add(comment)
    db.session.commit()

    return comment
Exemplo n.º 21
0
def load_data():
    """pull data from API and load into db"""
    page_num = 1
    # if type(num) == int:
    #     url = "https://refugerestrooms.org:443/api/v1/restrooms.json?page=1&per_page=" + str(page_num)
    # else:
    #     pass

    while True:
        url = "https://www.refugerestrooms.org:443/api/v1/restrooms.json?per_page=100&page=" + str(
            page_num)
        results = []
        response = requests.get(url)
        if response.status_code == 200:
            results = response.json()
            page_num += 1
        # loop thru json data
        for v in results:
            # add bathroom and location
            b = Bathroom(name=v['name'],
                         unisex=v['unisex'],
                         accessible=v['accessible'],
                         changing_table=v['changing_table'])
            db.session.add(b)
            db.session.commit()
            # add location
            if v['latitude'] == None or v['longitude'] == None:
                v['latitude'] = 0.00
                v['longitude'] = 0.00

                l = Location(bathroom_id=b.bathroom_id,street=v['street'],
                            city=v['city'], state=v['state'], \
                            country=v['country'], latitude=v['latitude'],
                            longitude=v['longitude'], directions=v['directions'])
                db.session.add(l)
                db.session.commit()
            # add comment
            if len(v['comment']) > 1:
                c = Comment(comment=v['comment'],
                            bathroom_id=b.bathroom_id,
                            user_id=0)
                db.session.add(c)
                db.session.commit()
            # add ratings
            if v['downvote'] == 1:
                r = Rating(bathroom_id=b.bathroom_id, user_id=0, score=2)
                db.session.add(r)
                db.session.commit()
            elif v['upvote'] == 1:
                r = Rating(bathroom_id=b.bathroom_id, user_id=0, score=5)
                db.session.add(r)
                db.session.commit()

            time.sleep(1)
        else:
            break

    return "finished loading data"
Exemplo n.º 22
0
 def post(self):
     arg_data = json.loads(self.get_argument('data'))
     user_info = self.session['index_user'].to_json()
     data = {"UserID": user_info['UserID'], "ProductID": arg_data['pid'],
             "Content": arg_data['user_comment_content'], "Status": False}
     obj = Comment(**data)
     session.add(obj)
     session.commit()
     self.write_json(u"新增成功、审核成功后即可显示!", code=1)
Exemplo n.º 23
0
Arquivo: crud.py Projeto: nbgal/frame
def add_comment(comment_date, commenter_id, img_id, comment_text):

    comment = Comment(comment_date=comment_date,
                      commenter_id=commenter_id,
                      img_id=img_id,
                      comment_text=comment_text)
    db.session.add(comment)
    db.session.commit()
    return comment
Exemplo n.º 24
0
def create_comment(comment, like, profile_id):
    """Create and return a new comment."""

    comment = Comment(comment=comment, like=like, profile_id=profile_id)

    db.session.add(comment)
    db.session.commit()

    return comment
Exemplo n.º 25
0
def create_comment(user, response):
    """Create and return a new comment."""

    comment = Comment(user=user, response=response)

    db.session.add(comment)
    db.session.commit()

    return comment
Exemplo n.º 26
0
def create_comment(comment, user_id, blog_id):
    """Create and return a new comment."""

    comment=Comment(comment=comment, user_id=user_id, blog_id=blog_id)

    db.session.add(comment)
    db.session.commit()

    return comment
Exemplo n.º 27
0
def create_a_comment(body):
    try:
        with create_session() as s:
            o = Comment()
            o.body = body
            s.add(o)
        return ''
    except Exception as e:
        logging.warning(e)
        return 'Write a comments failed'
Exemplo n.º 28
0
def add_comment_to_db(tag_id, user_id, content):
    """Update database with new comment"""

    comment = Comment(tag_id=tag_id,
                          user_id=user_id,
                          content=content)
    db.session.add(comment)
    db.session.commit()

    return comment
Exemplo n.º 29
0
async def api_create_comments(request, *, id, content):
    uid = next_id()
    comment = Comment(id=uid,
                      blog_id=id,
                      user_id=request.__user__.id,
                      user_name=request.__user__.name,
                      user_image=request.__user__.image,
                      content=content)
    await comment.save()
    return comment
Exemplo n.º 30
0
def save_comment_data(glitz_id, user_id, grid_id, comment_text):
    # save to DB
    now = datetime.datetime.now()

    comment = Comment(glitz_id=glitz_id, user_id=user_id, grid_id=grid_id, 
                      commented_on=now, comment_text=comment_text)

    db.session.add(comment)
    db.session.commit()

    return comment.comment_id