Skip to content

Commit

Permalink
Adding Google Captcha (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
animemoeus authored Sep 24, 2024
1 parent 41226b5 commit 1bded85
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,6 @@

# OpenAI
OPENAI_API_KEY = env.str("OPENAI_API_KEY", default="")

# Google Captcha
GOOGLE_CAPTCHA_SECRET_KEY = env.str("GOOGLE_CAPTCHA_SECRET_KEY", default="")
12 changes: 12 additions & 0 deletions instagram/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.test import TestCase, override_settings


class TestRoastingProfileView(TestCase):
def setUp(self):
self.instagram_username = "angiehsl"

@override_settings(DEBUG=True)
def test_get_roasting(self):
response = self.client.get(f"/instagram/roasting/{self.instagram_username}/?captcha=ARTERTENDEAN").json()
print(response.get("roasting_text"))
self.assertIsNotNone(response.get("roasting_text"))
4 changes: 2 additions & 2 deletions instagram/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import path

from .views import GetUserInfo, InstagramUserListView
from .views import InstagramUserListView, RoastingProfileView

urlpatterns = [
path("users/", InstagramUserListView.as_view(), name="instagram_user_list"),
path("get-user-info/<str:username>/", GetUserInfo.as_view(), name="get-user-info"),
path("roasting/<str:username>/", RoastingProfileView.as_view(), name="roasting"),
]

app_name = "instagram"
10 changes: 7 additions & 3 deletions instagram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ def get_instagram_roasting_text(cls, data: str) -> str:
{formatted_user_data}
roasting foto profilnya
roasting username dan atau namanya
roasting namanya jika ada, kalo ga bilang dia anak haram dsb
roasting usernamenya
roasting foto profilnya, kalo kosong: bilang orangnya jelek
roasting jumlah following, follower dan post
roasting kategori profilenya jika ada, tuduh dia ngaku ngaku
roasting biografinya jika ada, jika kosong: berikan roasting yang berhubungan dengan no life
roasting info lainnya juga, jangan lupa pake emoji
jangan pake list, langsung teks roastingnya
jangan ada key dari json yang muncul
buat dalam satu kalimat yang panjang
kalimatnya pake efek barnum hingga pembaca merasa ini dibuat khusus untuk dia
""",
},
],
Expand Down
29 changes: 22 additions & 7 deletions instagram/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import requests
from django.conf import settings
from rest_framework import filters
from rest_framework.authentication import BasicAuthentication, TokenAuthentication
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import User as InstagramUser
from .pagination import InstagramUserPagination
from .serializers import InstagramUserSerializer
from .utils import InstagramAPI
from .utils import InstagramAPI, RoastingIG


class InstagramUserListView(ListAPIView):
Expand All @@ -27,11 +27,26 @@ class InstagramUserListView(ListAPIView):
ordering = ["-created_at"]


class GetUserInfo(APIView):
authentication_classes = [TokenAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]

class RoastingProfileView(APIView):
def get(self, request, username: str):
captcha = request.GET.get("captcha", "")
if not self.recaptcha_validation(captcha):
return Response({"error": "Invalid Captcha"}, status=400)

instagram_api = InstagramAPI()
user_info = instagram_api.get_user_info_v2(username)
roasting_text = RoastingIG.get_instagram_roasting_text(user_info)

user_info["roasting_text"] = roasting_text
return Response(user_info)

def recaptcha_validation(self, captcha) -> bool:
if settings.DEBUG and captcha == "ARTERTENDEAN":
return True

url = "https://www.google.com/recaptcha/api/siteverify"
payload = {"secret": settings.GOOGLE_CAPTCHA_SECRET_KEY, "response": captcha}

response = requests.request("POST", url, data=payload)

return response.json().get("success", False)

0 comments on commit 1bded85

Please sign in to comment.