def success(): if request.method == 'POST': username = request.form['username'] if not username: return render_template("failure.html", error='Please enter a username') # Check if user is in db user = User.from_name(username) if not user: try: user_data = get_user_data(username) comments_data = get_comment_data(username) except HTTPError as err: if err.response.status_code == 404: return render_template( "failure.html", error=f'User \'{username}\' does not exist') return render_template("failure.html", error='External API error') user = User(**user_data) user.comments = [ Comment(**comment_data) for comment_data in comments_data ] user.prediction = pred_lean(user) db.session.add(user) user.searches += 1 if not user.prediction: user.prediction = pred_lean(user) db.session.commit() return render_template( "success.html", stance_name=user.prediction.stance_name(), user=user.name, img=user.prediction.img(), h_fullstance=user.prediction.stance_name(axis='h_binary'), v_fullstance=user.prediction.stance_name(axis='v_binary'), h_confidence=f'{abs(user.prediction.h_pos):.0%}', v_confidence=f'{abs(user.prediction.v_pos):.0%}') elif request.method == 'GET': return redirect(url_for('index'))
nonexist_new = set() # Buffer-Like construction that we use to write non-existent users to csv for i, (username, stance_name) in enumerate(usernames.items()): if stance_name == 'libright2': stance_name = 'libright' if stance_name != 'None' and not User.from_name(username): try: user_data = get_user_data(username) comments_data = get_comment_data(username) except HTTPError as e: print(f'{username:<24} | HTTPError {e.response.status_code} thrown') if e.response.status_code in [404, 403]: nonexist_new.add(username) continue user = User(**user_data) user.comments = [Comment(**comment_data) for comment_data in comments_data] user.stance = Stance.from_str(username, stance_name) db.session.add(user) db.session.commit() print(f'{username:<24} | {len(comments_data)} comments gathered') if i % 20 == 0: tablesize = db.engine.execute("SELECT pg_size_pretty(pg_total_relation_size('public.comment'))").first()[0] print(f'Table size in db: {tablesize}') with open(nonexist_file, 'a') as f: writer = csv.writer(f) writer.writerows([[name] for name in nonexist_new]) nonexist_new = set()