Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple targets for /posts/findTarget and add repost and feedbacks fields #42

Merged
merged 3 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 28 additions & 9 deletions copypasta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sqlite3
import subprocess
import sys
import validators
from datetime import datetime
from html import unescape
from flask import Flask, render_template, request, jsonify, g, redirect, url_for
Expand Down Expand Up @@ -222,12 +223,19 @@ def get_pending():
@app.route("/posts/findTarget", methods=['GET'])
def get_target():
try:
url_one = request.args["url"]
except KeyError as error:
return jsonify({"status": "failure",
"message": "Error - Missing argument {}".format(error.args[0])}), 400
targets = retrieve_targets(url_one)
posts = [{"post_id": i, "target_url": j} for i, j in targets]
urls = request.args["url"]
except KeyError as e:
return jsonify({"status": "failure", "message": "Error - Missing argument {}".format(e.args[0])}), 400
urls_split = urls.split(',')
# Check if URLs are actually valid before searching for posts
if not all(validators.url("https:" + url) for url in urls_split):
return jsonify({"status": "failure", "message": "One or more URLs are invalid"}), 400
targets = retrieve_targets(urls_split)
post_ids = [item[0] for item in targets]
feedbacks = get_feedbacks_for_posts(post_ids)
posts = [{"post_id": post_id, "original_url": original, "target_url": target, "repost": user1 == user2,
"feedbacks": [[feedback, link] for id_post, feedback, link in feedbacks if id_post == post_id]}
for post_id, original, target, user1, user2 in targets]
return jsonify({"status": "success", "posts": posts})


Expand Down Expand Up @@ -417,11 +425,22 @@ def fetch_posts_without_feedback_with_details():
return cur.fetchall()


def retrieve_targets(url_one):
def retrieve_targets(urls_split):
with app.app_context():
question_marks = ",".join(["?" for _ in urls_split])
cur = get_db().cursor()
cur.execute("SELECT posts.post_id, url_one, url_two, user_url_one, user_url_two FROM posts "
"WHERE url_one IN (" + question_marks + ")", (*urls_split,))
targets = cur.fetchall()
return targets


def get_feedbacks_for_posts(post_ids):
with app.app_context():
question_marks = ",".join(["?" for _ in post_ids])
cur = get_db().cursor()
cur.execute("SELECT post_id, url_two FROM posts "
"WHERE url_one=?", (url_one,))
cur.execute("SELECT post_id, feedback_type, link FROM feedback "
"WHERE post_id IN (" + question_marks + ")", (*post_ids,))
targets = cur.fetchall()
return targets

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flake8~=3.8.0
Flask>0.12.2

validators>0.18.1