File size: 3,369 Bytes
b60402f
 
 
 
ab0a73d
b60402f
 
 
 
 
 
 
 
 
 
ab0a73d
 
 
 
 
 
b60402f
 
 
ab0a73d
b60402f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab0a73d
 
 
 
 
 
 
 
 
 
b60402f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab0a73d
b60402f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab0a73d
 
 
 
 
 
 
 
 
 
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
"""
Chat API views module.
"""

import asyncio
from fastapi import Query

from app.api.common.db_requests import (
    get_all_objs,
    delete_obj,
    get_obj_by_id,
)
from app.api.common.dto import Paging
from app.api.common.schemas import AllObjectsResponse, FilterRequest
from app.api.scraper import scraper_router
from app.api.scraper.db_requests import (
    filter_jobs,
    search_field_options,
    get_statistics,
)
from app.api.scraper.schemas import SearchOptionRequest, StatisticsResponse
from app.api.scraper.dto import JobFilter
from app.api.scraper.models import JobModel
from app.core.wrappers import CbhResponseWrapper
from app.api.scraper.services import run_update


@scraper_router.get("/all")
async def get_all_jobs(
    pageSize: int = Query(  # pylint: disable=C0103
        10, description="Number of objects to return per page"
    ),
    pageIndex: int = Query(  # pylint: disable=C0103
        0, description="Page index to retrieve"
    ),
) -> CbhResponseWrapper[AllObjectsResponse[JobModel]]:
    """
    Get a paginated list of all jobs for a user.
    """
    jobs, total_count = await get_all_objs(JobModel, pageSize, pageIndex)
    response = AllObjectsResponse(
        paging=Paging(pageSize=pageSize, pageIndex=pageIndex, totalCount=total_count),
        data=jobs,
    )
    return CbhResponseWrapper(data=response)


@scraper_router.post("/filter")
async def scrape(
    search_request: FilterRequest[JobFilter],
) -> CbhResponseWrapper[AllObjectsResponse[JobModel]]:
    """
    Scrape a specific URL.
    """
    jobs, total_count = await filter_jobs(search_request)
    return CbhResponseWrapper(
        data=AllObjectsResponse(
            paging=Paging(
                pageSize=search_request.pageSize,
                pageIndex=search_request.pageIndex,
                totalCount=total_count,
            ),
            data=jobs,
        )
    )


@scraper_router.get("/statistics")
async def get_update_statistics() -> CbhResponseWrapper[StatisticsResponse]:
    """
    Get the update statistics.
    """
    statistics = await get_statistics()
    return CbhResponseWrapper(data=statistics)

@scraper_router.post("/option/{field}/search")
async def scrape_option_search(
    field: str,
    request: SearchOptionRequest,
) -> CbhResponseWrapper[AllObjectsResponse[str]]:
    """
    Scrape a specific URL.
    """
    objects = await search_field_options(field, request.value)
    return CbhResponseWrapper(
        data=AllObjectsResponse(
            paging=Paging(
                pageSize=5,
                pageIndex=0,
                totalCount=len(objects),
            ),
            data=objects,
        )
    )


@scraper_router.get("/{jobId}")
async def get_job(
    jobId: str,  # pylint: disable=C0103
) -> CbhResponseWrapper[JobModel]:
    """
    Get a specific job by ID.
    """
    job = await get_obj_by_id(JobModel, jobId)
    return CbhResponseWrapper(data=job)


@scraper_router.delete("/{jobId}")
async def delete_job(
    jobId: str,  # pylint: disable=C0103
) -> CbhResponseWrapper:
    """
    Delete a specific job by ID.
    """
    await delete_obj(JobModel, jobId)
    return CbhResponseWrapper()


@scraper_router.post("/run")
async def run_scraper() -> CbhResponseWrapper:
    """
    Run the scraper.
    """
    asyncio.create_task(run_update())
    return CbhResponseWrapper()