File size: 4,621 Bytes
42d523e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f5bf4c
42d523e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f5bf4c
42d523e
4f5bf4c
 
 
 
 
 
 
 
 
 
 
42d523e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f5bf4c
42d523e
4f5bf4c
 
 
 
 
 
 
 
 
 
42d523e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from fastapi import APIRouter, status
from typing import Optional
from helper.is_site_available import check_if_site_available
import time
import asyncio
from helper.error_messages import error_handler


router = APIRouter(tags=["Combo Routes"])


@router.get("/search")
async def get_search_combo(query: str, limit: Optional[int] = 0):
    start_time = time.time()
    query = query.lower()
    all_sites = check_if_site_available("1337x")
    sites_list = list(all_sites.keys())
    tasks = []
    COMBO = {"data": []}
    total_torrents_overall = 0
    ignoresites = ["glodls", "ybt", "tgx", "torlock", "torrentfunk"]
    for site in sites_list:
        if site not in ignoresites:
            limit = (
                all_sites[site]["limit"]
                if limit == 0 or limit > all_sites[site]["limit"]
                else limit
            )
            tasks.append(
                asyncio.create_task(
                    all_sites[site]["website"]().search(query, page=1, limit=limit)
                )
            )
    results = await asyncio.gather(*tasks)
    for res in results:
        if res is not None and len(res["data"]) > 0:
            for torrent in res["data"]:
                COMBO["data"].append(torrent)
            total_torrents_overall = total_torrents_overall + res["total"]
    COMBO["time"] = time.time() - start_time
    COMBO["total"] = total_torrents_overall
    if total_torrents_overall == 0:
        return error_handler(
            status_code=status.HTTP_404_NOT_FOUND,
            json_message={"error": "Result not found."},
        )
    return COMBO


@router.get("/trending")
async def get_all_trending(limit: Optional[int] = 0):
    start_time = time.time()
    # * just getting all_sites dictionary
    all_sites = check_if_site_available("1337x")
    sites_list = [
        site
        for site in all_sites.keys()
        if all_sites[site]["trending_available"] and all_sites[site]["website"]
    ]
    tasks = []
    COMBO = {"data": []}
    total_torrents_overall = 0
    ignoresites = ["glodls", "ybt", "tgx", "torlock", "torrentfunk"]
    for site in sites_list:
        if site not in ignoresites:
            limit = (
                all_sites[site]["limit"]
                if limit == 0 or limit > all_sites[site]["limit"]
                else limit
            )
            tasks.append(
                asyncio.create_task(
                    all_sites[site]["website"]().trending(
                        category=None, page=1, limit=limit
                    )
                )
            )
    results = await asyncio.gather(*tasks)
    for res in results:
        if res is not None and len(res["data"]) > 0:
            for torrent in res["data"]:
                COMBO["data"].append(torrent)
            total_torrents_overall = total_torrents_overall + res["total"]
    COMBO["time"] = time.time() - start_time
    COMBO["total"] = total_torrents_overall
    if total_torrents_overall == 0:
        return error_handler(
            status_code=status.HTTP_404_NOT_FOUND,
            json_message={"error": "Result not found."},
        )
    return COMBO


@router.get("/recent")
async def get_all_recent(limit: Optional[int] = 0):
    start_time = time.time()
    # just getting all_sites dictionary
    all_sites = check_if_site_available("1337x")
    sites_list = [
        site
        for site in all_sites.keys()
        if all_sites[site]["recent_available"] and all_sites[site]["website"]
    ]
    tasks = []
    COMBO = {"data": []}
    total_torrents_overall = 0
    ignoresites = ["glodls", "ybt", "tgx", "torlock", "torrentfunk"]
    for site in sites_list:
        if site not in ignoresites:
            limit = (
                all_sites[site]["limit"]
                if limit == 0 or limit > all_sites[site]["limit"]
                else limit
            )
            tasks.append(
                asyncio.create_task(
                    all_sites[site]["website"]().recent(category=None, page=1, limit=limit)
                )
            )
    results = await asyncio.gather(*tasks)
    for res in results:
        if res is not None and len(res["data"]) > 0:
            for torrent in res["data"]:
                COMBO["data"].append(torrent)
            total_torrents_overall = total_torrents_overall + res["total"]
    COMBO["time"] = time.time() - start_time
    COMBO["total"] = total_torrents_overall
    if total_torrents_overall == 0:
        return error_handler(
            status_code=status.HTTP_404_NOT_FOUND,
            json_message={"error": "Result not found."},
        )
    return COMBO