File size: 4,762 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
110
111

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 RequestCache
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 get_list(request):
    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)
    search = payload.get("search")
    page = payload.get("page")
    source = payload.get("source")
    
    if search.get("text"): DATA = web_scrap.source_control[source].search.scrap(search=search,page=page)
    else: DATA = web_scrap.source_control["colamanga"].get_list.scrap(page=page)

    return JsonResponse({"data":DATA}) 


@ratelimit(key='ip', rate='60/m')
def search(request):
    # if request.method != "POST": return HttpResponseBadRequest('Allowed POST request only!', status=400)
    try:
        DATA = web_scrap.source_control["colamanga"].search.scrap(search="妖")
        return JsonResponse({"data":DATA}) 
    except Exception as e:
        return HttpResponseBadRequest(str(e), status=500)
    


@csrf_exempt
@ratelimit(key='ip', rate='60/m')
def get(request):
    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)
    id = payload.get("id")
    source = payload.get("source")
    
    try:
        DATA = web_scrap.source_control[source].get.scrap(id=id)
        return JsonResponse({"data":DATA}) 
    except Exception as e:

        return HttpResponseBadRequest(str(e), status=500)


@ratelimit(key='ip', rate='60/m')
def get_cover(request,source,id,cover_id):
    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)
    
    try:
        DATA = web_scrap.source_control[source].get_cover.scrap(id=id,cover_id=cover_id)
        if not DATA: HttpResponseBadRequest('Image Not found!', status=404)
        response = HttpResponse(DATA, content_type="image/png")
        response['Content-Disposition'] = f'inline; filename="{id}-{cover_id}.png"'
        return response
    except Exception as e:
        return HttpResponseBadRequest(str(e), status=500)

    
def get_chapter(request):
    try:
        id = "manga-lo816008/1/410.html"
        job = web_scrap.source_control["colamanga"].get_chapter.scrap(id=id,output_dir=os.path.join(BASE_DIR,"media"))
        if job.get("status") == "success":
            chapter_id = id.split("/")[-1].split(".")[0]
            input_dir = os.path.join(BASE_DIR,"media",id.split("/")[0],chapter_id,"original")
            merged_output_dir =  os.path.join(BASE_DIR,"media",id.split("/")[0],chapter_id,"merged")
            os.makedirs(merged_output_dir,exist_ok=True)
            
            manage_image.merge_images_vertically(input_dir=input_dir,output_dir=merged_output_dir,max_size=10*1024*1024)

            translated_merged_output_dir = os.path.join(BASE_DIR,"media",id.split("/")[0],chapter_id,"merged_translated")
            os.makedirs(translated_merged_output_dir,exist_ok=True)
        
        
            subprocess.run(
                ["python", "-m", "manga_translator", "-v", "--manga2eng", "--translator=m2m100_big", "-l", "ENG", "-i", f"{merged_output_dir}", "-o", f"{translated_merged_output_dir}"],
                cwd=os.path.join(BASE_DIR,"backend","module","utils","image_translator"), shell=True, check=True
            )
            
            translated_splited_output_dir = os.path.join(BASE_DIR,"media",id.split("/")[0],chapter_id,"translated")
            
            os.makedirs(translated_splited_output_dir,exist_ok=True)
            manage_image.split_image_vertically(input_dir=translated_merged_output_dir,output_dir=translated_splited_output_dir)
    except Exception as e:
        print(e)
            
    return JsonResponse({})