def create_comment(post_id): ''' file: ./documentation/create_comment.yml ''' post = Post.query.filter_by(id=post_id).first() if post is not None: request_body = json.loads(request.data) # Code here checks for blank body requests / @beforerequests checks for None body requests if not request_body.get('text') == '' and not request_body.get( 'username') == '': comment = Comment(text=request_body.get('text'), username=request_body.get('username'), post_id=post_id) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({ 'success': True, 'data': comment.serialize() }), 201 return json.dumps({ 'success': False, 'error': 'invalid body format' }), 412 return json.dumps({'success': False, 'error': 'Post not found!'}), 404
def create_comment(user_id, post_id, body): comment = Comment(timestamp=datetime.datetime.now(), content=body.get("content"), edited=False, user_id=user_id, post_id=post_id) db.session.add(comment) db.session.commit() return comment.serialize()
def create_comment(post_id): post = Post.query.filter_by(id=post_id).first() if post is None: return json.dumps({'success': False, 'error': 'Post not found!'}), 404 comment_body = json.loads(request.data) comment = Comment(text=comment_body.get('text'), username=comment_body.get('username'), post_id=post.id) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201
def getComment(): Comment.drop_collection() baseurl = "http://www.1point3acres.com/bbs/forum.php?mod=forumdisplay&fid=28&sortid=192&filter=sortid&sortid=192&page=" for i in range(1, 142): r = requests.get(baseurl + str(i)) soup = BeautifulSoup(r.text) table = soup.find_all("table", summary="forum_28")[0] for tbody in table.find_all("tbody")[1:]: url = a = tbody.find_all("a", class_="s xst")[0].attrs['href'] td = tbody.find_all("td", class_="num")[0] num = int(td.a.text) fetchComment((url, num)) print "done,{}/{}".format(i, 142)
def create_comment(userid, postid): post_body = json.loads(request.data) try: comment = Comment(text=post_body.get('text'), user_id=userid, post_id=postid) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201 except KeyError as e: return json.dumps({'success': False, 'data': 'Invalid input'}), 404
def post_com(post_id): post = Post.query.filter_by(id=post_id).first() if post is not None: com_body = json.loads(request.data) com = Comment( content=com_body.get("content"), username=com_body.get("username"), post_id=post_id, ) post.comments.append(com) db.session.add(com) db.session.commit() return json.dumps({"success": True, "data": com.serialize()}) return json.dumps({"success": False, "error": "Post not found"}), 404
def create_comment(post_id): post = Post.query.filter_by(id=post_id).first() if not post: return failure_response('Post not found') body = json.loads(request.data) name = body.get('name') description = body.get('description') if not body: return failure_response('Missing required field') new_comment = Comment(netid = body.get("netid"), name = name, description = description) post.comments.append(new_comment) db.session.add(new_comment) db.session.commit() return success_response(new_comment.serialize(), 201)
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()
def add_post_comment(post_id): post = Post.query.filter_by(id=post_id, active=True).first() if post is None: return nopost() comment_body = extract(request) if 'text' not in comment_body or 'token' not in comment_body: return missing() uid = token_to_uid(comment_body) if uid is None: return invalid_token() comment = Comment(text=comment_body.get('text'), uid=uid, post_id=post_id, creation_time=time.time()) activate(uid) post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': comment.serialize()}), 201
def fake_comment(self, count: int = 100): """构造虚拟评论""" for i in range(count): comment_obj = Comment( author=self.fake.name(), content=self.fake.sentence(), post=session.query(Post).get(random.randint(1, session.query(Post).count())), ) session.add(comment_obj) session.commit()
def write_to_database(q): counter = 0 while True: json_filepath = q.get() data = None with open(json_filepath, 'r') as f: data = json.load(f) for json_item in data['data']: if 'body' in json_item: # if 'body' is present then assume it's a comment db_record = db_Comment.get_or_none( db_Comment.id == json_item['id']) if not db_record: json_item['body'] = clean_text(json_item['body']) db_record = db_Comment.create(**json_item) print(f"comment {json_item['id']} written to database") counter += 1 elif 'selftext' in json_item: # if 'selftext' is present then assume it's a submission db_record = db_Submission.get_or_none( db_Submission.id == json_item['id']) if not db_record: json_item['selftext'] = clean_text(json_item['selftext']) db_record = db_Submission.create(**json_item) print(f"submission {json_item['id']} written to database") counter += 1 q.task_done()
def post_comment(post_id): post = Post.query.filter_by(id=post_id).first() if not post: return json.dumps({'success': False, 'error': 'Post not found'}), 404 post_body = json.loads(request.data) body_comment = post_body.get('body') username = post_body.get('username') user = User.query.filter_by(username=username).first() if not user: return json.dumps({'success': False, 'error': 'user not found'}), 404 user_id = user.id newComment = Comment(body_comment=body_comment, time_stamp=datetime.now(), date=date.today(), user_id=user_id, post_id=post_id) db.session.add(newComment) db.session.commit() return json.dumps({'success': True, 'data': newComment.serialize()}), 201
def create_comment(trail_id, review_id): trail = Trail.query.filter_by(id=trail_id, ).first() if trail is None: return failure_response('Trail not found!') review = Review.query.filter_by(id=review_id, trail=trail).first() if review is None: return failure_response('Review not found!') body = json.loads(request.data) new_comment = Comment(body=body.get('body'), username=body.get('username'), review_id=review_id, review=review) if new_comment.body is None or new_comment.username is None: return failure_response('Could not create comment!') review.comments.append(new_comment) db.session.add(new_comment) db.session.commit() return success_response(new_comment.serialize(), 201)
def fake_reply(self, count: int = 50): # 回复评论 for i in range(count): reply_obj = Comment( author=self.fake.name(), content=self.fake.sentence(), post=session.query(Post).get(random.randint(1, session.query(Post).count())), replied=session.query(Comment).get(random.randint(1, session.query(Comment).count())) ) session.add(reply_obj) session.commit()
def comment_post(post_id): """Vote on a post.""" post_body = json.loads(request.data) post = Post.query.filter_by(id=post_id).first() if post is not None: comment = Comment(text=post_body['text'], username=post_body['username'], post_id=post.id) post.comment_count = post.comment_count + 1 post.comments.append(comment) db.session.add(comment) db.session.commit() return json.dumps({'success': True, 'data': post.serialize()}), 200 return json.dumps({'success': False, 'error': 'Invalid post!'}), 404
def get_comments(share, like_commentids, dislike_commentids): 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) print(like_commentids, comment.id) print(dislike_commentids, comment.id) comment.is_liking = comment.id in like_commentids comment.is_disliking = comment.id in dislike_commentids comments.append(comment) return comments
def comment_on_item(post_id): post = Post.query.filter_by(id=post_id).first() if post is None: return failure_response('Item not found') if post.active != None and post.active != True: return failure_response('Item inactive') body = json.loads(request.data) if body.get('content') is None: return failure_response('No message provided') if not logged_in(current_user): return failure_response('User not logged in') new_comment = Comment(sender=current_user.id, content=body.get('content'), post=post_id) db.session.add(new_comment) db.session.commit() return success_response(post.serialize())
def storeData(self, res): comment = Comment(comment=res) comment.save()
from db import Genre, Film, User, Comment path = "kinopoisk.sql" def convertToBinaryData(filename): # Convert digital data to binary format with open(filename, 'rb') as file: blobData = file.read() return blobData def write_file(data, filename): # Convert binary data to proper format and write it on Hard Disk with open(filename, 'wb') as file: file.write(data) genresTable = Genre(path) films = Film(path) users = User(path) commentTable = Comment(path) tupleGenres = [] genre_map = {} listGenres = genresTable.get_genres() for i in listGenres: genre_map[i[1]] = i[0] tupleGenres.append(i[1])
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")
def gather_comments_for_submission(sub): if any(s in sub.selftext for s in negative_keywords): # if the submission contains a negative keyword, # ignore it so we don't train the bot on bad stuff print(f"{sub.id} contains negative keywords") return if any(s in sub.selftext for s in text_removed): # if the post has been edited or deleted it might contain [removed] or [deleted] # if it does, ignore this submission because we can't train on that print(f"blacklist selftext: {sub.selftext}") return if sub.author.lower() in author_blacklist: print(f"author blacklist {sub.author}") return # pick out all of the comments in this submission(topic) top_rated_comments = list(db_Comment.select().where( (db_Comment.link_id == f't3_{sub.id}') & (fn.Lower(db_Comment.author.not_in(author_blacklist))))) for tr_comment in top_rated_comments: # Here, we will start to create a string representation of the reddit submission, with all comments in a thread # in chronological order print(f"starting top rated comments loop {sub.id}") # this is the end of the training string.. all text will be prepended to this if tr_comment.submission().is_self: # is_self parameter means it is a selftext submission text_gen_string = "<|eoss|>" else: # Otherwise it is a link submission (ie just a title and URL) text_gen_string = "<|eols|>" ancestor = tr_comment comments_counted = 0 # From the top rated comment, we'll loop back up the comment thread until # we reach the submission # Then we have the submission text and comment text all in the correct reply # order that represents how humans have a conversation while ancestor is not None: if (ancestor.author.lower() in author_blacklist or ancestor.author.lower().endswith('bot')): # is probably a bot account, break the loop break if isinstance(ancestor, db_Comment): if any(s in ancestor.body for s in text_removed): print("blacklist text... breaking") break record_string = f"<|sor|>{ancestor.body}<|eor|>" # build the text_gen_string up backwards text_gen_string = record_string + text_gen_string comments_counted += 1 elif isinstance(ancestor, db_Submission): if ancestor.is_self: # is_self parameter means it is a selftext submission record_string = f"<|soss|><|sot|>{ancestor.title}<|eot|><|sost|>{ancestor.selftext}<|eost|>" else: # if there's no selftext then it's just a linkpost. record_string = f"<|sols|><|sot|>{ancestor.title}<|eot|><|sol|>{ancestor.url}<|eol|>" text_gen_string = record_string + text_gen_string break # get the next comment up in the thread and compile the text for that, too. ancestor = ancestor.parent() if text_gen_string.startswith("<|sols") or text_gen_string.startswith( '<|soss') and comments_counted > 0: # sols/soss is in the thread so we reached the submission and counted at least one comment # that's sufficient to add into the training output data. return text_gen_string
db.session.commit() print('Added Question') ## create Answer resource to db answer1 = Answer( question_id=1, # has to exist answer_text='test_answer_text', explanation_text='test_explanation_text', is_correct=1) db.session.add(answer1) db.session.commit() print('Added Answer') ## create Comment resource to db comment1 = Comment( user_id=1, # has to exist question_id=1, # has to exist comment_text='test_comment_text') db.session.add(comment1) db.session.commit() print('Added Comment') ## create Quiz resource to db quiz1 = Quiz(user_id=1, created=datetime.now(), completed=datetime.now(), result='test_result', number_of_questions=1) db.session.add(quiz1) db.session.commit() print('Added Quiz')