def get_comments(meal_id): if (meal_id is not None): db = current_app.config["db"] comments = db.get_comments(meal_id) return comments else: return None
def post(postid=None): isError = False error = "" curr_loc = database.get_location(postid) print curr_loc curr_comments = database.get_comments(postid) if request.method == "GET": if 'username' in session: user = database.get_user(session['username']); return render_template("post.html", location=curr_loc,get_timestamp=get_timestamp, comments=curr_comments, get_votes=database.get_votes_pst,postid=postid,session=session,user=user) return render_template("post.html", location=curr_loc,get_timestamp=get_timestamp, comments=curr_comments, get_votes=database.get_votes_pst,postid=postid,session=session) else: if 'username' in session: user=database.get_user(session['username']); rated() postid = postid if "content" in request.form: content = request.form['content'] if content == "": error = "You didn't write anything!" isError=True return render_template("post.html", error=error, isError=isError, location=curr_loc, get_timestamp=get_timestamp, comments=curr_comments, get_votes=database.get_votes, postid=postid, session=session, user=user) elif "username" in session: author = session['username'] user = database.get_user(author) database.add_comment(None, content, postid, author) users.update( user, {'$push':{'comments':postid}} ) #return redirect(url_for("post",postid=postid,session=session)) #return redirect(url_for("post",postid=postid)) return redirect(url_for("post",postid=postid,session=session, isError=isError, error=error))
def get_comments(user, title): if not user or not title: return None comments = database.get_comments(user, title) if not comments: return None output = [] for c in comments: output.append({ 'username': c['username'], 'author': c['author'], 'date_uploaded': c['date_uploaded'], 'date_uploaded_formatted': get_formatted_time(get_epoch_time(c['date_uploaded'])), 'date_added': c['date_added'], 'date_added_formatted': get_formatted_time(get_epoch_time(c['date_added'])), 'message': c['message'] }) return output
def show_cloud(): # Read all of comments. total_comment_text = "" for comment in database.get_comments(): total_comment_text += re.sub('<[^<]+?>', '', comment['text']).replace( '\n', '').strip() # re用来去除html标签 # Cut sentences to short words. wordlist = jieba.lcut(total_comment_text) wordliststr = " ".join(wordlist) font = os.path.join(os.path.dirname(__file__), "word_cloud_yahei.ttf") mask = np.array(Image.open( 'word_cloud_mask.png')) # background and shape of the word cloud # Get the word cloud. wd = wordcloud.WordCloud(scale=8, width=1920, height=1080, font_path=font, mask=mask, max_font_size=100, min_font_size=12, background_color="white", stopwords=get_stop_words()).generate(wordliststr) image_colors = wordcloud.ImageColorGenerator(mask) wd.recolor(color_func=image_colors) # color is from the background image plt.figure() plt.imshow(wd) plt.axis("off") plt.show()
def correct_response(): # Checks that the data has been updated in the users table user_data1 = main.get_user_data("new_user_name") user_data2 = main.get_user_data("new_e_mail") scen_1 = user_data1["user_name"] == user_data2["user_name"] == "new_user_name" scen_2 = user_data1["e_mail"] == user_data2["e_mail"] == "new_e_mail" scen_3 = user_data1["country"] == user_data2["country"] == "new_country" scen_4 = user_data1["city"] == user_data2["city"] == "new_city" scen_5 = user_data1["location"] == user_data2["location"] == "new_location" scen_6 = user_data1["avatar_image"] == user_data2["avatar_image"] == "new_image" scen_7 = response["response"] == "Success" users_updated = scen_1 and scen_2 and scen_3 and scen_4 and scen_5 and scen_6 and scen_7 # Checks that the data has been updated in the ideas table # Since an idea by the user who's user name has been updated # had posted an idea, the function get_most_recent_idea returns # a non-empty list when using the new user name as parameter ideas_updated = db.get_most_recent_idea("new_user_name") != [] # Checks that the data has been updated in the messages table # Since the original user has sent a message, the return value # from get conversation using the new user name and the other # user's name has to return a non-empty list messages_updated = db.get_conversation("new_user_name", "TestUser2") != [] # Checks that the data has been updated in the comments table # The user who's name was changed posted a comment on the idea with id 2 comments_updated = False for comment in db.get_comments(2): if "new_user_name" in comment: comments_updated = True return users_updated and ideas_updated and messages_updated and comments_updated
def story(): if request.method=="POST": if request.form["button"]=="comment": comment=str(request.form["comments"]) title=str(request.form["title"]) database.add_comment(title,comment) return render_template("story.html",title=title,comments=database.get_comments(title)) if request.form["button"]=="go back": return redirect(url_for("index"))
def get(self): comments = get_comments() comments_html = ''.join([ '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td><a href="/delete/{0}" onclick="return removeComment(this);">Удалить</a></td></tr>' .format(*item) for item in comments ]) try: with open('templates/view.html') as template_file: template = Template(template_file.read()) except IOError: return 'Not Found', '404 NOT FOUND' result = template.substitute({'comments': comments_html}) return result, '200 OK'
def index(): if request.method=='GET': return render_template("home.html",titles=database.get_stories()) if request.method=='POST': if(request.form["button"]=="Submit"): database.add_story(str(request.form['title'])) return render_template("home.html",titles=database.get_stories()) elif (request.form["button"]=="Go To Story"): title=str(request.form['select']) assert title!="" return render_template("story.html",title=title,comments=database.get_comments(title)) elif (request.form["button"]=="Delete Story"): title=str(request.form['select']) database.delete_story(title) return render_template("home.html",titles=database.get_stories())
def get_sentence_statistics(): counts = {} for comment in database.get_comments(): sentence = re.sub('<[^<]+?>', '', comment['text']).replace('\n', '').strip() # re用来去除html标签 counts[len(sentence)] = counts.get(len(sentence), 0) + 1 items = list(counts.items()) items.sort(key=lambda x: x[0], reverse=True) for i in range(30): word, count = items[i] print("{0:<10}{1:<5}".format(word, count)) return items
def application(environ, start_response): status = '200 OK' number_visitors = visitors.count_visitors() current_page = 1 error_message = '' if environ['QUERY_STRING'] != "": current_page, error_message = parse_get_request(environ['QUERY_STRING']) # забираем комментарии из базы данных comments = database.get_comments() # отрисовываем страницу page = comment_page.add_comments_page(comments, current_page, error_message, number_visitors) response_headers = [('Content-type', 'text/html'), ('Content-Length', str(len(page)))] start_response(status, response_headers) return [page]
def on_comments(): if request.method == 'POST': if not request.is_json: author = request.form.get('author') title = request.form.get('title') content = request.form.get('content') else: author = request.get_json().get('author') title = request.get_json().get('title') content = request.get_json().get('content') comment = db.create_comment(title, content, author) socketio.emit('new comment', {'comment': model_to_dict(comment)}, broadcast=True) return jsonify({'comment': model_to_dict(comment)}) comments = db.get_comments().dicts() return jsonify({'comments': [comment for comment in comments]})
def application(environ, start_response): status = '200 OK' page = read_html_page() current_page = 1 error_message = '' if environ['QUERY_STRING'] != "": get_params = parse_qs(environ['QUERY_STRING']) if get_params.get("page"): current_page = int(get_params.get("page")[0]) else: if get_params.get("comment"): if get_params.get("firstname"): if get_params.get("lastname"): valid_email = True if get_params.get("email"): valid_email = validate_email(get_params.get("email")[0]) if valid_email: commentator_name = get_params.get("firstname")[0] comment = get_params.get("comment")[0] database.add_comment(comment, commentator_name) else: error_message = 'Error invalid email' else: error_message = 'Error empty lastname' else: error_message = 'Error empty name' else: error_message = "Error empty comment" comments = database.get_comments() page = comment_page.add_comments_page(page, comments, current_page, error_message) response_headers = [ ('Content-type', 'text/html'), ('Content-Length', str(len(page)))] start_response(status, response_headers) return [page]
def get_word_statistics(): total_comment_text = "" for comment in database.get_comments(): total_comment_text += re.sub('<[^<]+?>', '', comment['text']).replace( '\n', '').strip() # re用来去除html标签 words = jieba.lcut(total_comment_text) stop_words = get_stop_words() counts = {} for word in words: if len(word) == 1: continue elif word in stop_words: continue else: counts[word] = counts.get(word, 0) + 1 items = list(counts.items()) items.sort(key=lambda x: x[1], reverse=True) for i in range(30): word, count = items[i] print("{0:<10}{1:<5}".format(word, count)) return items
# coding=utf-8 # Code by SmallSquare, 2020/5. # An example that show how to prepare data before wordCloudAnalysis. # 展示如何在数据分析之前获得数据。 import spider.spider_main as spider import database # Use spider like following. # 这样用爬虫爬数据。 database.del_all("movie") database.del_all("comment") database.insert_movie(spider.getMoviesInfor(10)) database.insert_comment(spider.getMovieShortComments(26885074, 2), 26885074) # Get data from database. # 这样获取数据库里的数据。 print(database.get_movies()) print(database.get_comments(26885074))
password=MONGO_PYTHON_DAEMON_PASSWORD, authSource=MONGO_INITDB_DATABASE, authMechanism='SCRAM-SHA-1') db = client[MONGO_INITDB_DATABASE] sub_coll = db['reddit_submissions'] comm_coll = db['reddit_comments'] sub_coll.delete_many({}) comm_coll.delete_many({}) print("Fetching data from {} to {}".format(starting_time.isoformat(), ending_time.isoformat())) r_sub = get_submissions(Config.URL_SUB, field_list=Config.SUBMISSION_FIELD_LIST, subreddit_list=Config.SUBREDDIT_LIST, after=int(starting_time.timestamp()), before=int(ending_time.timestamp())) print("Fetched {} reddit submissions".format(len(r_sub.json()['data']))) if r_sub.json()['data']: sub_coll.insert_many(r_sub.json()['data']) for sub in r_sub.json()['data']: #Get list of comments r_ids = get_comments_id(Config.URL_COMMENTS_ID, r_sub.json()['data'][0]['id']) id_list = r_ids.json()['data'] if id_list: r_comments = get_comments(Config.URL_COMMENTS, field_list=Config.COMMENT_FIELD_LIST, id_list=id_list) comm_coll.insert_many(r_comments.json()['data'])
def get_comments(idea_id): comments = db.get_comments(idea_id) return {"response": "Success", "comments": comments}
def test_get_comments(self): database.add_story("new story") database.add_comment("new story","comment one") database.add_comment("new story","comment one") comments=database.get_comments("new story") self.assertEqual(["comment one","comment one"],comments)
def test_add_comment(self): database.add_story("new story") database.add_comment("new story","comment one") database.add_comment("new story","comment two") self.assertEqual(2,len(database.get_comments("new story")))
def get_comments(idea_id): comments = db.get_comments(idea_id) return jsonify({"response": ["Success"], "comments": comments})
import database con = database.set_up_db("check.db"); cur= con.cursor() database.create_table(cur) database.insert_comment(cur, "Isha", "Trying hard") print(database.get_comments(cur)) # database.drop_all_tables(cur) con.commit() con.close()