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'))
Beispiel #2
0
nonexist_file = 'nonexist.csv'

with open(nonexist_file) as f:
    reader = csv.reader(f)
    nonexist = {row[0] for row in reader}

with open(data_file) as f:
    reader = csv.reader(f)
    usernames = {row[2]: row[3] for row in reader if row[2] not in nonexist}
    total = len(usernames)

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()
Beispiel #3
0
if __name__ == '__main__':
    from tables import Comment, User, db
    from collections import defaultdict

    comment_groups = Comment.query.with_entities(Comment.author, Comment.subreddit, db.func.count(Comment.subreddit))\
                            .group_by(Comment.author, Comment.subreddit)\
                            .all()

    subreddit_counts = defaultdict(dict)
    for author, subreddit, count in comment_groups:
        subreddit_counts[author][subreddit] = count

    features, labels = [], []
    for author, subs in subreddit_counts.items():
        user = User.from_name(author)
        if user and user.stance:
            features.append(subs)
            labels.append((user.stance.v_pos, user.stance.h_pos))

    labels = np.array(labels)
    features = pd.Series(features)

    splitter = StratifiedShuffleSplit(n_splits=1,
                                      test_size=0.2,
                                      random_state=42)
    for train_index, test_index in splitter.split(features, labels):
        X_train, y_train = features[train_index], labels[train_index]
        X_test, y_test = features[test_index], labels[test_index]

    if not args.noval: