from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-*xxgw(z=n06z!e)k8!(+tk7v4$=qu)9&3hgcmlm2tp-@bke)c2'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'unlocker',
    'drf_yasg'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'medium.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'medium.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = 'static/'

STATIC_ROOT = BASE_DIR / "staticfiles"

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

medium/settings.py
sudipnext/medium-unlocker
from django.contrib import admin
from django.urls import path, include
from drf_yasg.views import get_schema_view
from rest_framework import permissions
from drf_yasg import openapi
from django.conf.urls.static import static
from django.conf import settings

schema_view = get_schema_view(
    openapi.Info(
        title="Medium-Unlocker API",
        default_version='V 0.0.1',
        description="This API is solely made for Educational Purposes",
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('unlocker.urls')),
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), 
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

medium/urls.py
sudipnext/medium-unlocker
from django.apps import AppConfig


class UnlockerConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'unlocker'

unlocker/apps.py
sudipnext/medium-unlocker
from django.urls import path
from .views import UnlockerViewSet

urlpatterns = [
    path('unlock/', UnlockerViewSet.as_view({'get': 'retrieve'}), name='data-retrieve'),
    path('unlock/api/', UnlockerViewSet.as_view({'get': 'api'}), name='data-api')
]

unlocker/urls.py
sudipnext/medium-unlocker
from bs4 import BeautifulSoup
from django.urls import reverse

def update_links_to_proxy(soup: BeautifulSoup, is_api: bool=False):
    """Modify all anchor tags in the soup to route through the viewset."""
    for a_tag in soup.find_all('a', href=True):
        original_link = a_tag['href']
        proxy_url = reverse(f"data-{'api' if is_api else 'retrieve'}") + f"?url={original_link}"
        a_tag['href'] = proxy_url
    return soup

unlocker/utils.py
sudipnext/medium-unlocker
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from bs4 import BeautifulSoup
import re
import requests
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from html import unescape

from unlocker.utils import update_links_to_proxy


class UnlockerViewSet(viewsets.ViewSet):

    def retrieve_content(self, url: str, is_api: bool=False):
        validator = URLValidator()
        try:
            validator(url)
        except ValidationError:
            raise ValueError("Invalid URL format.")

        try:
            response = requests.get(f"http://webcache.googleusercontent.com/search?q=cache:{url}&strip=0&vwsrc=1&&hl=en&lr=lang_en")
            response.raise_for_status()
            soup = BeautifulSoup(response.text, 'html.parser')
            pres = soup.find_all('pre')

            if not pres:
                raise ValueError("No Data found")

            text_data = unescape(pres[0].get_text())
            text_data = re.sub(r'<script[^>]*>.*?</script>', '', text_data).strip()
            return update_links_to_proxy(BeautifulSoup(text_data, 'html.parser'), is_api)

        except requests.RequestException:
            raise ValueError("Invalid URL or Request error")

    @swagger_auto_schema(
        operation_description="Unlock and render the page",
        manual_parameters=[
            openapi.Parameter('url', openapi.IN_QUERY, description="URL to retrieve content from", type=openapi.TYPE_STRING)
        ],
        responses={
            200: 'Rendered HTML content'
        }
    )
    def retrieve(self, request: HttpRequest):
        try:
            content = self.retrieve_content(request.GET.get('url', ''))
            return HttpResponse(str(content), content_type="text/html")
        except ValueError as e:
            return HttpResponse(str(e), status=status.HTTP_400_BAD_REQUEST)

    @action(detail=False, methods=['GET'])
    @swagger_auto_schema(
        operation_description="Unlock content and return as API response",
        manual_parameters=[
            openapi.Parameter('url', openapi.IN_QUERY, description="URL to retrieve content from", type=openapi.TYPE_STRING)
        ],
        responses={
            200: 'JSON Response containing the unlocked content'
        }
    )
    def api(self, request: HttpRequest):
        try:
            content = self.retrieve_content(request.GET.get('url', ''), is_api=True)
            return Response({"content": str(content)})
        except ValueError as e:
            return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)

unlocker/views.py
sudipnext/medium-unlocker