File size: 4,988 Bytes
947c08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

import json, environ, requests, os, subprocess
import asyncio, uuid

from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest
from django_ratelimit.decorators import ratelimit
from django.views.decorators.csrf import csrf_exempt
from asgiref.sync import sync_to_async

from backend.module import web_scrap
from backend.module.utils import manage_image
from backend.models.model_cache import SocketRequestChapterQueueCache, ComicStorageCache
from core.settings import BASE_DIR
from backend.module.utils import cloudflare_turnstile


env = environ.Env()


@csrf_exempt
@ratelimit(key='ip', rate='60/m')
def request_chapter(request):
    try:
        if request.method != "POST": return HttpResponseBadRequest('Allowed POST request only!', status=400)
        token = request.META.get('HTTP_X_CLOUDFLARE_TURNSTILE_TOKEN')
        if not cloudflare_turnstile.check(token): return HttpResponseBadRequest('Cloudflare turnstile token not existed or expired!', status=511)
        
        payload = json.loads(request.body)
        source = payload.get("source")
        comic_id = payload.get("comic_id")
        chapter_id = payload.get("chapter_id")
        chapter_idx = payload.get("chapter_idx")
        socket_id = payload.get("socket_id")
        channel_name = payload.get("channel_name")
        options = payload.get("options") or {}
        
        options["translate"]["target"] = options.get("translate").get("target") if options.get("translate").get("state") else ""
        query_count = ComicStorageCache.objects.filter(
            source=source, 
            comic_id=comic_id, 
            chapter_id=chapter_id,
            chapter_idx=chapter_idx,
            colorize=options.get("colorize"),
            translate=options.get("translate").get("state"),
            target_lang = options.get("translate").get("target")
        ).count()
        
        if query_count: return JsonResponse({"status":"ready"})
        else:
            SocketRequestChapterQueueCache(
                socket_id=socket_id, 
                channel_name=channel_name, 
                source=source, 
                comic_id=comic_id,
                chapter_id=chapter_id, 
                chapter_idx=chapter_idx,
                options=options
            ).save()
            return JsonResponse({"status":"queue"})
    except Exception as e: 
        print(e)
        return HttpResponseBadRequest('Internal Error.', status=500)


@csrf_exempt
@ratelimit(key='ip', rate='60/m')
def request_info(request):
    try:
        if request.method != "POST": return HttpResponseBadRequest('Allowed POST request only!', status=400)
        token = request.META.get('HTTP_X_CLOUDFLARE_TURNSTILE_TOKEN')
        if not cloudflare_turnstile.check(token): return HttpResponseBadRequest('Cloudflare turnstile token not existed or expired!', status=511)

        payload = json.loads(request.body)
        socket_id = payload.get("socket_id")
        source = payload.get("source")
        comic_id = payload.get("comic_id")
        chapter_requested = payload.get("chapter_requested")
        
        
        result_request = {}
        for chapter in chapter_requested:
            options = chapter.get("options")
            query_count = SocketRequestChapterQueueCache.objects.filter(socket_id=socket_id, source=source, comic_id=comic_id, chapter_id=chapter.get("chapter_id")).count()
            if query_count: result_request[chapter.get("chapter_id")] =  {"state":"queue","chapter_idx":chapter.get("chapter_idx"),"options":options}
            else:
                query_result = ComicStorageCache.objects.filter(
                    source=source, 
                    comic_id=comic_id, 
                    chapter_id=chapter.get("chapter_id"),
                    chapter_idx=chapter.get("chapter_idx"),
                    colorize=options.get("colorize"),
                    translate=options.get("translate").get("state"),
                    target_lang = options.get("translate").get("target") if options.get("translate").get("state") else ""
                ).first()
                
                if query_result:
                    file_path = query_result.file_path
                    if os.path.exists(file_path): 
                        result_request[chapter.get("chapter_id")] = {"state":"ready","chapter_idx":chapter.get("chapter_idx"),"options":options}
                    else:
                        ComicStorageCache.objects.filter(id=query_result.id).delete()
                        result_request[chapter.get("chapter_id")] = {"state":"unkown","chapter_idx":chapter.get("chapter_idx"),"options":options}
                else:
                    result_request[chapter.get("chapter_id")] = {"state":"unkown","chapter_idx":chapter.get("chapter_idx"),"options":options}
        return JsonResponse(result_request)
        
    except Exception as e: 
        print(e)
        return HttpResponseBadRequest('Internal Error.', status=500)