spark_api.py 1.1 KB
Newer Older
1
from loguru import logger
kernel.h@qq.com's avatar
kernel.h@qq.com committed
2

赵小蒙's avatar
赵小蒙 committed
3
from magic_pdf.libs.drop_reason import DropReason
kernel.h@qq.com's avatar
kernel.h@qq.com committed
4
5


赵小蒙's avatar
赵小蒙 committed
6
7
8
9
10
def get_data_source(jso: dict):
    data_source = jso.get("data_source")
    if data_source is None:
        data_source = jso.get("file_source")
    return data_source
赵小蒙's avatar
赵小蒙 committed
11
12


赵小蒙's avatar
赵小蒙 committed
13
14
15
16
17
def get_data_type(jso: dict):
    data_type = jso.get("data_type")
    if data_type is None:
        data_type = jso.get("file_type")
    return data_type
kernel.h@qq.com's avatar
kernel.h@qq.com committed
18
19


赵小蒙's avatar
赵小蒙 committed
20
21
22
23
24
def get_bookid(jso: dict):
    book_id = jso.get("bookid")
    if book_id is None:
        book_id = jso.get("original_file_id")
    return book_id
kernel.h@qq.com's avatar
kernel.h@qq.com committed
25
26


赵小蒙's avatar
赵小蒙 committed
27
28
29
30
31
32
def exception_handler(jso: dict, e):
    logger.exception(e)
    jso["_need_drop"] = True
    jso["_drop_reason"] = DropReason.Exception
    jso["_exception"] = f"ERROR: {e}"
    return jso
赵小蒙's avatar
赵小蒙 committed
33

34

赵小蒙's avatar
赵小蒙 committed
35
36
37
38
39
def get_bookname(jso: dict):
    data_source = get_data_source(jso)
    file_id = jso.get("file_id")
    book_name = f"{data_source}/{file_id}"
    return book_name
40
41


42
43
44
45
46
47
48
49
50
51
def spark_json_extractor(jso: dict) -> dict:

    """
    从json中提取数据,返回一个dict
    """

    return {
        "_pdf_type": jso["_pdf_type"],
        "model_list": jso["doc_layout_result"],
    }