aboutsummaryrefslogtreecommitdiff
path: root/blog
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-06-06 18:15:28 +0000
committerBobby <[email protected]>2024-06-06 18:15:28 +0000
commitdebfb4676328d587d31d44cc679c3d382fadd6ed (patch)
tree604fe923a7f2e0971f6a35ec3f33d4328cde6e5e /blog
parent57d54e24ac6fe72ac8a37236e827914c0dd235ee (diff)
downloadthatcomputerscientist-debfb4676328d587d31d44cc679c3d382fadd6ed.tar.xz
thatcomputerscientist-debfb4676328d587d31d44cc679c3d382fadd6ed.zip
Spam Comments Fix
Diffstat (limited to 'blog')
-rw-r--r--blog/context_processors.py10
-rw-r--r--blog/views.py13
2 files changed, 16 insertions, 7 deletions
diff --git a/blog/context_processors.py b/blog/context_processors.py
index 99655dc2..ebb19577 100644
--- a/blog/context_processors.py
+++ b/blog/context_processors.py
@@ -28,7 +28,7 @@ akismet_api = akismet.Akismet(
)
-def check_spam(comment):
+def check_spam(comment, post):
# spam = False
# akismet_data = {
# "comment_type": "comment",
@@ -47,14 +47,18 @@ def check_spam(comment):
else:
genai.configure(api_key=gemini_api_key)
- model = genai.GenerativeModel("gemini-1.5-flash")
+ model = genai.GenerativeModel("gemini-pro")
- input_prompt = f"Comment Processing Checker. This is for a personal blog site. Output only Y or N for the included text. Y if the comment seems like spam. N if the comment seems safe. Do not access links. Just mark Y or N for the text. \n\nText: {comment}"
+ input_prompt = f"Comment Processing Checker. This is for a personal blog site. Output only Y or N for the included text. Y if the comment seems like spam, unrelated to post or random gibberish. N if the comment seems safe. Do not access links. Just mark Y or N for the text. Post Title: {post.title}. \n Post Body: {post.body}. \n Comment: {comment}. \n Judge if the comment belongs on the post or not. Random texts, links, and gibberish are considered spam. Trying to phish or promote shady links are also considered spam. Please mark Y if the comment seems like spam. N if the comment seems safe. Only output a single character. Y or N."
response = model.generate_content(input_prompt)
r_text = response.text
+ r_text = r_text.strip()
+
+ print(r_text)
+
return r_text
diff --git a/blog/views.py b/blog/views.py
index f7fec1b4..ec4d9376 100644
--- a/blog/views.py
+++ b/blog/views.py
@@ -224,7 +224,8 @@ def comment(request, slug):
if request.method == 'POST':
if request.user.is_authenticated:
try:
- r_spam = check_spam(comment=request.POST.get('body'))
+ print(request.POST.get('comment'))
+ r_spam = check_spam(comment=request.POST.get('comment'), post=Post.objects.get(slug=slug))
if r_spam != 'N':
messages.error(request, r_spam, extra_tags='spam')
return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#new-comment')
@@ -259,7 +260,7 @@ def anon_comment(request, slug):
anonymous_token, at = request.POST.get('anonymous-token'), request.POST.get('anonymous-token')
new_anonymous_token = request.POST.get('new-anonymous-token')
anonymous_comment = request.POST.get('anonymous-comment')
- res_spam = check_spam(comment=anonymous_comment)
+ res_spam = check_spam(comment=anonymous_comment, post=Post.objects.get(slug=slug))
if res_spam != 'N':
messages.error(request, res_spam, extra_tags='spam')
return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#new-comment')
@@ -330,7 +331,9 @@ def edit_comment(request, slug):
user_ip = request.META.get('REMOTE_ADDR')
user_agent_string = request.META.get('HTTP_USER_AGENT', '')
user_agent = parse(user_agent_string)
- if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=request.user.username):
+ # if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=request.user.username
+ res_spam = check_spam(comment=request.POST.get('body'), post=comment.post)
+ if res_spam != 'N':
messages.error(request, request.POST.get('body'), extra_tags='spam')
return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#comment-' + str(comment.id))
if comment.user == request.user:
@@ -369,7 +372,9 @@ def anon_edit_comment(request, slug):
user_ip = request.META.get('REMOTE_ADDR')
user_agent_string = request.META.get('HTTP_USER_AGENT', '')
user_agent = parse(user_agent_string)
- if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=comment.anonymous_user.name):
+ res_spam = check_spam(comment=request.POST.get('body'), post=comment.post)
+ if res_spam != 'N':
+ # if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=comment.anonymous_user.name):
messages.error(request, request.POST.get('body'), extra_tags='spam')
return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#comment-' + str(comment.id))
if comment.anonymous_user.token == anonymous_token: